Skip to main content

Git Quick Reference

Here are some useful commands for working with Git, a powerful and popular version control system that allows us to manage our codebase efficiently.

Useful Git Commands

Clone a Git repository

git clone <repo-url>

This commands allows us to 'download' (retrieve) a Git repository online via URL. It might be the most widely used Git command.

Initialise a Git repository

git init <repo-name>

If you have already created your project folder but haven't set up Git within it, just run:

git init .

Set up a remote repository

After initialising a Git repository for your project, you still can't find it on repository platforms online such as GitHub, GitLab and Codeberg. You need to set up a corresponding remote repository first.

Before running the following command, please make sure that you've created a new repository online through your preferred repository hosting platform so that you can obtain its remote repository URL.

git remote add origin <remote-url>

If you want to change the remote URL, e.g. when you want to migrate to another hosting platform, use the following command:

git remote set-url <old-remote-url> <new-remote-url>
note

The supplied URL for <remote-url> should always end with the extension .git, i.e. if your repository URL is initially https://github.com/OWNER/REPOSITORY, <remote-url> should look like https://github.com/OWNER/REPOSITORY.git.

Check the status of a Git repository

git status

This command allows us to check the current state of your working Git repository (which branch you are in, what files/folders are modified or untracked and so on).

Fetch the latest changes from the remote

git pull

Push the local changes to the remote

git push

Stage and commit changes

  • To stage files or folders that are ready for commit:
git add <file-directory>
  • To unstage files or folders:
git restore <file-directory>
  • Git diff:

    • To check for changes made locally but not yet staged for commit:
    git diff
    • To compare changes staged for commit to the committed version:
    git diff --staged

    If you only want to compare changes for a specific file or folder, supply <file-directory> argument at the end of the commands above.

  • To commit changes, i.e. to add the change into the history record of the repository:

git commit -m "<commit-message>"

Remember to supply a helpful note to each commit you make.

Branching

Git branches form a core functionality of Git. They allow us to generate different versions of the same repository so that multiple parties can work with different parts of the codes simultaneously, or multiple versions can be saved concurrently. Imagine this as creating multiple parallel universes.

Here are some important commands in working with branches.

  • To look at the list of branches in the repository:
git branch -l
  • To change to another existing branch:
git checkout <existing-branch-name>
  • To create a new branch without switching to it:
git branch <new-branch-name>
  • To create and switch to a new branch:
git checkout -b <new-branch-name>
  • To delete a branch:
git branch -d <existing-branch-name>

Merging

Move to the branch to keep first, then run the following command with the name of the branch to merge supplied:

git merge <branch-to-merge-name>

Note that the branch to merge is not automatically deleted after this. You may wish to delete that branch if needed.

Advanced Git operations

Stashing

Stashing allows us to switch to another branch of our repository halfway through making changes to the current branch by saving the changes somewhere without actually committing it.

Let's say you've made changes to branch alpha and you want to switch to branch beta.

Making sure you're still on branch alpha, to stash the changes made, run the following command:

git stash

You can then switch to branch beta without worry. Once you're done and switch back to branch alpha, to retrieve the changes made, run the following command:

git stash pop

If you decide not to keep the changes, run the following command:

git stash drop

Submodules

Git submodules allow us to embed another Git repository into a larger repository whilst managing them separately as if they're unrelated to each other. Submodules are useful when, say, you want to apply a theme file repo to your existing website repo.

After navigating to the directory within your repository in which you want to add a submodule, run the following command:

git submodule add <submodule-repo-url>

You'll find that the contents of the submodule repo have been added into the directory. A new .gitmodules file is also generated, which looks like this:

[submodule "<submodule-name>"]
path = <submodule-repo-name>
url = <submodule-repo-url>

If multiple submodules are added, the .gitmodules file may look like this:

[submodule "<submodule-1-name>"]
path = <submodule-repo-1-name>
url = <submodule-repo-1-url>

[submodule "<submodule-2-name>"]
path = <submodule-repo-2-name>
url = <submodule-repo-2-url>

Note that when making changes inside the submodule, it is treated as a separate repo so the commit records are not connected to the larger repo that includes it. To push the changes of the submodule, you need to first commit and push them within the submodule, then after navigating to the home directory of the repo, stage the entire submodule itself then run git push again as usual.

Back to cloning projects. Note that if there are submodules included within a repo, the usual git clone command will not retrieve the contents of the submodules for us. To obtain the submodules, add the --recurse-submodules option to the command:

git clone --recurse-submodules <repo-url>

To remove a submodule, run the following command, then commit the changes (from the repository it is contained in):

git rm <path-to-submodule>

You may refer to this Stack Overflow thread or the official documentation for more details, particularly the deinit command.

Managing Large Files: Git LFS

The git push command for the normal version of Git does not perform well when handling with binaries, especially when their file sizes are large. Common examples include images, audio, video and PSD files. In this case, it is advisable to use Git Large File Storage (LFS) to manage them instead.

The following introduction is extracted from the homepage of the official website, where the installation steps and basic usage instructions can also be found.

Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.

More details can be found on its documentation site.

tip

You can also find PDF versions of Git cheat sheets published by GitHub and GitLab here.

note

Of course, the functionalities of Git are way more powerful and versatile than just these. Covering all of them here defeats the purpose of this being a quick reference. To delve deeper into Git, you may refer to the official Git documentation.

Sidenote: Installing Git

Although there are graphic-based user interfaces of Git such as GitHub Desktop available, I personally recommend using a command-line interface (CLI) because it's easier and faster to issue and distribute text commands than navigate through graphical interfaces. Here are some tips in installing a CLI for Git.

Unix systems (MacOS and Linux)

Git should come pre-installed in most popular Linux distributions, such as Ubuntu (and its derivatives like Pop!_OS, which I'm using), Fedora and KDE. If it's not installed in your system, follow the instructions outlined by the official documentation.

As for MacOS, the most straightforward way is by installing through Homebrew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Skip this if Homebrew is already installed.
brew install git

Windows

The installation step of Git for Windows is the same as most other Windows programs: download the installer and follow the instructions. This should give you a separate Bash (command-line) interface for Git.

Visual Studio Code

VS Code has built-in support and integration for Git. You may refer to this page for more details.

tip

For a more comprehensive installation guide, you may refer to the official Git page.