Git usable commands

Delete branches
git branch -d branch-name # delete local branch
git push origin :branch-name # delete remote branch

Rename local branch
git branch -m old-branch new-branch

Join/merge last X (in example below 3) commits into one
git rebase -i HEAD~3

In editor (pick last, squash 2 earlier commits):
pick 7e168d2 commit message 3
s b3bd215 commit message 2
s 436052d commit message 1

In next editor edit final commit message:
# This is a combination of 3 commits.
# The first commit's message is:
commit message 1
# This is the 2nd commit message:
commit message 2
# This is the 3rd commit message:
commit message 3

Get branch graphs
git log --graph --oneline --all --abbrev-commit --decorate

Move last X (in example below 10) commits to new branch
git branch new-branch # create copy of master
git reset --hard HEAD~10 # reset master to 10 commits back
git checkout new-branch

Delete last commit
git reset --hard HEAD~1
git push origin HEAD --force

Checkout remote branch
git checkout --track origin/remote-branch

Count commits by contributor
git shortlog | grep -E '^[^ ]'

Pull with ‘theirs’ strategy
git pull -s recursive -X theirs

Tags
git tag -a v1.0 -m 'version 1.0' # add tag
git tag -d v1.0 # remove tag
git push origin :refs/tags/v1.0 # remove remote tag

Export
git archive --format zip --output /path/repo.zip master

Leave a Reply

Your email address will not be published. Required fields are marked *