summaryrefslogtreecommitdiff
path: root/doc/topics/git/useful_git_commands.md
blob: 030e62f485acef61307f2bd4bdecc837f6ded5d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
type: reference
---

# Useful Git commands

Here are some useful Git commands collected by the GitLab support team. You may not
need to use often, but they can can come in handy when needed.

## Remotes

### Add another URL to a remote, so both remotes get updated on each push

```sh
git remote set-url --add <remote_name> <remote_url>
```

## Staging and reverting changes

### Remove last commit and leave the changes in unstaged

```sh
git reset --soft HEAD^
```

### Unstage a certain number of commits from HEAD

To unstage 3 commits, for example, run:

```sh
git reset HEAD^3
```

### Unstage changes to a certain file from HEAD

```sh
git reset <filename>
```

### Revert a file to HEAD state and remove changes

There are two options to revert changes to a file:

- `git checkout <filename>`
- `git reset --hard <filename>`

### Undo a previous commit by creating a new replacement commit

```sh
git revert <commit-sha>
```

### Create a new message for last commit

```sh
git commit --amend
```

### Add a file to the last commit

```sh
git add <filename>
git commit --amend
```

Append `--no-edit` to the `commit` command if you do not want to edit the commit
message.

## Stashing

### Stash changes

```sh
git stash save
```

The default behavor of `stash` is to save, so you can also use just:

```sh
git stash
```

### Unstash your changes

```sh
git stash apply
```

### Discard your stashed changes

```sh
git stash drop
```

### Apply and drop your stashed changes

```sh
git stash pop
```

## Refs and Log

### Use reflog to show the log of reference changes to HEAD

```sh
git reflog
```

### Check the Git history of a file

The basic command to check the git history of a file:

```sh
git log <file>
```

If you get this error message:

```text
fatal: ambiguous argument <file_name>: unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
```

Use this to check the Git history of the file:

```sh
git log -- <file>
```

### Find the tags that contain a particular SHA

```sh
git tag --contains <sha>
```

### Check the content of each change to a file

```sh
gitk <file>
```

### Check the content of each change to a file, follows it past file renames

```sh
gitk --follow <file>
```

## Debugging

### Use a custom SSH key for a git command

```sh
GIT_SSH_COMMAND="ssh -i ~/.ssh/gitlabadmin" git <command>
```

### Debug cloning

With SSH:

```sh
GIT_SSH_COMMAND="ssh -vvv" git clone <git@url>
```

With HTTPS:

```sh
GIT_TRACE_PACKET=1 GIT_TRACE=2 GIT_CURL_VERBOSE=1 git clone <url>
```

## Rebasing

### Rebase your branch onto master

The -i flag stands for 'interactive':

```sh
git rebase -i master
```

### Continue the rebase if paused

```sh
git rebase --continue
```

### Use git rerere

To _reuse_ recorded solutions to the same problems when repeated:

```sh
git rerere
```

To enable `rerere` functionality:

```sh
git config --global rerere.enabled true
```

<!-- ## Troubleshooting

Include any troubleshooting steps that you can foresee. If you know beforehand what issues
one might have when setting this up, or when something is changed, or on upgrading, it's
important to describe those, too. Think of things that may go wrong and include them here.
This is important to minimize requests for support, and to avoid doc comments with
questions that you know someone might ask.

Each scenario can be a third-level heading, e.g. `### Getting error message X`.
If you have none to add when creating a doc, leave this section in place
but commented out to help encourage others to add to it in the future. -->