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/
git log --format=%B -n 1 <revision>
git log --format=%B -n 1 HEAD
git log --format=%B -n 1 fd613f0
how to perform git commit --amend
i.e. amending the previous commit without editing the commit message
git commit --amend -no-edit
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
git diff branchA..branchB
git show HEAD
git log --oneline
git whatchanged -p <filename>
This command isequivalent to git log -p <filename>
gitk [filename]
## or, if you don't want to use a gui based tool like gitk, then
gitk --follow [filename]
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>
git log $(git describe --tags --abbrev=0)..HEAD --oneline
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
git reset --hard HEAD~1
Use --soft
flag instead of --hard
:
git reset --soft HEAD~1
git reset --hard <commit id i.e. the SHA>
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.
git revert HEAD~2..HEAD
git revert <commit id 1>..<commit id 2>
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