File: commands.md

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (247 lines) | stat: -rw-r--r-- 5,965 bytes parent folder | download
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---
stage: Create
group: Source Code
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---

# Common Git commands

Learn more about the most commonly used Git commands.

## `git add`

Use `git add` to files to the staging area.

```shell
git add <file_path>
```

You can recursively stage changes from the current working directory with `git add .`, or stage all changes in the Git
repository with `git add --all`.

For more information, see [Add files to your branch](add_files.md).

## `git blame`

Use `git blame` to report which users changed which parts of a file.

```shell
git blame <file_name>
```

You can use `git blame -L <line_start>, <line_end>` to check a specific range of lines.

For more information, see [Git file blame](../../user/project/repository/files/git_blame.md).

### Example

To check which user most recently modified line five of `example.txt`:

```shell
$ git blame -L 5, 5 example.txt
123abc (Zhang Wei 2021-07-04 12:23:04 +0000 5)
```

## `git bisect`

Use `git bisect`to use binary search to find the commit that introduced a bug.

Start by identifying a commit that is "bad" (contains the bug) and a commit that is "good" (doesn't contain the bug).

```shell
git bisect start
git bisect bad                 # Current version is bad
git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be good
```

`git bisect` then picks a commit in between the two points and asks you identify if the commit is "good" or "bad" with
`git bisect good`or `git bisect bad`. Repeat the process until the commit is found.

## `git checkout`

Use `git checkout` to switch to a specific branch.

```shell
git checkout <branch_name>
```

To create a new branch and switch to it, use `git checkout -b <branch_name>`.

For more information, see [Create a Git branch for your changes](branch.md).

## `git clone`

Use `git clone` to copy an existing Git repository.

```shell
git clone <repository>
```

For more information, see [Clone a Git repository to your local computer](clone.md).

## `git commit`

Use `git commit` to commits staged changes to the repository.

```shell
git commit -m "<commit_message>"
```

If the commit message contains a blank line, the first line becomes the commit subject while the remainder becomes the
commit body. Use the subject to briefly summarize a change, and the commit body to provide additional details.

For more information, see [Stage, commit, and push changes](commit.md).

## `git commit --amend`

Use `git commit --amend` to modify the most recent commit.

```shell
git commit --amend
```

## `git diff`

Use `git diff` to view the differences between your local unstaged changes and the latest version that you cloned or
pulled.

```shell
git diff
```

You can display the difference (or diff) between your local changes and the most recent version of a branch. View a
diff to understand your local changes before you commit them to the branch.

To compare your changes against a specific branch, run:

```shell
git diff <branch>
```

In the output:

- Lines with additions begin with a plus (`+`) and are displayed in green.
- Lines with removals or changes begin with a minus (`-`) and are displayed in red.

## `git init`

Use `git init` to initialize a directory so Git tracks it as a repository.

```shell
git init
```

A `.git` file with configuration and log files is added to the directory. You shouldn't edit the `.git` file directly.

The default branch is set to `main`. You can change the name of the default branch with `git branch -m <branch_name>`,
or initialize with `git init -b <branch_name>`.

## `git pull`

Use `git pull` to get all the changes made by users after the last time you cloned or pulled the project.

```shell
git pull <optional_remote> <branch_name>
```

## `git push`

Use `git push` to update remote refs.

```shell
git push
```

For more information, see [Stage, commit, and push changes](commit.md).

## `git reflog`

Use `git reflog` to display a list of changes to the Git reference logs.

```shell
git reflog
```

By default, `git reflog` shows a list of changes to `HEAD`.

For more information, see [Undo changes](undo.md).

## `git remote add`

Use `git remote add` to tell Git which remote repository in GitLab is linked to a local directory.

```shell
git remote add <remote_name> <repository_url>
```

When you clone a repository, by default the source repository is associated with the remote name `origin`.

For more information on configuring additional remotes, see [Forks](../../user/project/repository/forking_workflow.md).

## `git log`

Use `git log` to display a list of commits in chronological order.

```shell
git log
```

## `git show`

Use `git show` to show information about an object in Git.

### Example

To see what commit `HEAD` points to:

```shell
$ git show HEAD
commit ab123c (HEAD -> main, origin/main, origin/HEAD)
```

## `git merge`

Use `git merge` to combine the changes from one branch with another.

For more information on an alternative to `git merge`, see [Rebase to address merge conflicts](git_rebase.md).

### Example

To apply the changes in `feature_branch` to the `target_branch`:

```shell
git checkout target_branch
git merge feature_branch
```

## `git rebase`

Use `git rebase` to rewrite the commit history of a branch.

```shell
git rebase <branch_name>
```

You can use `git rebase` to [resolve merge conflicts](git_rebase.md).

In most cases, you want to rebase against the default branch.

## `git reset`

Use `git reset` to undo a commit and rewind the commit history and continue on from an earlier commit.

```shell
git reset
```

For more information, see [Undo changes](undo.md).

## `git status`

Use `git status` to show the status of the working directory and staged files.

```shell
git status
```

When you add, change, or delete files, Git can show you the changes.