Cheatsheet - Git

While working with Git, generally we use three sources, Git Official Docs, Bitbucket Docs and Google. Even though we refer these, it consumes lot of time to get what we want i.e. right syntax with example.
To avoid doing this every time I have enlisted few of them as cheatsheet which can be used whenever required.
A. Setup
- List Global Settings
git config --global --list
2. Set User Name and Email Id
git config --global user.name "<user_name>"
git config --global user.email "<user_email_id>"
B. Commits
- Push all changed files
git add .
git commit –m "<commit_message>"
git push origin <remote_branch_name>
2. Push a specific commit
git push origin <commit_SHA_id>:master
3. Undo local commit and keep local changes.
git reset --soft HEAD~1 (Reset last commit)
git reset --soft HEAD~2 (Reset last 2 commits)
4. Undo local commit and don’t keep local changes.
git reset --hard HEAD~1 (Reset last commit)
git reset --hard HEAD~2 (Reset last 2 commits)
C. Stash
- Stash the changes.
git stash save "<stash_message>"
2. Get list of stash.
git stash list
3. Pop last stash. It apply the top stash and removes it from stash list.
git stash pop
4. Apply a particular stash without removing it (Recommended)
Use list command (previous command) to know the stash Id which you want to retrieve. Following command will retrieve stash with ID “stash@{3}
”. Generally it follows stack index. Ex. last stash index will be 0
, second last stash index will be 1
and so on.
git stash apply "stash@{3}"
5. Delete top stash without applying.
git stash drop
6. Delete a stash which is at level 3.
git stash drop 3
7. Delete all stash.
git stash clear
D. Branches
- List all branches
git branch –a
2. Navigate between the branches.
git checkout <branch_name>
3. Create new branch from current branch and navigate to that branch. Push newly created branch to origin repo.
git checkout -b <branch_name>
git push origin <branch_name>
4. Create new branch from commit id and navigate to that branch. Push newly created branch to origin repo.
git checkout -b <branch_name> <commit_id>
git push origin <branch_name>
5. Delete a branch.
git checkout -d <branch_name>
6. Merge remote develop branch into local feature branch. When you are on feature branch, run following command:
git pull origin develop
Here source branch is origin (In this case origin refers to local feature branch) and destination branch is develop (In this case develop refers to remote develop branch)
7. Merge a branch into active(current) branch
git merge <branch_to_be_merged_in_current_branch>
8. Reset to remote branch develop
& keep your changes.
git reset origin/develop
9. Reset to remote branch develop
& keep your changes.
git reset origin/develop
10. Cleanup local branch references ( prune )
git fetch --prune
F. Forking
- Show list of remote repositories
git remote -v
2. Add fork repository (from which we want to take the pull) in remote list
git remote add upstream <url_of_upstream_repository_with_dot_git>
git remote add yuvraj <url_of_fork_repository_of_yuvraj_with_dot_git>
git remote add john <url_of_fork_repository_of_john_with_dot_git>
3. Know current remote
git remote
4. Take latest from upstream’s develop
branch into your fork’s feature branch. Run it when you are on your fork’s feature branch.
git pull upstream develop
5. Take latest from John’s feature/ABC-123
branch into your fork’s feature branch. Run it when you are on your fork’s feature branch.
git pull john feature/ABC-123
6. Create a branch on local machine from upstream repo’s dev branch and push that new branch to fork repo.
git checkout -b <new_branch_name> upstream/dev
git push origin <new_branch_name>
git branch --set-upstream-to origin/<new_branch_name>
7. Create branch in fork repo using upstream repo’s feature branch (say v31400
).
git remote add upstream <url_of_upstream_repository_with_dot_git>
git fetch upstream
git checkout -b <new_branch_name> upstream/v31400
git push origin <new_branch_name>
git branch <new_branch_name> --set-upstream-to origin/<new_branch_name>
G. Miscellaneous
git clone <repo_clone_url> --depth 1
2. Get commit id of commit which was pushed 1 year back.
git log --pretty=format:"%h" --until="1 year ago" -1