Awesome
Git cheat sheet
Another Git cheat sheet yet :octocat:
Menu
- Configuration
- Commiting
- Branching
- Pulling
- Pushing
- Logging
- Tagging
- Reflog
- Resolve conflicts
- Aliases
- Bonus
- Resources
Configuration
User setup
git config user.name "Name"
git config user.email '<name@email.com>'
Tip: Add --global flag for global config.
Sign commits using a GPG key
List your gpg keys:
gpg --list-secret-keys --keyid-format LONG
/Users/username/.gnupg/secring.gpg
------------------------------------
sec 4096R/2AA5B24170567CE0 2019-01-01 [expires: 2020-01-01]
Tip: For a GPG key setup check out Generating a new GPG key
Add your GPG signing key in Git globally:
git config --global user.signingkey 2AA5B24170567CE0
Tip: For disable signing commits temporarily try: git config --global commit.gpgsign false
Global .gitignore file
git config --global core.excludesfile ~/.gitignore
Add remote origin
git remote add <remote_origin> <repo_url>
Remove remote origin
git remote remove <remote_origin>
Commiting
Show the working tree status
git status
Tip: Use -sb
flag to see the status in a compact way.
Stage files to commit
git add <some_files_or_directories>
Tips: Use a dot git add .
to stage all files for commit.
Commit a change
git commit <some_files_or_directories>
Tips: Add -m "Message here"
to commit with a message directly.
Change last commit message
git commit --amend
Tip: Add --author="Name <name@email>"
to override author.
Revert previous commit but keeping last changes
git reset HEAD^
Revert previous commit (no safety)
Caution: This command throws away all your uncommitted changes. For safety, you should always check that the output of git status
is clean (that is, empty) before using it. See this thread for more details.
git reset --hard HEAD
Cherry-pick a commit
Cherry pick means to choose a commit from one branch and apply it onto another. See this thread for more details.
git cherry-pick <other_branch_parent_commit> -m 1
Note: Parent number starting from 1. See this thread for more details.
Cherry-pick last commit
git cherry-pick $(git rev-parse <other_branch_name>) -m 1
Branching
Create a new branch and start it
git checkout -b <new_branch>
Checkout a remote branch
git checkout -tb <local_branch> origin/<remote_branch>
Rename a branch
git branch -m <new_branch_name>
Tip: Use -M
flag to forcing instead.
List all branches
git branch -a
List remote branches
git branch -r
Delete branches
git branch -d <branch_name_1> <branch_name_2> <branch_name_n>
Tip: Tip: Use -D
flag to forcing instead.
Merge two branches
git checkout <target_branch>
git merge <source_branch>
Tip: Add --squash
flat for a squash merging.
Pulling
Pull changes from remote server but saving uncommitted changes
This command makes this for you:
- Save your uncommitted changes locally with
git stash
. - Local changes you made will be rebased on top of the remote changes.
- Return your uncommitted changes locally again.
git pull <remote_origin> <target_branch> --rebase --autostash
Tip: Skip --rebase
if you want to merge your changes with the remote changes.
Pushing
Push changes to remote server
git push <remote_origin> <branch_name>
Logging
Show log info of last commit
git log -1 HEAD
Show logs in a fancy way
git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Tagging
Create a tag
git tag v1.0.0
Delete a tag
git tag -d v1.0.0
Reflog
Find and manage the history of all your Git stuff
git reflog
Resolve conflicts
TODO
Aliases
If you don't want to type the entire text of each of the Git commands above, you can easily set up an alias for each command and then just run it.
Example: We will create an alias last
that shows log info of the last commit.
git config --global alias.last 'log -1 HEAD'
Then just run:
git last
Bonus
- git-useful-aliases — Set of useful Git aliases.
- GitNow — Speed up your Git workflow.
Resources
- https://git-scm.com/docs
- https://github.com/nvie/gitflow
- https://github.com/jakubpawlowicz/git-cheat-sheet
- https://github.com/git-tips/tips
- https://github.com/razzius/git-tricks
Contributions
Feel free to send some Pull request or issue.
License
To the extent possible under law, Jose Quintana has waived all copyright and related or neighboring rights to this work.