Git Handbook (🚧Continuous Update)

1. Discard the changes

git checkout -- .

2. Create a new branch

git checkout -b <branch_name>
# or 
git branch <branch_name>

3. Cache the password

git config --global credential.helper store
git config --global credential.helper cache

4. Stash the current changes

git stash

5. Recovery/Apply the stash to the current branch

git stash list

git stash apply stash@{stash_index}
# Most often we recover the stash@{0}, so we also can use below
git stash apply

6. Configure username and email locally

Sometime we need to configure a different username and email for the specific repository

git config --local user.name "oscarzhou"
git config --list

7. Delete a specific branch

git branch -d <branch_name>

8. Rebase the feature branch (standard mode)

git checkout <feature_branch_name>
git rebase <target_branch>
# i.e.
git checkout feat/xx
git rebase master

9. Rebase the branch with interactive mode

git checkout <feature_branch_name>
git commit -m "xx"
git rebase -i 

How to rebase the pushed branch

10. List log in more visualized way

git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

or

git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit HEAD origin/develop

11. Find the deleted commits

git reflog
git reset --hard <id>
git push -f origin <branch_name>

A real case is that I wiped out a collaborator’s commits in a feature branch with git rebase -i xxx by mistake. So I need to find his commits back. So the above is the solution.

12. Unset the git config

git config --unset <key>

13. Cache the credential for repository

git config credential.helper store

14. Disable the cached credentials(username/password)

git config credential.helper ""

15. Remove config key with multiple vaules

git config --global --unset-all safe.directory

16. Update the commit author

git log
git rebase -i
# update commits that needs to be amended from pick to edit
# in the interactive mode
git commit --amend --author="username <email>"
# after save changes
git rebase --continue
# repeat above two commands

17. Merge feature branch to master branch

git checkout master
git merge <feature_branch>

18. Unstage the file

git reset -- <file>

19. Clone a private repository

git clone https://username@github.com/username/repo_name

20. List refs without cloning the repository

This command can be executed outside the repository

git ls-remote <repo_url>

21. Shallow clone git repository

git clone --no-checkout --depth 1 <repo_url>
# specify a branch 
git clone --no-checkout --depth 1 <repo_url> -b <feature_branch>

22. List file tree on the current head ref

This command needs to be executed inside a repository with .git folder

git ls-tree --full-name --name-only -r HEAD

23. Get the commit sha on a specific branch

git rev-parse HEAD
# Or
git rev-parse <branch_name>

Reference link


If this post helped you to solve a problem or provided you with new insights, please upvote it and share your experience in the comments below. Your comments can help others who may be facing similar challenges. Thank you!
Buy Me A Coffee
DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !