2019-08-04 23:43:35 -04:00
---
2020-07-29 08:09:45 -04:00
stage: Create
group: Source Code
2022-09-22 09:39:34 -04:00
info: "To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments"
2019-08-04 23:43:35 -04:00
type: reference
---
2021-02-09 13:09:59 -05:00
# Useful Git commands **(FREE)**
2019-08-04 23:43:35 -04:00
2021-01-27 16:09:12 -05:00
The GitLab support team has collected these commands to help you. You may not
2022-04-28 14:10:01 -04:00
need them frequently.
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
2022-04-28 14:10:01 -04:00
To revert changes to a file, you can use either:
2019-08-04 23:43:35 -04:00
- `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.
2021-03-17 11:09:03 -04:00
- `GIT_TRACE_SETUP=1` : enables tracing of what `git` is discovering about the repository and environment it's interacting with.
2019-11-28 10:06:57 -05:00
- `GIT_TRACE_PACKET=1` : enables packet-level tracing for network operations.
2019-08-04 23:43:35 -04:00
## Rebasing
2021-05-20 14:10:33 -04:00
### Rebase your branch onto the default
2019-08-04 23:43:35 -04:00
2021-05-20 14:10:33 -04:00
The `-i` flag stands for 'interactive'. Replace `<default-branch>` with the name
of your [default branch ](../../user/project/repository/branches/default.md ):
2019-08-04 23:43:35 -04:00
2020-01-30 10:09:15 -05:00
```shell
2021-05-20 14:10:33 -04:00
git rebase -i < default-branch >
2019-08-04 23:43:35 -04:00
```
### 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. -->