Basics
Basic cheatsheet
Advanced Git Tutorials Overview | Atlassian Git Tutorial
Advanced git usage
Purge a file from history - 30 seconds of code
Purge a file
git pull -r
or git pull --rebase
A different strategy of combining your code with the remote repository, where your local commits, no matter how old, are placed on top of the latest remote code until you push them to the remote location. It involves only 1 branch, and not merging with another like in git fetch, git merge
git log
A history of the all the commits, with the commit hash code, author name and commit message.
git log -author="Ray"
Shows only commits of those authors whose name contains the letters 'Ray'.
git log --pretty=oneonline
Shows only the commit msgs and hashes in a single line.
git log <origin/master>..<master>
List all the commits in the local system that have not yet been pushed to the remote branch.
git stash
Takes all staged and unstaged changes of your tracked files and stores them in a temporary "first in, last out" storage. This allows you to have a clean working directory again, without the current state of the files being your last commit in the branch. Useful when you are not yet ready to commit your changes, but you either need to switch branches or pull the latest changes before you continue.
git stash list
Shows you the last of all saves in the git stash.
git stash pop
or git stash apply
Pops out the most recent save made in the stash back into your working directory. However the changes that you staged are no longer staged. To preserve the staging as well, see #7.
git stash apply --index
Preserves the staging as well, from #6.
git stash apply stash@{2}
Applies the saves from the 2nd latest point in the stash.
git stash store "$(git stash create)"
Creates a stash entry but also leaves the working directory unchanged
git checkout .
Returns all files in the working directory back to the state of the last commit. CAREFUL! All uncommitted changes get permanently discarded.
git commit --amend
This adds your staged changes to the most recent commit, instead of creating a new commit.
git diff
Used to compared changes between commits. In-depth usage
git diff HEAD^ HEAD
Difference between the latest and 2nd latest commit
git diff > filename.patch
Creates a patch of all your unstaged changes, so that they can be saved to later or given to someone else with the same repo.
git diff --cached > filename.patch
Same as #13, but for staged changes.
git apply filename.patch
Apply a patch onto your projects as unstaged changes.
git reset --soft
Undoes the last commit, but your staged and unstaged changes are still there.
git reset --hard
CAREFUL! Undoes the last commit as well as all your staged and unstaged changes. (Seems the same as git checkout .
)
git reset <commit's SHA>
Undoes all commits upto the mentioned SHA, but your staged and unstaged changes are still there.
Bringing my branch up to date with master
git checkout <mybranch>
git merge master
git rebase -i ~n
where n is number of commits
This command is used to reorder / change the commit msgs of / delete a commit / and other some other commands of the last n commits. Useful when I want to reorder some commits, so that only the oldest or youngest of the n commits can be pushed to the remote branch.
I can also use it to squash multiple commits into a single one, by replacing pick
with squash
in the interactive menu.
CAREFUL! Once this is complete, the commits get new hashes.
git push origin <commit hash>:master
Push all the commits up to and including the specified commit to the remote branch. For example, if there are 5 commits in local, and I want to push only the first 3, I would do git push origin <3rd commit hash>:master
git checkout <commit> path/to/file
Revert a single file to a previous commit.
git gc
Garbage collect. Removes commits that aren’t on any branches.
git log --pretty=oneline --graph --decorate --all
Show commits as a graph.
git commit --allow-empty -m <message>
Create an empty commit.
[Git - only push up the most recent commit to github](<https://stackoverflow.com/questions/2276686/git-only-push-up-the-most-recent-commit-to-github>)
Delete old local branches which are not present in remote:
!git branch -vv | GREP_OPTIONS= grep ': gone]' | awk '{print $1}' | xargs -I {} git branch -D {}
Adding and deleting tags:
git tag <tagname>
git push origin --tags
#Deleting a tag:
git tag -d released/aug202
git push origin :refs/tags/released/aug202
filter-branch. Modifying git history, deleting files from history, etc
e.g.: git filter-branch --tree-filter 'rm filename' HEAD
This removes the file, and also creates a new SHA. It's essentially a modified version of the original commit, which is destroyed.
Yarn lint only over changed files
git diff —name-only develop... | grep E '\\\\.js?$' | xargs yarn lint
Git name of every remote branch and author
git for-each-ref —format='%(authorname) %09 %(refname)' | grep origin
Number of commits by every author in all branches
git shortlog -s -n —all —no-merges
Number of lines changed by author in one branch
git log --author="<authorname>" --oneline --shortstat
Stats of authors in every branch in the codebase
git log --shortstat --pretty="%cE" | sed 's/\\(.*\\)@.*/\\1/' | grep -v "^$" | awk 'BEGIN { line=""; } !/^ / { if (line=="" || !match(line, $0)) {line = $0 "," line }} /^ / { print line " # " $0; line=""}' | sort | sed -E 's/# //;s/ files? changed,//;s/([0-9]+) ([0-9]+ deletion)/\\1 0 insertions\\(+\\), \\2/;s/\\(\\+\\)$/\\(\\+\\), 0 deletions\\(-\\)/;s/insertions?\\(\\+\\), //;s/ deletions?\\(-\\)//' | awk 'BEGIN {name=""; files=0; insertions=0; deletions=0;} {if ($1 != name && name != "") { print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net"; files=0; insertions=0; deletions=0; name=$1; } name=$1; files+=$2; insertions+=$3; deletions+=$4} END {print name ": " files " files changed, " insertions " insertions(+), " deletions " deletions(-), " insertions-deletions " net";}'
https://stackoverflow.com/questions/1265040/how-to-count-total-lines-changed-by-a-specific-author-in-a-git-repository
Load submodules within a project for the first time
git submodule update —init —recursive
Update submodules within a project
git submodule update —remote —merge
Note: this step fetches the latest changes from the submodule, and these changes also register as a change in the primary project as a new SHA reference. The new SHA reference needs to be committed as well.
Referencing issues on GitHub
git commit message ‘fixes xxx’
https://stackoverflow.com/questions/1687262/link-to-the-issue-number-on-github-within-a-commit-message