GIT cheat sheet
Table of Contents generated with DocToc
Commits, reverts etc
Revert committed changes to a single file
$ git checkout <revision number to revert to> <file path>
$ git commit
Revert merge commit
git revert -m 1 [sha_of_merge_commit]
Correct commit message that hasn’t been pushed yet
git commit --amend
# Ref: https://help.github.com/articles/changing-a-commit-message/
# Amend author info associated with commit
git commit --amend --reset-author
Rewrite history after pushing to remote
git rebase -i HEAD~n
git push --force
Ref:
https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History
https://help.github.com/articles/changing-a-commit-message/
Force pull from remote
git fetch --all
git reset --hard origin/master
git pull origin master
Git pull allowing unrelated histories
git pull --allow-unrelated-histories
Signing commits
Check git knows about your GPG signing key
git config --global user.signingkey
Configure project to sign all commits
git config commit.gpgsign true
Configure git user to match that associated with GPG key
git config user.email 864639+anuragkapur@users.noreply.github.com
Branches
Delete remote branch
$ git push origin --delete <branch-name>
Rename local and remote branch
git branch -m old_branch_name new_branch_name
git push origin :old_branch_name
git push --set-upstream origin new_branch_name
Default branch name config
# Needs git version >=2.28
git config --global init.defaultBranch main
Tags
Create tag
git tag v1.7.1
Push tag to remote
git push origin v2.44.0
Delete remote tag
git push --delete origin v1.7.1
Delete local tag
git tag --delete v1.7.1
Forks
Update fork from origin
$ git remote add upstream <remote-repo-url>
$ git fetch upstream
$ git checkout master
$ git pull
$ git merge upstream/master
Force update fork from upstream
git reset --hard upstream/master
git push --force
Git submodules
Init and update submodules
$ git submodule init
$ git submodule update