Git Cheat Sheet
create a new repo
# create a repo for a new project
$ git init
$ git add . && git commit -m "It begins."
$ git create -d "My new thing" # "My new thing" is a description of a repo
→ (creates a new project on GitHub with the name of current directory)
$ git push origin master
open a github page for the current repo
git browse
open a particular PR in github page
git pr list
git pr browse <PR_NUMBER>
How to ignore a file when diffing
from: https://stackoverflow.com/questions/10415100/want-to-exclude-file-from-git-diff
You could set up a custom diff driver with a no op command and assign it to those files that should be ignored.
Create a repository specific diff driver with this command
git config diff.nodiff.command /bin/true
or for all your repos with --global
.
(If /bin/true
doesn’t exist in MacOS, alternatives would be using /usr/bin/true
or echo
).
Then, assign the new diff driver to those files you want ignored in your .git/info/attributes
file.
irrelevant.php diff=nodiff
If this state is supposed to be shared with other developers you could use .gitattributes
instead of .git/info/attributes
and share the git config
command with your peers (through a documentation file or something).
Git Log
Show the entire history of the file 1
git log --follow -p -- <file>
Show the stat of the changes between two commits(things you see when you pull)
git log COMMIT..COMMIT --stat
Show the last N commits
git log -n 5 # show last 5 commits
Show the source along with commit messages
git log -p
Git Diff
git diff ..master -- <FILE_NAME
Git Commit
How to change commit messages
git commit --amend
Commit only part of a file in Git
git add --patch/-p <filename>
y: stage this hunk for the next commit
n: do not stage this hunk for the next commit
q: quit; do not stage this hunk or any of the remaining hunks
a: stage this hunk and all later hunks in the file
d: do not stage this hunk or any of the later hunks in the file
g: select a hunk to go to
/: search for a hunk matching the given regex
j: leave this hunk undecided, see next undecided hunk
J: leave this hunk undecided, see next hunk
k: leave this hunk undecided, see previous undecided hunk
K: leave this hunk undecided, see previous hunk
s: split the current hunk into smaller hunks
e: manually edit the current hunk
?: print hunk help
Commit empty commit
git commit --allow-empty
Cancel a commit message in Vim
In a command mode,
:cq
as opposed to :wq
:cq[uit][!] Quit Vim with an error code, so that the compiler will not compile the same file again.
WARNING: All changes in files are lost! Also when the
[!] is not used. It works like ":qall!" |:qall|,
except that Vim returns a non-zero exit code.
Git Branch
Rename
git branch -m <OLD_NAME> <NEW_NAME>
Delete or remove local/remote branch
## delete local branch
git branch -d BRANCH_NAME
## remote
git push origin --delete BRANCH_NAME
Git Reset
git reset HEAD~
Git Revert
Use the following command to revert (oldest commit to revert, newest commit to revert]
(left exclusive, right inclusive boundaries)
* git revert --no-edit exclusive..inclusive
git revert --no-edit dev~5..dev
Git Edit Commits*: Break commits into multiple commits
git rebase -i COMMIT^
git reset HEAD^
git add -p
git commit
gt rebase --continue
Break a previous commit into multiple commits
https://stackoverflow.com/questions/6217156/break-a-previous-commit-into-multiple-commits
Git Grep Search
https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history
git grep <PATTERN> $(git rev-list --all)
Git Rm
When you want remove / forget a file that has been committed
git rm --cached /path/to/file
Git Clean
git clean -f # deletes files
git clean -df # deletes directory, including empty directory!
Git Search*: How to find things
https://stackoverflow.com/questions/2928584/how-to-grep-search-committed-code-in-the-git-history
Search commit message :
git log --grep=<SUBSTRING TO SEARCH IN COMMIT MESSAGE>
Search commit contents (if a REGEX is different)
git log -G REGEX -p
Search commit contents : (if a REGEX is added or not)
git log -S<regex> --pickaxe-regex -p
3rd Party Git Productivity Tools
Hub
Use hub to interact with github using CLI
GitUp
git repo visualizer
link: https://gitup.co
Git Quick Stats
link: https://github.com/arzzen/git-quick-stats