--- 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 ``` ## 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 ``` ### Revert a file to HEAD state and remove changes There are two options to revert changes to a file: - `git checkout ` - `git reset --hard ` ### Undo a previous commit by creating a new replacement commit ```sh git revert ``` ### Create a new message for last commit ```sh git commit --amend ``` ### Add a file to the last commit ```sh git add 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 ``` If you get this error message: ```text fatal: ambiguous argument : 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 -- ``` ### Find the tags that contain a particular SHA ```sh git tag --contains ``` ### Check the content of each change to a file ```sh gitk ``` ### Check the content of each change to a file, follows it past file renames ```sh gitk --follow ``` ## Debugging ### Use a custom SSH key for a Git command ```sh GIT_SSH_COMMAND="ssh -i ~/.ssh/gitlabadmin" git ``` ### Debug cloning With SSH: ```sh GIT_SSH_COMMAND="ssh -vvv" git clone ``` With HTTPS: ```sh GIT_TRACE_PACKET=1 GIT_TRACE=2 GIT_CURL_VERBOSE=1 git clone ``` ## 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 ```