cheatsheets/git-tricks.md

153 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2014-09-11 14:25:43 +00:00
title: Git tricks
2015-11-24 04:30:17 +00:00
category: Git
2013-10-14 02:36:58 +00:00
---
2013-01-08 08:37:25 +00:00
### Refs
2014-09-11 14:25:43 +00:00
HEAD^ # 1 commit before head
HEAD^^ # 2 commits before head
HEAD~5 # 5 commits before head
### Branches
2014-09-11 14:25:43 +00:00
# create a new branch
git checkout -b $branchname
git push origin $branchname --set-upstream
# get a remote branch
git fetch origin
git checkout --track origin/$branchname
# delete local remote-tracking branches (lol)
git remote prune origin
# list merged branches
git branch -a --merged
# delete remote branch
git push origin :$branchname
2017-10-13 22:36:53 +00:00
# go back to previous branch
git checkout -
### Collaboration
2017-10-13 22:36:53 +00:00
# Rebase your changes on top of the remote master
git pull --rebase upstream master
# Squash multiple commits into one for a cleaner git log
# (on the following screen change the word pick to either 'f' or 's')
git rebase -i $commit_ref
2014-09-11 14:25:43 +00:00
2015-04-23 01:37:06 +00:00
Submodules
----------
2013-01-08 08:37:25 +00:00
### Submodules
2013-01-08 08:37:25 +00:00
# Import .gitmodules
git submodule init
# Clone missing submodules, and checkout commits
git submodule update --init --recursive
# Update remote URLs in .gitmodules
# (Use when you changed remotes in submodules)
git submodule sync
2014-02-25 10:32:14 +00:00
2015-04-23 01:37:06 +00:00
Diff
----
### Diff with stats
git diff --stat
app/a.txt | 2 +-
app/b.txt | 8 ++----
2 files changed, 10 insertions(+), 84 deletions(-)
### Just filenames
git diff --summary
Log options
-----------
### Options
2015-04-23 01:37:06 +00:00
--oneline
e11e9f9 Commit message here
--decorate
shows "(origin/master)"
--graph
shows graph lines
--date=relative
"2 hours ago"
Misc
----
### Cherry pick
2014-02-25 10:32:14 +00:00
git rebase 76acada^
2014-09-11 14:25:43 +00:00
2018-11-16 19:49:59 +00:00
### Misc
2014-09-11 14:25:43 +00:00
# get current sha1 (?)
git show-ref HEAD -s
# show single commit info
git log -1 f5a960b5
# Go back up to root directory
cd "$(git rev-parse --show-top-level)"
2014-09-11 14:25:43 +00:00
## Short log
$ git shortlog
$ git shortlog HEAD~20.. # last 20 commits
James Dean (1):
Commit here
Commit there
Frank Sinatra (5):
Another commit
This other commit
2015-05-05 16:00:33 +00:00
## Bisect
2015-10-04 20:28:42 +00:00
git bisect start HEAD HEAD~6
2015-05-05 16:00:33 +00:00
git bisect run npm test
2015-10-04 20:28:42 +00:00
git checkout refs/bisect/bad # this is where it screwed up
git bisect reset
2015-05-05 16:00:33 +00:00
2015-10-04 20:28:42 +00:00
### Manual bisection
2015-05-05 16:00:33 +00:00
2015-09-17 07:22:13 +00:00
git bisect start
2015-10-04 20:28:42 +00:00
git bisect good # current version is good
2015-09-17 07:22:13 +00:00
2015-10-04 20:28:42 +00:00
git checkout HEAD~8
npm test # see if it's good
git bisect bad # current version is bad
git bisect reset # abort
2015-09-08 13:16:39 +00:00
### Searching
2015-09-08 13:16:39 +00:00
git log --grep="fixes things" # search in commit messages
git log -S"window.alert" # search in code
git log -G"foo.*" # search in code (regex)
2018-11-16 19:49:59 +00:00
### GPG signing
2018-11-16 19:49:59 +00:00
git config set user.signingkey <GPG KEY ID> # Sets GPG key to use for signing
git commit -m "Implement feature Y" --gpg-sign # Or -S, GPG signs commit
git config set commit.gpgsign true # Sign commits by default
git commit -m "Implement feature Y" --no-gpg-sign # Do not sign