OSDN Git Service

Regular updates
[twpd/master.git] / git-tricks.md
1 ---
2 title: Git tricks
3 category: Git
4 layout: 2017/sheet
5 ---
6
7 ### Refs
8
9     HEAD^       # 1 commit before head
10     HEAD^^      # 2 commits before head
11     HEAD~5      # 5 commits before head
12
13 ### Branches
14
15     # create a new branch
16       git checkout -b $branchname
17       git push origin $branchname --set-upstream
18
19     # get a remote branch
20       git fetch origin
21       git checkout --track origin/$branchname
22
23     # delete local remote-tracking branches (lol)
24       git remote prune origin
25
26     # list merged branches
27       git branch -a --merged
28
29     # delete remote branch
30       git push origin :$branchname
31       
32     # go back to previous branch
33       git checkout -
34       
35 ### Collaboration
36
37     # Rebase your changes on top of the remote master
38       git pull --rebase upstream master
39       
40     # Squash multiple commits into one for a cleaner git log
41     # (on the following screen change the word pick to either 'f' or 's')
42       git rebase -i $commit_ref
43
44 Submodules
45 ----------
46
47 ### Submodules
48
49     # Import .gitmodules
50       git submodule init
51
52     # Clone missing submodules, and checkout commits
53       git submodule update --init --recursive
54
55     # Update remote URLs in .gitmodules
56     # (Use when you changed remotes in submodules)
57       git submodule sync
58
59 Diff
60 ----
61
62 ### Diff with stats
63
64     git diff --stat
65     app/a.txt    | 2 +-
66     app/b.txt    | 8 ++----
67     2 files changed, 10 insertions(+), 84 deletions(-)
68
69 ### Just filenames
70
71     git diff --summary
72
73 Log options
74 -----------
75
76 ### Options
77
78     --oneline
79       e11e9f9 Commit message here
80
81     --decorate
82       shows "(origin/master)"
83
84     --graph
85       shows graph lines
86
87     --date=relative
88       "2 hours ago"
89
90 Misc
91 ----
92
93 ### Cherry pick
94
95     git rebase 76acada^
96
97 ### Misc
98
99     # get current sha1 (?)
100       git show-ref HEAD -s
101
102     # show single commit info
103       git log -1 f5a960b5
104
105     # Go back up to root directory
106       cd "$(git rev-parse --show-top-level)"
107
108 ## Short log
109
110      $ git shortlog
111      $ git shortlog HEAD~20..    # last 20 commits
112
113      James Dean (1):
114          Commit here
115          Commit there
116
117      Frank Sinatra (5):
118          Another commit
119          This other commit
120
121 ## Bisect
122
123     git bisect start HEAD HEAD~6
124     git bisect run npm test
125     git checkout refs/bisect/bad   # this is where it screwed up
126     git bisect reset
127
128 ### Manual bisection
129
130     git bisect start
131     git bisect good   # current version is good
132
133     git checkout HEAD~8
134     npm test          # see if it's good
135     git bisect bad    # current version is bad
136
137     git bisect reset  # abort
138
139 ### Searching
140
141     git log --grep="fixes things"  # search in commit messages
142     git log -S"window.alert"       # search in code
143     git log -G"foo.*"              # search in code (regex)
144
145 ### GPG signing
146
147     git config set user.signingkey <GPG KEY ID>       # Sets GPG key to use for signing
148
149     git commit -m "Implement feature Y" --gpg-sign    # Or -S, GPG signs commit
150
151     git config set commit.gpgsign true                # Sign commits by default
152     git commit -m "Implement feature Y" --no-gpg-sign # Do not sign
153