debian-mirror-gitlab/doc/topics/git/useful_git_commands.md

223 lines
4.4 KiB
Markdown
Raw Normal View History

2019-10-12 21:52:04 +05:30
---
2020-10-24 23:57:45 +05:30
stage: Create
group: Source Code
2021-02-22 17:27:13 +05:30
info: "To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments"
2019-10-12 21:52:04 +05:30
type: reference
---
2021-03-11 19:13:27 +05:30
# Useful Git commands **(FREE)**
2019-10-12 21:52:04 +05:30
2021-03-11 19:13:27 +05:30
The GitLab support team has collected these commands to help you. You may not
need to use them often.
2019-10-12 21:52:04 +05:30
## Remotes
### Add another URL to a remote, so both remotes get updated on each push
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git remote set-url --add <remote_name> <remote_url>
```
## Staging and reverting changes
### Remove last commit and leave the changes in unstaged
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git reset --soft HEAD^
```
### Unstage a certain number of commits from HEAD
To unstage 3 commits, for example, run:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git reset HEAD^3
```
### Unstage changes to a certain file from HEAD
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
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-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git revert <commit-sha>
```
### Create a new message for last commit
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git commit --amend
```
### Add a file to the last commit
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
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-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash save
```
2020-03-13 15:44:24 +05:30
The default behavior of `stash` is to save, so you can also use just:
2019-10-12 21:52:04 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash
```
### Unstash your changes
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash apply
```
### Discard your stashed changes
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash drop
```
### Apply and drop your stashed changes
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash pop
```
## Refs and Log
### Use reflog to show the log of reference changes to HEAD
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git reflog
```
### Check the Git history of a file
2019-12-21 20:55:43 +05:30
The basic command to check the Git history of a file:
2019-10-12 21:52:04 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git log <file>
```
If you get this error message:
2020-05-24 23:13:21 +05:30
```plaintext
2019-10-12 21:52:04 +05:30
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-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git log -- <file>
```
### Find the tags that contain a particular SHA
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git tag --contains <sha>
```
### Check the content of each change to a file
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
gitk <file>
```
### Check the content of each change to a file, follows it past file renames
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
gitk --follow <file>
```
## Debugging
2019-12-21 20:55:43 +05:30
### Use a custom SSH key for a Git command
2019-10-12 21:52:04 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
GIT_SSH_COMMAND="ssh -i ~/.ssh/gitlabadmin" git <command>
```
### Debug cloning
With SSH:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
GIT_SSH_COMMAND="ssh -vvv" git clone <git@url>
```
With HTTPS:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
GIT_TRACE_PACKET=1 GIT_TRACE=2 GIT_CURL_VERBOSE=1 git clone <url>
```
2020-01-01 13:55:28 +05:30
### 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-04-29 21:17:54 +05:30
- `GIT_TRACE_SETUP=1`: enables tracing of what `git` is discovering about the repository and environment it's interacting with.
2020-01-01 13:55:28 +05:30
- `GIT_TRACE_PACKET=1`: enables packet-level tracing for network operations.
2019-10-12 21:52:04 +05:30
## Rebasing
2021-09-04 01:27:46 +05:30
### Rebase your branch onto the default
2019-10-12 21:52:04 +05:30
2021-09-04 01:27:46 +05:30
The `-i` flag stands for 'interactive'. Replace `<default-branch>` with the name
of your [default branch](../../user/project/repository/branches/default.md):
2019-10-12 21:52:04 +05:30
2020-03-13 15:44:24 +05:30
```shell
2021-09-04 01:27:46 +05:30
git rebase -i <default-branch>
2019-10-12 21:52:04 +05:30
```
### Continue the rebase if paused
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git rebase --continue
```
2019-12-21 20:55:43 +05:30
### Use `git rerere`
2019-10-12 21:52:04 +05:30
To _reuse_ recorded solutions to the same problems when repeated:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git rerere
```
To enable `rerere` functionality:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
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. -->