Skip to content

Instantly share code, notes, and snippets.

@niderhoff
Last active January 25, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niderhoff/4aee58245e4d02fc78f2d8d608d2cd4a to your computer and use it in GitHub Desktop.
Save niderhoff/4aee58245e4d02fc78f2d8d608d2cd4a to your computer and use it in GitHub Desktop.
Handy git commands

Handy git cmds

checkout earlier commit for a specific file only

this will delete your local version!!

go back to commit x (=c5f567)

git checkout c5f567 -- file/1/to/restore file/2/to/restore

go back to commit x-1

git checkout c5f567~1 -- file/1/to/restore file/2/to/restore

note the -- indicating a destructive overwrite of the working copy

Delete local copies of remotely deleted branches

maybe update your branches first: `git fetch

  1. git remote prune prunes local tracking branches which have no remote counterpart.

  2. git branch --merged list all branches that have been merged into the current branch

  3. now pick all except master/develop and do this for all of them:

git branch -d <branch> deletes a branch

alternative: git fetch -p might be better since it also updates all the references. then you can git branch -vv and all branches which have ": gone" besides them can safely be deleted.

Better git log

add to ~/.gitconfig

[alias]
adog = log --all --decorate --oneline --graph
lg = !"git lg1"
lg1 = !"git lg1-specific --all"
lg2 = !"git lg2-specific --all"
lg3 = !"git lg3-specific --all"
lg1-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'
lg2-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
lg3-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) %C(bold cyan)(committed: %cD)%C(reset) %C(auto)%d%C(reset)%n''          %C(white)%s%C(reset)%n''          %C(dim white)- %an <%ae> %C(reset) %C(dim white)(committer: %cn <%ce>)%C(reset)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment