Common Git commands

You can do many Git operations directly in GitLab. However, the command line is required for advanced tasks.

Convert a local directory into a repository

You can initialize a local folder so Git tracks it as a repository.

  1. Open the terminal in the directory you’d like to convert.
  2. Run this command:

    git init

    A .git folder is created in your directory. This folder contains Git records and configuration files. You should not edit these files directly.

  3. Add the path to your remote repository so Git can upload your files into the correct project.

Add a remote

You add a “remote” to tell Git which remote repository in GitLab is tied to the specific local folder on your computer. The remote tells Git where to push or pull from.

To add a remote to your local copy:

  1. In GitLab, create a project to hold your files.
  2. Visit this project’s homepage, scroll down to Push an existing folder, and copy the command that starts with git remote add.
  3. On your computer, open the terminal in the directory you’ve initialized, paste the command you copied, and press enter:

    git remote add origin

View your remote repositories

To view your remote repositories, type:

git remote -v

The -v flag stands for verbose.

Download the latest changes in the project

To work on an up-to-date copy of the project, you pull to get all the changes made by users since the last time you cloned or pulled the project. Replace <name-of-branch> with either:

  • The name of your default branch to get the main branch code.
  • The name of the branch you are working in.
git pull <REMOTE> <name-of-branch>

When you clone a repository, REMOTE is typically origin. The remote is where the repository was cloned from, and it indicates the SSH or HTTPS URL of the repository on the remote server. <name-of-branch> is usually the name of your default branch, but it may be any existing branch. You can create additional named remotes and branches as necessary.

You can learn more on how Git manages remote repositories in the Git Remote documentation.

Add another URL to a remote

Add another URL to a remote, so both remotes get updated on each push:

git remote set-url --add <remote_name> <remote_url>

Display changes to Git references

A Git reference is a name that points to a specific commit, or to another reference. The reference HEAD is special. It usually points to a reference which points to the tip of the current working branch:

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

When a reference is changed in the local repository, Git records the change in its reference logs. You can display the contents of the reference logs if you need to find the old values of a reference. For example, you might want to display the changes to HEAD to undo a change.

To display the list of changes to HEAD:

git reflog

Check the Git history of a file

The basic command to check the Git history of a file:

git log <file>

If you get this error message:

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:

git log -- <file>

Check the content of each change to a file

gitk <file>

Delete changes

If want to undo your changes, you can use Git commands to go back to an earlier version of a repository.

Deleting changes is often an irreversible, destructive action. If possible, you should add additional commits instead of reverting old ones.

Overwrite uncommitted changes

You can use git checkout as a shortcut to discard tracked, uncommitted changes.

To discard all changes to tracked files:

git checkout .

Your changes are overwritten by the most recent commit in the branch. Untracked files are not affected.

Reset changes and commits

Do not reset a commit if you already pushed it to the remote repository.

If you stage a change with git add and then decide not to commit it, you might want to unstage the changes. To unstage a change:

  • From your repository, run git reset.

If your changes have been committed (but not pushed to the remote repository), you can reset your commits:

git reset HEAD~<number>

Here, <number> is the number of commits to undo. For example, if you want to undo only the latest commit:

git reset HEAD~1

The commit is reset and any changes remain in the local repository.

To learn more about the different ways to undo changes, see the Git Undoing Things documentation.

Merge a branch with default branch

When you are ready to add your changes to the default branch, you merge the feature branch into it:

git checkout <default-branch>
git merge <feature-branch>

In GitLab, you typically use a merge request to merge your changes, instead of using the command line.

To create a merge request from a fork to an upstream repository, see the forking workflow.

Synchronize changes in a forked repository with the upstream

To create a copy of a repository in your namespace, you fork it. Changes made to your copy of the repository are not automatically synchronized with the original. To keep the project in sync with the original project, you need to pull from the original repository.

You must create a link to the remote repository to pull changes from the original repository. It is common to call this remote repository the upstream.

You can now use the upstream as a <remote> to pull new updates from the original repository, and use the origin to push local changes and create merge requests.