Further Reading

Git Handbook

Basics

GitHub Git Cheat Sheet

Basic cheatsheet

Advanced Git Tutorials Overview | Atlassian Git Tutorial

Advanced git usage

Purge a file from history - 30 seconds of code

Purge a file

  1. 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

  2. git log

    A history of the all the commits, with the commit hash code, author name and commit message.

  3. git log -author="Ray"

    Shows only commits of those authors whose name contains the letters 'Ray'.

  4. git log --pretty=oneonline Shows only the commit msgs and hashes in a single line.

  5. git log <origin/master>..<master> List all the commits in the local system that have not yet been pushed to the remote branch.

  6. 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.

  7. git stash list

    Shows you the last of all saves in the git stash.

  8. 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.

  9. git stash apply --index

    Preserves the staging as well, from #6.

  10. git stash apply stash@{2}

    Applies the saves from the 2nd latest point in the stash.

  11. git stash store "$(git stash create)" Creates a stash entry but also leaves the working directory unchanged

  12. git checkout .

    Returns all files in the working directory back to the state of the last commit. CAREFUL! All uncommitted changes get permanently discarded.

  13. git commit --amend

    This adds your staged changes to the most recent commit, instead of creating a new commit.

  14. git diff

    Used to compared changes between commits. In-depth usage

  15. git diff HEAD^ HEAD Difference between the latest and 2nd latest commit

  16. 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.

  17. git diff --cached > filename.patch

    Same as #13, but for staged changes.

  18. git apply filename.patch

    Apply a patch onto your projects as unstaged changes.

  19. git reset --soft

    Undoes the last commit, but your staged and unstaged changes are still there.

  20. git reset --hard

    CAREFUL! Undoes the last commit as well as all your staged and unstaged changes. (Seems the same as git checkout . )

  21. git reset <commit's SHA> Undoes all commits upto the mentioned SHA, but your staged and unstaged changes are still there.

  22. Bringing my branch up to date with master

    git checkout <mybranch>
    git merge master
    
  23. 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.

  24. 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

  25. git checkout <commit> path/to/file Revert a single file to a previous commit.

  26. git gc Garbage collect. Removes commits that aren’t on any branches.

  27. git log --pretty=oneline --graph --decorate --all Show commits as a graph.

  28. git commit --allow-empty -m <message> Create an empty commit.

Advanced Multi-Step Tips:

[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>)
  1. 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 {}
    
  2. 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
    
  3. 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.

    Copy of filter-branch article

  4. Yarn lint only over changed files git diff —name-only develop... | grep E '\\\\.js?$' | xargs yarn lint

  5. Git name of every remote branch and author git for-each-ref —format='%(authorname) %09 %(refname)' | grep origin

  6. Number of commits by every author in all branches git shortlog -s -n —all —no-merges

  7. Number of lines changed by author in one branch git log --author="<authorname>" --oneline --shortstat

  8. 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

  9. Load submodules within a project for the first time git submodule update —init —recursive

  10. 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.