Skip to content

Commit 6928633

Browse files
committed
Add Git cheatsheet note
1 parent a88dcc5 commit 6928633

1 file changed

Lines changed: 243 additions & 0 deletions

File tree

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
---
2+
title: "Cheatsheet: Git"
3+
dateCreated: 2024-11-12
4+
dateUpdated: 2024-11-12
5+
---
6+
7+
This cheatsheet assumes that you're already familiar with Git concepts and know the usual `init`, `pull`, `add`, `commit`, `push` flow. It aims to document commands and flags that I tend to use but forget when I haven't used them in a while.
8+
9+
## Branching
10+
11+
### List branches
12+
13+
```console
14+
git branch
15+
```
16+
17+
### Create branch
18+
19+
```console
20+
git branch <branch>
21+
```
22+
23+
### Switch to branch
24+
25+
```console
26+
git switch <branch>
27+
```
28+
29+
### Create branch then switch to it
30+
31+
```console
32+
git switch --create <branch>
33+
```
34+
35+
Or shorthand:
36+
37+
```console
38+
git switch -c <branch>
39+
```
40+
41+
### Rename current branch
42+
43+
```console
44+
git switch --move <new-name>
45+
```
46+
47+
Or shorthand:
48+
49+
```console
50+
git switch -m <new-name>
51+
```
52+
53+
### Delete branch
54+
55+
```console
56+
git branch --delete <branch>
57+
```
58+
59+
Or shorthand:
60+
61+
```console
62+
git branch -d <branch>
63+
```
64+
65+
### Delete branch WITHOUT pushing it upstream
66+
67+
```console
68+
git branch --delete --force <branch>
69+
```
70+
71+
Or shorthand:
72+
73+
```console
74+
git branch -D <branch>
75+
```
76+
77+
## Stashing
78+
79+
### Stash changes
80+
81+
```console
82+
git stash
83+
```
84+
85+
Or to add a message to remember the stash by:
86+
87+
```console
88+
git stash --message <message>
89+
```
90+
91+
Or shorthand:
92+
93+
```console
94+
git stash -m <message>
95+
```
96+
97+
### List stashes
98+
99+
```console
100+
git stash list
101+
```
102+
103+
### Apply stash to current working tree
104+
105+
```console
106+
git stash apply [<index>]
107+
```
108+
109+
### Apply stash to current working tree THEN remove it
110+
111+
```console
112+
git stash pop [<index>]
113+
```
114+
115+
### Remove stash
116+
117+
```console
118+
git stash drop [<index>]
119+
```
120+
121+
### Remove all stashes
122+
123+
```console
124+
git stash clear
125+
```
126+
127+
## Staging
128+
129+
### Stage part of a file
130+
131+
```console
132+
git add --patch <file>
133+
```
134+
135+
Or shorthand:
136+
137+
```console
138+
git add -p <file>
139+
```
140+
141+
**Note:** `y` means stage this hunk. `n` means don't stage this hunk. `s` means split this hunk (not always possible). `e` means manually edit this hunk to add the specific lines you want to add (done when hunk is too big and `s` is not possible).
142+
143+
## Committing
144+
145+
### Undo latest commit
146+
147+
```console
148+
git reset HEAD~
149+
```
150+
151+
Or to keep uncommitted changes staged:
152+
153+
```console
154+
git reset --soft HEAD~
155+
```
156+
157+
Or to discard ALL uncommitted changes:
158+
159+
```console
160+
git reset --hard HEAD~
161+
```
162+
163+
**Note:** You can lose a lot of work when using `--hard`, so only use it if you're sure you want to nuke your working tree (yes, you can use `reflog`, but still).
164+
165+
### Change latest commit message
166+
167+
```console
168+
git commit --amend
169+
```
170+
171+
**Note:** There should be no staged changes or else those will be added to the latest commit.
172+
173+
### Change previous commit messages
174+
175+
```console
176+
git rebase --interactive <hash>~
177+
```
178+
179+
Or shorthand:
180+
181+
```console
182+
git rebase -i <hash>~
183+
```
184+
185+
Then, change `pick` to `reword` or `r` on the commits whose message you want to change.
186+
187+
### Add changes to latest commit
188+
189+
Stage new changes, then:
190+
191+
```console
192+
git commit --amend
193+
```
194+
195+
Or to NOT change the commit message:
196+
197+
```console
198+
git commit --amend --no-edit
199+
```
200+
201+
### Change previous commits
202+
203+
```console
204+
git rebase --interactive <hash>~
205+
```
206+
207+
Or shorthand:
208+
209+
```console
210+
git rebase -i <hash>~
211+
```
212+
213+
Then, change `pick` to `edit` or `e` on the commits you want to change. Once you're done with your changes, stage and commit them:
214+
215+
```console
216+
git commit --amend
217+
```
218+
219+
Or to NOT change the commit message:
220+
221+
```console
222+
git commit --amend --no-edit
223+
```
224+
225+
Then, move on to the next commit to change:
226+
227+
```console
228+
git rebase --continue
229+
```
230+
231+
Or to discard the changes you've done so far and exit the interactive rebase:
232+
233+
```console
234+
git rebase --abort
235+
```
236+
237+
## References
238+
239+
- <https://git-scm.com/docs>
240+
- <https://stackoverflow.com/q/179123>
241+
- <https://stackoverflow.com/q/927358>
242+
- <https://stackoverflow.com/q/1085162>
243+
- <https://stackoverflow.com/q/1186535>

0 commit comments

Comments
 (0)