- Distributed Version Control(DVCS) e.g. Git, Mercurial(Hg), BitKeeper, Bazaar.
- Linus Torvalds created Linux and Git.
- Msysgit is good git version for Windows.
- Every object in Git has unique SHA1 key
Good links:
Porcelain Commands:
- Add
- Commit
- Push
- Fetch
- Pull
- Branch
- Checkout
- Merge
- Rebase
Plumbing Commands:
- Cat-file
- Hash-object
- Count-objects
Install git on Linux system:
- Debian/Ubuntu distro: apt-get install git-core
- Fedora distro: yum install git-core
Git version command:
|
|
Get help command:
|
|
Git areas(Mnemonic WIRES)
- Working-Area
- Index-Area
- Repository-Area (Local m/c)
- External-Repository-Area ( Remote m/c)
- Stash-Area
Pointers:
- HEAD: Pointer to a pointer. Points to current branch.
- Master: Pointer to default local branch.
- Origin: Pointer to default remote branch.
- Stash: Pointer to stashed content.
- BranchName: Pointer to branchName SHA1 hash.
.gitignore
|
|
Git hash-object command:
|
|
Git init command:
- Initiates a Git repository
- Adds a .Git folder.
- You will only find one instance of .git directory per repository. It is not repeated in sub directories similar to SVN.12$ git initInitialized empty Git repository in C:/Users/unshakeable/MyGit/.git/
Git hash-object Command with -w flag:
|
|
- MyGit>.git>objects directory stores all Git objects
- You can ignore info and pack directories
Git cat-file command with -t flag:
- Use cat-file to check the value stored in Hash-map against the SHA1 key
- “t flag” is used to display type.12$ git cat-file 23991897e13e47ed0adb91a0082c31c82fe0cbe5 -tblob
Git cat-file command with -p flag:
- “p flag” is used for pretty printing12$ git cat-file 23991897e13e47ed0adb91a0082c31c82fe0cbe5 -pApple Pie
Sample Folder/File structure:
- Create “cookbook” directory with below structure:123456789$ tree.|-- .git|-- menu.txt`-- recipes|-- README.txt`-- apple_pie.txt1 directory, 3 files
Git status command:
|
|
Git log command:
|
|
- Most commonly used:git god1$ git log --graph --oneline --decorate --all
Git shortlog command:
|
|
Git add command:
used to stage a file i.e move from Working-Area to Index-Area.
Add all new files and modified files to staging area:
1$ git add .Add new file to staging area:
1$ git add -uAdd all files to staging area:
1$ git add -AAdd all html files to staging area:
1$ git add *.html
Git rm –cached command:
Used to unstage a file i.e deletes the file from Index-Area region only.
12$ git rm --cached menu.txtrm 'menu.txt'Deleting file from both Index-Area and Working-Area. Warning will be displayed
1$ git rm <fileName>Force delete file from both Index-Area and Working-Area
1$ git rm f
Git clean
|
|
Git checkout command:
- Used to move a file from Index-Area to Working-Area.
- Changes in Working-Area will be overwritten.1$ git checkout -- menu.txt
Git Configuration:
Git configuration can be stored at 3 places. Git config is hierarchical in nature. Below is the order of hierarchy:
- System-level configuration(Rarely used): git config –system [config file is stored at C:\Program Files\Git\etc\gitconfig]
- User-level git configuration: git config –global [config file is stored at C:\Users\helloGuy.gitconfig]
- Repository-level git configuration: git config [config file is stored at C:\Users\helloGuy\Code.git\config]
Common user-level git configuration:
Add user.name to global git config:
1$ git config --global user.name "helloGuy"Add user.email to global git config:
1$ git config --global user.email "hello.world@gmail.com"Add help.autocorrect to global git config:
1$ git config --global help.autocorrect 1Add color attributes to global git config:
123$ git config --global color.ui auto$ git config --global color.status auto$ git config --global color.branch autoAdd core.autocrlf to global git config:
1$ git config --global core.autocrlf trueAdd branch.autosetuprebase to global git config:
1$ git config --global branch.autosetuprebase alwaysAdd default diff algorithm
12$ git config --global diff.algorithm histogram$ git config --global diff.algorithm patienceAdd alias for commonly used commands:
1$ git config --global alias.god "log --graph --oneline --decorate --all"Configure Beyond Compare as diff-tool:
12$ git config --global diff.tool bc3$ git config --global difftool.bc3.path "c:/Program Files/Beyond Compare 4/bcomp.exe"Configure Beyond Compare as merge-tool:
12$ git config --global merge.tool=bc3$ git config --global mergetool.bc3.path=c:/Program Files/Beyond Compare 4/bcomp.exeShow all global git config:
1234567$ git config --global --listuser.name=helloGuyuser.email=hello.world@gmail.comcore.editor=notepad++core.autocrlf=truehelp.autocorrect=1color.ui=autoRemove user.name from repository git config:
1$ git config --unset user.nameShow all repository level git config: This will list all config properties from all 3 scopes.
1$ git config --list
Git commit command:
- What is a commit?
- SHA1 to a tree
- Author name
- Commit date
- Commit message
- Parent: used for versioning
|
|
Git object model: Structure of the initial commit
|
|
Git object model: Structure of subsequent commits(versioning)
- From 2nd commit onwards, you have additional “parent” field, which points to previous commit.
|
|
Git object model: Structure of tree
- Tree object can point to files and other trees123456$ git cat-file be4d5bfce489a2591e7fed5c672f9e52cd695a43 -ttree$ git cat-file be4d5bfce489a2591e7fed5c672f9e52cd695a43 -p100644 blob 23991897e13e47ed0adb91a0082c31c82fe0cbe5 menu.txt040000 tree 3ee76fde69b730530f1682f1f51789e89cf30500 recipes
Git object model: Structure of blob
- File name is not stored in blob, but in the tree pointing to it.12345$ git cat-file 23991897e13e47ed0adb91a0082c31c82fe0cbe5 -tblob$ git cat-file 23991897e13e47ed0adb91a0082c31c82fe0cbe5 -pApple Pie
Git count-objects command:
|
|
Git Tags:
- Tags are like labels/references.
- A tag is like a branch that doesn’t move.
- Git has two type of Tags.
- Regular or lightweight tags
- Annotated tags
Annotated tags: comes with message
|
|
Lightweight/Regular tag:
|
|
Get list of Git tags:
|
|
Git object model: Structure of Tag
|
|
Stash: is useful when we have uncommitted changes and need to switch branch.
$ git stash
$ git stash apply
$ git stash list
$ git stash clear
$ git stash pop
$ git stash drop
Git branch:
- Git creates the default branch(master) on first commit.
- Current branch will be marked with asterisk.
- Branch is just a reference to a commit.
Command to list all branches:
|
|
Structure of Master branch:
- Git Master branch reference is stored in .git/refs/heads directory12$ cat .git/refs/heads/masterf2b32d8d5f262f035e0090c7b7d2161f2ff26f45
Command to create new branch:
|
|
|
|
Command to delete a branch
|
|
Head:
- HEAD is just a reference to a branch.
- So it is kind of pointer to a pointer.
|
|
How to switch to other branch: checkout
As part of checkout following two actions happens:
- HEAD reference moves to decRelease
- Content of decRelease is downloaded to Working-Area and Index-Area.12$ git checkout decReleaseSwitched to branch 'decRelease'
Git diff command:
Find difference between Working-Area and Index-Area:
1$ git diffFind difference between Index-Area and Repository-Area:
1$ git diff --cachedFind difference between two commits:
1$ git diff hash1..hash2Find difference between current commit and previous commit:
1$ git diff head~1..head
|
|
|
|
Git reset command:
It moves the head to specified location and updated area contents based on flags.
Hard reset: Sets the HEAD to corresponding location and updates both Working-Area and Index-Area.
1$ git reset --hard <branchName>Mixed reset(default): Sets the HEAD to corresponding location and updates just the Index-Area.
1$ git reset --mixed <branchName>Soft reset: Just sets the HEAD to corresponding location. Does not update content of any area.
1$ git reset --soft head~1
Gitk: git UI tool
|
|
Merge command:
You are on master branch and want to merge with decRelease branch
1$ git merge decReleaseIn case of merge conflict git will place markers in the corresponding file, which need to be removed manually in Working-Area.
- Use ADD to stage your merged files
- Use Commit to check-in the merged files. This is a special commit and it will have two PARENT tags in it.
- For complex merge conflicts its better to configure and use merger tool e.g. WinMerge, tourtoiseMerge
Fast-forward:
- Generally, after merge it make sense to do merge in other direction as well
Detached Head:
- Use git checkout command with SHA1 to move head to a particular SHA1.
- Use normal commit to create subsequent commit points and move HEAD.
- At any point you can switch to master to move HEAD back to it and abandon new commits. As no pointer is pointing at newly created SHA1 references, they will be garbaged collected
- If you want to preserve them just create a Branch
Git fsck command:
- find dangling and unreachable commits12$ git fsck --dangling --no-progress$ git fsck --unreachable --no-progress
Clone command:
- Copy entire .git folder from remote.
- Clone only copy the master branch to Working-Area.
- This operation makes a entry in git config about the remote repository.123$ git clone https://github.com/unshakeable$ git clone https://github.com/unshakeable .$ git clone https://github.com/jquery/jquery.git
Display all branches after cloning:
|
|
Git Remote command:
|
|
Git show-ref command:
|
|
Git show command:
|
|
Git push command:
- Push local commits to remote repo defined in git config123$ git pushUsername for https://github.com':Password for https://github.com':
|
|
|
|
Git fetch command:
- Fetch> Merge> Push cycle12$ git fetch$ git fetch origin
Git pull command:
- git pull is equivalent to FETCH followed by MERGE.1$ git pull
Fork:
- Remote clone