Git is a version control system I use for all my projects.
I almost never type a full git command but instead have a variety of aliases like g
for git status
. You can view the full list on Github. Some of the more useful and unusual ones are listed below.
git log --graph --pretty=format'\'':%C(red)%h%Cgreen%d%Creset %s %C(blue) %an, %ar%Creset'\
I alias this to gl
which gives a nicer output than standard:
* ecf4ca4 Fix typo Robb Knight, 4 hours ago
* fecce5a Add copy to clipboard feature Robb Knight, 5 hours ago
* 418d17c Menu now shows and hides Robb Knight, 10 hours ago
git log --date=short --pretty=format:%ad | sort | uniq -c
Example:
1 2017-12-08
6 2017-12-26
12 2018-01-01
13 2018-01-02
10 2018-01-14
7 2018-01-17
5 2018-01-18
# limit to 5 commits
git log -n 5
git log --date=iso
git log -1 --format="%ad" -- index.html
# get updated for all files
git ls-tree -r --name-only HEAD | while read filename; do
echo "$(git log -1 --format="%ad" -- $filename) $filename"
done
git add -p
will go through each hunk of changes and allow you to add them ready for commiting.
git cherry-pick HASH
allows you to take a commit from another branch and add it to your current one. Useful when you commit to the wrong branch, or have changes you need from elsewhere.
Git stash allows you to temporarily staash changes you've made and then apply them again later.
$ git stash
# change branch, pull recent changes, etc
$ git stash apply
git checkout path/to/file.txt
git remote -v
Output:
origin git@github.com:rknightuk/awesome-repo.git (fetch)
origin git@github.com:rknightuk/awesome-repo.git (push)
e.g. Changing the origin
remote url
git remote rm origin
git remote add origin https://github.com/rknightuk/awesome-repo.git
git config --global init.defaultBranch main
git commit --amend