Basic Git Terminal Command Quick Reference

Published: 2022 July 05

Last edited: 2023 October 20

software development Git technical quick reference

Introduction

This post is part of a series about Git and GitHub.

Context

All commands are used in a terminal and assume that Git is installed.

Commands should be executed in a terminal pointed to the directory of the relevant Git repository, unless otherwise stated.

Basic commands

Note: All the commands in this section can be executed in a terminal pointed at any directory.

List basic Git commands

Command:

git

or

Command:

git --help

Check Git version

Command:

git --version

Initial setup

Note: All the commands in this section can be executed in a terminal pointed at any directory EXCEPT for two commands noted below.

Set your user name

Command form:

git config --global user.name "[user name]"

Example:

git config --global user.name "John Doe"

Set your user email

Command form:

git config --global user.email [user email]

Example:

git config --global user.email johndoe@example.com

Set your default editor

Command form:

git config --global core.editor "[command for editor]"

Example:

Sets the default editor to Visual Studio Code.

git config --global core.editor "code --wait"

Set your default difftool

To set a custom difftool, you need to set two parameters:

  1. difftool name
  2. difftool command

Command 1 form:

git config --global diff.tool [difftool name]

Command 1 Example:

Sets the default diff.tool name variable to vscode (for Visual Studio Code).

git config --global diff.tool vscode

Command 2 form:

git config --global difftool.[difftool name].cmd "[command for editor]"

Note that the [difftool name] must match the name in the previous command.

Command 2 Example:

Sets the difftool command variable to launch Visual Studio Code to display diffs.

git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
Additional resources

The video Git Tutorial for Beginners by Programming with Mosh (starting at timestamp 50:33) demonstrates setting these values.

For further information (including if you want to use one of the built-in diff.tool values) consult the Git documentation for configuring diff.tool.

Set the default branch name

Command form:

git config --global init.defaultBranch [branch name]

Example:

git config --global init.defaultBranch main

Check global Git configuration settings

Command:

git config --global --list

Displays global Git settings.

Edit global Git configuration settings

Command:

git config --global --edit

Opens your Git configuration settings file in your default text editor.

Check all Git configuration settings

Note: This command should be executed in a terminal pointed to the directory of the relevant Git repository.

Command:

git config --list

Displays all Git settings for the current repository.

Edit all Git configuration settings

Note: This command should be executed in a terminal pointed to the directory of the relevant Git repository.

Command:

git config --edit

Opens your Git configuration settings file in your default text editor.

Additional resources

See the “Getting Started - First-Time Git Setup” chapter from Pro Git for more details on Git configuration.

Establishing a Git repository

The commands in this section should be entered in a terminal pointed to the directory where you want your Git repository.

Check the status of any existing Git repository

Command:

git status

Output:

If you are in a directory that hasn’t been initialized previously, you should see the message:

fatal: not a git repository (or any of the parent directories): .git

Initialize a new Git repository

Command:

git init

The Version Control Cycle

Adding files

Add one or more files to Git’s staging area

Command form:

git add [[file 1] ... [file N]]

Example:

git add index.html

Adds index.html to the staging area.

Example:

git add index.html styles.css

Adds index.html and styles.css to the staging area.

Example:

git add index.html css/styles.css

Adds index.html and css/styles.css to the staging area. Note that you can specify files in subdirectories.

Add all modified files to Git’s staging area

Command:

git add .

Add files to Git’s staging area using wildcards

To add all modified files that match a particular pattern you can use a wildcard (*).

Command form:

git add [files or directories, using the * wildcard to replace portions of the file name]

Example:

git add *.html

Adds all files with the .html extension to the staging area.

Example:

git add article0*.html

Adds all files that begin with article0 and end with .html to the staging area.

Example:

git add app.*

Adds all files that begin with app. to the staging area.

Committing files

Commit staged files with a commit message

Command form:

git commit -m "[commit message]"

Example:

git commit -m "Update spacing"

Amend the last commit message

Command form:

git commit --amend -m "[commit message]"

Example:

git commit --amend -m "Update spacing and styling"

Checking status

Check the status of the current repository

Command:

git status

Check the log of commits in the current repository

Command:

git log

Command form:

git log -[number]

Example:

git log -5

Shows the log of only the last 5 commits.

Command:

git log --oneline

Shows the log in a condensed form to fit each commit on one line.

Check the log of commits in an expanded scope

Command:

git log --all

Shows the log in an expanded scope including other branchs and tags.

Check the log of commits in an expanded scope with graphical formatting

Command:

git log --all --decorate --oneline --graph

Shows the expanded log in a more graphical format.

Note: If the following alias is added to git configs:

git config --global alias.adog "log --all --decorate --oneline --graph"

a simpler command can be used:

git adog

This command structure and alias was found in a very helpful StackOverflow answer.

Renaming files

NOTE: Use caution when renaming files or you risk losing that file’s update history.

According to Thiago Jung Bauermann if you modify a file and rename that file in the same commit you will lose the file’s update history.

The solution, according to Roland McGrath in a post that Bauermann links to, is the following:

To preserve history, you have to carefully do a commit that only renames, and then a commit that touches the contents of the file.

So, if you’re going to rename a file:

  1. Make sure all changes to the file are committed.
  2. Rename the file with the git mv command and do not change anything else about the file.
  3. Commit the file immediately after the rename before anything else is changed.

Renaming a file

Command form:

git mv [old_filename] [new_filename]

Example:

git mv extra.css custom.css

Renames extra.css to custom.css.

Branching and Merging

Branching

See the list of branches for the current repository

Command:

git branch

Shows basic branch information.

Command:

git branch -vva

Shows additional branch information.

Create a new branch

Command form:

git branch [new branch name]

Example:

git branch feature-visualization

Switch to a different branch

Command form:

git checkout [branch name]

Example:

git checkout feature-visualization

Merging

Merge one branch into another

Changes are always merged into the active branch, so merging has two parts:

  1. Checkout the branch you wish to merge into.
  2. Merge another branch into your active branch.
1. Checkout the branch you want to merge into

Command form:

git checkout [branch name]

Example:

For this example we want to merge the changes we made on the development branch into the master branch.

git checkout master

Output:

You’ll either see the message:

Already on 'master'

or

Switched to branch 'master'

Either way, the master branch is now the active branch.

2. Merge the changes from the desired branch into the current branch

Command form:

git merge [branch name]

Example:

For this example we want to merge the changes we made on the development branch into the master branch.

git merge development

Abort an in-progress merge

If you start to merge a branch and decide you don’t want to complete the merge, you can use the following to revert it.

Command:

git merge --abort

Rebase your commit history

If you want to apply all the file changes of one branch to another without also applying the commit history of that branch you can use the rebase command.

NOTE: This is a command where it’s important to know what you’re doing, especially if you are working with remote repositories. Understand the impact of this command before using it.

Command form:

git rebase [branch]

Example:

git rebase feature-sort
Additional resources

To learn more about using the rebase command watch the video Rebase as an Alternative to Merge by Tower and read the “Git Branching - Rebasing” section from Pro Git.

Deleting branches

Delete a branch

Command form:

git branch --delete [branch]

Example:

git branch --delete development 

Stashing files

Stash your current work

If you want to save your current work without committing it you can use the stash command.

Command

git stash

And if you want to stash your current work including untracked files you can add the -u flag.

Command

git stash -u

See what work has been stashed

Command

git stash list

Apply

To apply the last stashed changes to the working area and then remove them from the stash use the following command.

Caution: This will remove files from the stash after applying them.

Command

git stash pop
Additional resources

To learn more about using the stash command watch the videos Git Stash In 5 Minutes by Colt Steele and Using the Stash by Tower and read the “Git Tools - Stashing and Cleaning” section from Pro Git .

Remote Repositories

Establishing remote-tracking repositories

Clone a Git repository

If there is an existing project that you want to work with, the git clone command allows you to download a copy of the Git repository. This will download the .git file, which includes the entire history of the project.

Navigate to the directory where you want the project on your local machine and then run the command with the URL for your project.

Command form:

git clone [URL]

Example:

git clone https://github.com/dwmkerr/hacker-laws
Additional resources

The video Cloning an Existing Repository by Tower illustrates this command.

Add a Git remote repository

If you have started a project locally and want to upload it to a remote host, the git remote add command will create a remote repository for it.

Navigate to the directory of your local Git repository and then run the command with a chosen shortname (“origin” is the convention) and the URL for the remote repository.

Command form:

git remote add [shortname] [URL]

Example:

git remote add origin https://github.com/exampleuser/exampleproject
Additional resources

The video Connecting a Remote Repository by Tower illustrates this command.

Exchanging data with remote-tracking repositories

Fetching remote repository data

Command form:

git fetch [shortname]

Example:

git fetch origin
Additional resources

The video Pulling & Fetching Changes from a Remote by Tower illustrates this command.

Git pull

The git pull command can fetch and attempt to automatically merge changes from your remote repository into your local repository.

Command:

git pull
Additional resources

The video Pulling & Fetching Changes from a Remote by Tower illustrates this command.

Git push

Pushing to a remote repository (the first time)

When you first use the push command for a given branch you have to use the longer form.

To begin, make sure the active branch is the branch that you want to share, then you can use the command.

Command form:

git push -u [shortname] [remote branch name]

Example:

git push -u origin newfeature

This will create the new branch on the remote repository, share the repository data for that branch, and establish a tracking connection with that branch on your local repository.

Additional resources

The video Pushing Changes to a Remote by Tower illustrates this command.

Pushing to a remote repository with a tracking connection (subsequent times)

If you have established a tracking connection, the next time you want to push changes from that branch to the remote repository, you can use a simpler version of the push command.

Command:

git push

This will push the latest changes from your current branch on your local repository to the tracked branch on the remote repository.

Additional resources

The video Pushing Changes to a Remote by Tower illustrates this command.

Reviewing remote repository connections

Listing the remote repositories that have tracking connections with your local repository

Command:

git remote -v

Listing the remote repositories that have tracking connections with the active branch on your local repository

Command:

git branch -vva

Listing more information about a remote repository

Command form:

git remote show [shortname]

Example:

git remote show origin

Altering remote repositories

Renaming a remote repository connection

Command form:

git remote rename [shortname-previous] [shortname-new]

Example:

For a remote called temp that you want to rename to offsite, use the following:

git remote rename temp offsite

Changing the URL of a remote repository connection

Command form:

git remote set-url [shortname] [new-URL]

Example:

To update a remote called offsite that you want to connect to https://github.com/OWNER/REPOSITORY.git, use the following:

git remote set-url offsite https://github.com/OWNER/REPOSITORY.git

Removing a remote repository connection

Command form:

git remote remove [shortname]

Example:

To delete a remote called legacy use the following:

git remote remove legacy

Additional Resources

The Visual Git command reference shows Git commands organized visually by the areas each command affects.

The Git documentation provides more detail about Git commands.

To find a curated list of many more resources for learning about Git you can review our Git and GitHub Topic post.

Copying Directory Structure With a Bash Script - With Help From ChatGPT AI

Published: 2024 January 22

Backing Up Git Config Values With a Bash Script

Published: 2023 August 25

Backing Up Visual Studio Code (VS Code) Settings and Extensions With a Bash Script

Published: 2022 November 09