Sunday, June 3, 2018

GIT

GIT(Global information Tracker) is a persistent map.
Key is SHA1 hash of object and value is blob content

Git has 4 main objects tree, blob, commit, annotated tag

Porcelain Commands

1] git help                     -- For all help commnads
    git help -a                 -- list available sub commands 
    git help -g                 -- list available concept guides
2] git help commit        -- Help for specific command
    OR    git -h commit
3] Setting up git user
    git config --global user.name "abc xyz"
    git config --global user.email a@b.com
    git config --global color.ui true
    git config --global init.defaultBranch main    -- will add variable to global config

  git config core.filemode true               -- will add config to default scope (local)

  git config --unset diff.renames             -- will delete entry

  git config --get core.filemode              -- Will get value of key

  git config --global core.excludesFile '~/.gitignore'  -- will ignore files mentioned

  git config --global -l                      -- list all config 

    git config --global -e                                       -- edit config

    Four config types global, system, local, worktree. or using -f option any specific config file can be used
4] git init                      --  For new local empty repository. this creates a hidden directory ".git" which stores metadata with subdirectories for objectsrefs/headsrefs/tags, logs, hooks and template files.
     a) objects        -- has info & pack directory
     b) config         -- config file with settings of worktree
     c) HEAD        -- file has ref information
     d) refs/tags     -- 
     e) refs/heads  --

    git init myProject    -- create project with directory
5] git status                  -- To check what is changed after last commit. default option git status --long
    git status -s              -- To display status info in short format
    git status -u              -- To display untracked files
6] git add file_name     -- Will add given file to staging area
    git add -- Will add all listed files to staging area
    git add --all               -- Will add all new & modified files to staging area
    git add .                     -- Will add all new & modified files to staging area
    git add -A                  -- Will add all new & modified files to staging area
    git add *.txt              -- Will add all txt files from CURRENT directory
    git add docs/*.txt     -- Will add all txt files from docs directory
    git add "*.txt"           -- Will add all txt files from project
6.1] git am            -- Apply a series of patches from a mailbox and commits
6.2]git apply          -- apply a patch to files or index but does not commit 
6.3]git format-patch   -- Create patch 
6.4]git  send-email           --  Send a collection of patches as emails
6.5] git archive                   -- Create an archive of files from a named tree 
6.6] git bundle                    -- Move objects and refs by archive, creates .pack files
7] git commit -m "initial commit"
    -s    squash commits
    --dry-run
    -p        interactive patch selection
    -e        edit message
8] git diff HEAD - Shows what has changed since the last commit
  git diff HEAD^ - Shows what has changed since the commit before the latest commit.
  git diff --cached - Show what has been added to the index(staging area) via git add but not yet committed.   (diff of staged changes with committed changes)
  git diff - Show what has changed but hasn't been added to the index(staging area) yet via git add.
                      (diff of local changes with staged changes)
    git diff master origin/master  -- shows diff between local repo master branch with remote master branch
    git diff commit1 commit2     -- shows diff between two commits.
    git diff -w                               -- diff with ignoring white spaces 
    git diff --check       --  to get a list of all conflicted files in your repository
8.1] git diff-tree     --  Compares the content and mode of blobs found via two tree objects
9] git reset HEAD     --  Will unstage file. HEAD refers to last commit of file.
    git reset --hard  --  Resets the index and working tree.Will delete all uncommited and staged changes.
    git reset --soft     -- resets the head to <commit>. will move changes from local commit 
    git reset --mixed -- Resets the index but not the working tree. will move changes from local commit to staged(default option).
    git reset --merge    -- Resets the index and working tree.Will keep files which are different in index and working tree.
    git reset --soft HEAD^   -- Will undo last commit
10] git checkout -- file      -- Will discard current changes & will revert to last committed version.
      git checkout -b   -- Will create branch in local m/c
      git checkout -m branch_name     -- will do three way merge and checkout branch.
11] git commit -a -m "committing files"   -- Will not add new added unstaged files.
      git commit --amend -m "Added files"    -- Adding to a commit
      git commit --amend            -- Will open editor where last commit message can be edited.
      git commit -S -m "YOUR_COMMIT_MESSAGE" -- will sign commit
14] git remote add origin https://github.com/sac.git
      git remote show origin  --  Will display all local & remotely tracked branch names
      git remote rename 
      git remote remove
15] git push -u origin master    -- Will push branch "master" to repository "origin"
      git push origin :branch_name  --  Will push commits to remote branch
      git push --tags    All refs under refs/tags are pushed, in addition to refspecs
                explicitly listed on the command line
16] git pull -- is git-fetch followed by git-merge
      git pull --rebase origin master  -- Will rebase & pull from master to the branch
16.1] git-fetch - Download objects and refs from another/remote repository into local repository but not updating working dir
17] git log                                -- Will display all commit messages
      git log --oneline                 -- Will display commit messages in one line
      git log --oneline --graph --decorate. -- Will display abbrev commit msg with path, more info
      git log --since="3 days ago"  -- Will display commits in last 3 days
      git log --after="date"        -- Will display commits after given date
      git log commit1...commit2.   -- all commits between these two commit range
      git log -2                                -- Will show last 2 commits  
      git log -patch                         -- Will show difference
      git log branch1 branch2 ^branch3      --list all the commits which are reachable from branch1 or  
                 branch2,  but not from branch3
      git log branch1..branch2       -- 
      git log commit1...commit2   -- commits is the symmetric difference between the two operands
       git log --name-only      -- will display file names from each commit
17.1] git show   -- Shows one or more objects (blobs, trees, tags and commits).
44] git reflog   -- Will display all git entries from local repositories, even HARD deleted also.
      is a record of all commits that are or were referenced in your repo at any time on local m/c.
19] git maintenance                -- Run tasks to optimize Git repository data
20] git branch    --  Will display current branch name
      git branch <branch_name>   --  Will create a new branch with given name but will not checkout
      git branch -a    -- Will display all local and remote branch names
      git branch -m OLD_BRANCH_NAME NEW_BRANCH_NAME    -- Will rename branch
      git branch -r     -- will display name of all remote branches
      git branch -d branch_name     -- Will delete local branch if all changes are merged
      git branch -D branch_name     -- Will force delete local branch
25] git tag    -- Will list all tags
      git tag myTag    -- Will create tag with marker label as myTag
      git tag -a <TAG_NAME>     -- annotate
      git tag -d <TAG_NAME>     -- delete
      git tag -f <TAG_NAME>      -- force to change tag name if it is not pushed
26] git tag -a v.0.0.1 -m "version 0.0.1"  --   Will add new tag, annotated tag
28] git checkout branch_name
      git rebase master
      git checkout master
      git merge branch_name
      git checkout -b new_branch             -- Will create new local branch
29] git rebase <BRANCH_NAME>       -- Reapply commits on top of another base tip 
      git rebase -i HEAD~3       -- Will rebase branch in interactive mode with last three commits from head.
30] git rebase -i HEAD^         -- Will open editor with last commit with 'pick' word. if it is changed to 'reword' and message is changed then last commit message will be updated.
31] git rebase -i HEAD^        -- Will open editor with last commit with 'pick' word. If it is changed to 'edit' and saved then the last commit can be split into multiple commits.
32] git rebase -i HEAD~3        -- Will open editor with last 3 commits with 'pick' word. If last commit message is changed to 'squash', then  last commit will be merged with the previous of that commit.
33] git stash save                      -- Will save current changes to temp area. command is deprecated for push
      git stash push                      -- Similar to save and is default option.
      git stash save "message"     -- Will save current changes to temp area with message.
      git stash -u      -- Will save untracked files with tracked files
34] git stash apply                  -- Will apply latest saved changes from temp area.
      git stash apply stash@{0}  -- same as above command.
35] git stash list                      -- Will list all stash
36] git stash apply stash@{1}   -- Will apply second stash from last three stash records.
37] git stash drop                    -- Will delete the latest stash
38] git stash save --include-untracked    -- Will include untracked(unstaged) files.
39] git stash show  -- Will show details of latest stash content with last commit be4 stash created.
40] git stash clear   -- Will delete all stash entries.  
41] git filter-branch --tree-filter 'rm -f password.txt' -- --all        --  Will rewrite history. will delete entry of password.txt file from all commits.
42] git attributes  -- to specify line ending character for different file types.
43] git repo --
46] git ls-files  -- Will display all staged files
47] git mv level12.txt level.txt    -- Renaming files Move or rename a file, a directory, or a symlink
48] git rm level.txt       -- deleting files from git only
49] git config --global --alias.hist "log --all --graph --decorate --oneline"    --Will create alias "hist" for the command.  These commands are stored in .gitconfig file.
50] git cherry-pick SHA1_COMMIT
51] git add --patch
52] git revert                    -- reverts older commit
      git revert --edit          -- To edit the commit message prior to committing the revert 
63] git restore.                       -- Restore working tree files from index
53] git clean                     -- remove all unstaged files
54] git switch                   -- similar like checkout, difference is it prompts if there are any changes in staging and working area.
      git switch -c <new_branch_name>
55] git-rerere - Reuse recorded resolution of conflicted merges. --- 
   The resolution is stored and applied in the future if the same conflict is encountered.
     You need to set the configuration variable rerere.enabled in order to enable this command. Run this command when auto merge conflict happens. After manually resolving conflicts, run this command again to record conflict resolution. 
 In future if similar conflict is observed, then running this command will auto resolve conflict.
56] git gc                     -- Runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase

       performance), removing unreachable objects which may have been created from prior invocations of git add, packing refs, pruning reflog, rerere

       metadata or stale working trees. May also update ancillary indexes such as the commit-graph.  git gc --auto command will run the pre-auto-gc hook

57] git merge <BRANCH/TAG_NAME>   --  Default is -ff  fast forward

    git merge tag creates default commit, git merge branch by default does not commit.

    git merge BRANCH1 BRANCH2     -- Multiple branches can be merged.(octopus strategy

used for multiple branch merge)  

     ort  -- default merge strategy.

58] git-format-patch -     Prepare patches for e-mail submission
59] git grep   -   Look for specified patterns in the tracked files in the work tree, blobs registered in the index file, or blobs in given tree objects
61] git notes - Add or inspect object notes
62] git range-diff - Compare two commit ranges (e.g. two versions of a branch)
63] git tag  <tag name>  --  will create tag with given name, tag is nothing but a pointer to commit.
      git tag -a <tag name>   -- will create annotated tag, 
      git tag -d <tag_name> -- will delete tag
      git push --tags    -- will push all tags
      git push <tag name>   --  will push given tag 
64] git request-pull -- Generate a request asking your upstream project to pull changes into their tree.
65] git-shell - Restricted login shell for Git-only SSH access
66] git-receive-pack - Receive what is pushed into the repository, Invoked by git send-pack and updates the repository with the information fed from the remote end.
67] git-send-pack - Push objects over Git protocol to another repository

Plumbing Commands


1] git hash-object  --file name                -- Will generate hash key for given file
2] git cat-file -p SHA1                            -- Will display content from local commit.
3] git show-ref master                            -- Will show latest commits 
4] git filter-repo                                      -- Will help in removing file from previous commits(e.g. password  
                                                                   file from commts)
5] git submodule add <URL>
6] git custom scripts   create custom script and put in class path. script name MUST start with git-
    e.g. git-customScript.sh   file can be called from command prompt as git customScript
7] git bisect                                           -- give good and bad commit range , it will execute test cases and determine bad commit
    $ git bisect start HEAD HEAD~10 -- # culprit is among the last 10
$ git bisect run ~/test.sh
$ git bisect reset                   # quit the bisect session
8]'git describe' looks for the nearest tag reachable from the commit

.gitignore files can be multiple in a project in different  directories.

Default branch (master) can not be deleted unless some other branch is configured as default branch.

Git merge default strategy is ort.

Git refspec maps a branch in the local repository to a branch in a remote repository. 

Disadvantages of git over SSH -- It does not allow anonymous access and involves a more complicated setup process for contributors.

Why does Git garbage collect objects? -- To avoid storing unnecessary information or sending it over the network

Which command renames branch1 to branch2? -- git branch -m branch1 branch2

You are setting up a new project. Which files should you store in the version control system?  --   The minimum files necessary to rebuild the state of the project at a given point in time

Which git log command will produce the following output?

489f7ca4c (HEAD ->my-branch, origin/my-branch) Updated subtract function
81621dba9 Create subtract function
8dfddd479 Created new file math.cs
--   git log --oneline

Which command will you use to create and switch to a new branch named mybranch?  --   git switch -c mybranch

While analyzing the history of a local branch, git show HEAD@{4} displays a commit from another branch. Why?
--  You are using reflog shortnames instead of standard reference notation.

How do you clone a specific tag?
--  git clone --branch <tag_name> <repo_url>
    git checkout tags/<tag_name> -b <branch_name>

You store your local repository on a resource-constrained 
environment, for example, low hard disk space and no internet 
access. After working in the project for a while, you are 
running out of space, but you must keep working. You run 
git gc --auto, which does nothing to reduce your disk usage. 
How would you resolve the problem?
-- Change the gc.auto and gc.autopacklimit configuration settings.

 

What is the purpose of creating a submodule in your Git repository?

--  

To keep commits separate between two repositories

You create a new branch using the git branch mybranch command, but you
 do not check it out. What occurs when you next commit?
-- Git creates a new commit, but mybranch stays on the previous commit.

Several bugs have appeared since the last sprint ended. You must create 
a list of files that changed over the last two weeks to determine what 
files were modified. How would you obtain such information?
--  Use git log with a special format to print commits and dates, then 
use git show for the commits that fit that range with the
 --name-only flag to show a list of the files.

What is the difference between using git restore for a single file and 
the git clean command?
-- Restoring a file with git restore changes the file in the working 
directory to its state in the staged area, whereas git clean removes 
any untracked files from the working directory.

A developer on your team commits their latest changes and immediately 
pushes them to the repository. They include a file with sensitive 
authentication information in their commits. In addition to ensuring 
that it is safe to commit all the staged files, how could they avoid 
this situation?
--  Run git status before committing.

No comments:

Post a Comment