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:
1
2
3
$ git branch
$ git branch -r
* master
Structure of Master branch:
Git Master branch reference is stored in .git/refs/heads directory
1
2
$ cat .git/refs/heads/master
f2b32d8d5f262f035e0090c7b7d2161f2ff26f45
Command to create new branch:
1
2
3
4
5
6
7
8
9
10
11
$ git branch decRelease
$ git branch
decRelease
* master
$ cat .git/refs/heads/decRelease
f2b32d8d5f262f035e0090c7b7d2161f2ff26f45
$ cat .git/refs/heads/master
f2b32d8d5f262f035e0090c7b7d2161f2ff26f45
1
$ git branch --set -upstream master origin/master
Command to delete a branch
1
git branch -D <BranchName>
Head:
HEAD is just a reference to a branch.
So it is kind of pointer to a pointer.
1
2
$ cat .git/HEAD
ref: refs/heads/master
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.
1
2
$ git checkout decRelease
Switched to branch 'decRelease'
Git diff command:
Find difference between Working-Area and Index-Area:
1
$ git diff
Find difference between Index-Area and Repository-Area:
1
$ git diff --cached
Find difference between two commits:
1
$ git diff hash1..hash2
Find difference between current commit and previous commit:
1
$ git diff head~1..head
1
$ git diff head~3:Readme.txt..head:Readme.txt
1
$ git diff --histogram
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
1
$ gitk
Merge command:
You are on master branch and want to merge with decRelease branch
1
$ git merge decRelease
In 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 commits
1
2
$ 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.
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum, tenetur, vel fuga accusamus velit consectetur quibusdam eligendi maxime unde in expedita et quidem laborum at ex laboriosam minima necessitatibus dicta.</p>