Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Last active February 3, 2021 12:35
Show Gist options
  • Save tangoabcdelta/801a3fd3169c372be4d675cfca5e7885 to your computer and use it in GitHub Desktop.
Save tangoabcdelta/801a3fd3169c372be4d675cfca5e7885 to your computer and use it in GitHub Desktop.
GIT helpers
how to determine programmatically, may be in a shell script, which branch you're on (without using git branch)
git rev-parse --abbrev-ref HEAD

more: https://bigfatsoftwareinc.wordpress.com/2021/01/25/how-to-determine-which-git-branch-youre-on/

how to retrieve the last commit message programmatically, in a shell script
git log --format=%B -n 1 <revision>
git log --format=%B -n 1 HEAD
git log --format=%B -n 1 fd613f0

more: https://bigfatsoftwareinc.wordpress.com/2021/01/27/how-to-retrieve-the-last-commit-message-programmatically-in-a-shell-script/

how to perform git commit --amend i.e. amending the previous commit without editing the commit message
git commit --amend -no-edit
how to see the recent set of changes that you made before committing
git diff
how to use git diff to see changes in files that you've staged for commit, without necessarily unstaging them
git diff --cached
use git diff to determine the diff between two git branches
git diff branchA..branchB
show the immediate last commit in the history (to determine which exact state in history we are in)
git show HEAD
show the commit history in a very simple format
git log --oneline
view the history of a file using git versioning
git whatchanged -p <filename>

This command isequivalent to git log -p <filename>

view the history of a file using git versioning (visually)
gitk [filename]
## or, if you don't want to use a gui based tool like gitk, then 
gitk --follow [filename]
view the entire history of a file using git including renames

This will show the entire history of the file including history beyond renames and with diffs for each change

git log --follow -p -- path-to-file
  • Without the --follow option, it will only show the file's history up to the point where it was renamed
  • It won't show the file's history when it was known as a different file (with a different name of course) i.e. it will be the same thing as git log -p <filename>

tagging

get all commits since the last tag
git log $(git describe --tags --abbrev=0)..HEAD --oneline

blmae

how to find out "which idiot in the team added this line of code"
git blame <filename>
git gui blame <filename>
how to find out "which idiot in the team added these lines of code", especially if the file is too big and you just want to focus on a change into a few lines or may be you just want to know who added this function defintiion
git blame <filename> -L 306,326

** Pro tip**: Most likely it was you

reset

use git to rollback to a previous commit
git reset --hard HEAD~1
use git to rollback to a previous commit without destroying the changes

Use --soft flag instead of --hard:

git reset --soft HEAD~1
use git to rollback to a previous commit (without needing to count the heads)
git reset --hard <commit id i.e. the SHA>
rollback several commits which aren't in a sequence
git revert <commit id 1> <commit id 2> <commit id 3>

Note: Note that revert will create a new commit, a reverse patch, to cancel the changes out.

rollback a range of commits
git revert HEAD~2..HEAD
rollback a range of commits (without needing to count the heads)
git revert <commit id 1>..<commit id 2>
how to keep your forked repo (on github.com) up-to-date with the original repo?

Say, you have forked a new repository on github today and you want to keep your fork up-to-date with the original?

No worries, git has you covered. You can simply add a new origin (in this case, we use the alias upstream to indicate the original) & keep rebasing against it to keep your fork up-to-date with the original one.

git remote add upstream https://github.com/simonccarter/react-conf-videos.git
git fetch upstream
git rebase upstream/master
  • git rev-parse is an ancillary plumbing command.
  • rev in plumbing commands is short for "revision"
  • it is primarily used for manipulation.
  • another common usage is to print the SHA1 hashes given a revision specifier
  • you can format the output by specifying a flag --short to print a shorter unique SHA1.
other use cases
  • --verify to verify that the specified object is a valid git object.
  • --git-dir for displaying the abs/relative path of the the .git directory.
  • Checking if you're currently within a repository using --is-inside-git-dir or within a work-tree using --is-inside-work-tree
  • Checking if the repo is a bare using --is-bare-repository
  • Printing SHA1 hashes of branches (--branches), tags (--tags) and the refs can also be filtered based on the remote (using --remote)
  • --parse-opt to normalize arguments in a script (kind of similar to getopt) and print an output string that can be used with eval
  • Massage implies that it is possible to convert the info from one form into another i.e. a transformation command.
examples:
  • a branch or tag name into the commit's SHA1 it is pointing to so that it can be passed to a plumbing command which only accepts SHA1 values for the commit.
  • a revision range A..B for git log or git diff into the equivalent arguments for the underlying plumbing command as B ^A

How to generate change logs using git:

Execute the following command: git-release-notes $(git describe --tags --abbrev=0 $(git describe --tags --abbrev=0)^)..$(git describe --tags --abbrev=0) scripts/changelog.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment