OSDN Git Service

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