Git - Tips

(how to) undo git reset HEAD~ or git --commit amend

$ git reset 'HEAD@{1}'


$ git reset ORIG_HEAD

when git commit --amend is undone, hash of the last commit is restored to its original value -> changes can be safely pushed to remote repo without --force option.

(how to) fork a fork and sync it with original repo

$ git clone <MY_FORK_REPO>
$ git remote -v
$ git remote add upstream <UPSTREAM_REPO>
$ git fetch upstream
$ git merge upstream/master
$ git mergetool

(how to) remove file from Git

$ git rm --cached config/database.yml

(how to) sign GitHub commits



for example:

$ gpg --list-keys --keyid-format LONG
pub   rsa4096/ACFDF508F789A2CB 2017-10-17 [SC]

official guides


use gpg --full-generate-key command to generate keypair - not gpg --gen-key command: the latter uses default key size of 2048 bits which can’t be changed.



gpg-agent is a daemon to manage secret (private) keys independently from any protocol.

The agent is automatically started on demand by gpg, gpgsm, gpgconf, or gpg-connect-agent. Thus there is no reason to start it manually.

=> it’s not necessary to start gpg-agent manually (either via gpg-agent command or corresponding plugin provided by your plugin manager for Zsh).




(how to) use common configs in Git repo

just use hardlinks instead of symlinks - they are commited to Git repo like ordinary files.


still it’s not possible: at some point (most likely, when hardlinked file is changed in upstream) hardlink and original file no longer share the same inode so fetched changes are not reflected in original file.

(how to) show git diff output in pager

  ; ~/.gitconfig

    ; ...
+   pager = less -+X -+F

don’t use git-up gem


This project is no longer maintained, for several reasons:

  • As of Git 2.9, git pull –rebase –autostash does basically the same thing.

=> add Git alias instead:

set upstream

  1. Git - Notes

see the linked post for details on how and why it’s necessary to set upstream for your branch - git-up gem would handle it for us before but now we should do it by ourselves.

(how to) merge master into develop

$ git checkout develop
$ git merge master
$ git mergetool
$ git commit -m "merge master"
$ git push

NOTE: it’s important to create and push commit even if there are no changes when all conflicts are resolved (git status displays a message that working tree is clean).