2019-08-04 23:43:35 -04:00
---
type: reference
---
# Useful Git commands
Here are some useful Git commands collected by the GitLab support team. You may not
2020-04-08 02:09:54 -04:00
need to use often, but they can come in handy when needed.
2019-08-04 23:43:35 -04:00
## Remotes
### Add another URL to a remote, so both remotes get updated on each push
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git remote set-url --add < remote_name > < remote_url >
```
## Staging and reverting changes
### Remove last commit and leave the changes in unstaged
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git reset --soft HEAD^
```
### Unstage a certain number of commits from HEAD
To unstage 3 commits, for example, run:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git reset HEAD^3
```
### Unstage changes to a certain file from HEAD
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
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
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git revert < commit-sha >
```
### Create a new message for last commit
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git commit --amend
```
### Add a file to the last commit
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
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
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git stash save
```
2020-01-08 22:07:56 -05:00
The default behavior of `stash` is to save, so you can also use just:
2019-08-04 23:43:35 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git stash
```
### Unstash your changes
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git stash apply
```
### Discard your stashed changes
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git stash drop
```
### Apply and drop your stashed changes
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git stash pop
```
## Refs and Log
### Use reflog to show the log of reference changes to HEAD
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git reflog
```
### Check the Git history of a file
2019-09-27 08:06:07 -04:00
The basic command to check the Git history of a file:
2019-08-04 23:43:35 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git log < file >
```
If you get this error message:
2020-05-19 23:08:04 -04:00
```plaintext
2019-08-04 23:43:35 -04:00
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:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git log -- < file >
```
### Find the tags that contain a particular SHA
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git tag --contains < sha >
```
### Check the content of each change to a file
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
gitk < file >
```
### Check the content of each change to a file, follows it past file renames
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
gitk --follow < file >
```
## Debugging
2019-09-27 08:06:07 -04:00
### Use a custom SSH key for a Git command
2019-08-04 23:43:35 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
GIT_SSH_COMMAND="ssh -i ~/.ssh/gitlabadmin" git < command >
```
### Debug cloning
With SSH:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
GIT_SSH_COMMAND="ssh -vvv" git clone < git @ url >
```
With HTTPS:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
GIT_TRACE_PACKET=1 GIT_TRACE=2 GIT_CURL_VERBOSE=1 git clone < url >
```
2019-11-28 10:06:57 -05:00
### Debugging with Git embedded traces
Git includes a complete set of [traces for debugging Git commands ](https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables#_debugging ), for example:
- `GIT_TRACE_PERFORMANCE=1` : enables tracing of performance data, showing how long each particular `git` invocation takes.
- `GIT_TRACE_SETUP=1` : enables tracing of what `git` is discovering about the repository and environment it’ s interacting with.
- `GIT_TRACE_PACKET=1` : enables packet-level tracing for network operations.
2019-08-04 23:43:35 -04:00
## Rebasing
### Rebase your branch onto master
The -i flag stands for 'interactive':
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git rebase -i master
```
### Continue the rebase if paused
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git rebase --continue
```
2019-09-27 08:06:07 -04:00
### Use `git rerere`
2019-08-04 23:43:35 -04:00
To _reuse_ recorded solutions to the same problems when repeated:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
git rerere
```
To enable `rerere` functionality:
2020-01-30 10:09:15 -05:00
```shell
2019-08-04 23:43:35 -04:00
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. -->