Labor Day starts with $70+ in savings on Coursera Plus. Save 40% for 3 months.

Git Commands Cheat Sheet: Essential Commands and Examples

Written by Coursera Staff • Updated on

Use this Git commands cheat sheet to learn how core Git commands work, with examples from making your first repository to merging branches.

[Featured image] A programmer uses a Git commands cheat sheet as they work at a computer in a spacious office next to a coworker.

Key takeaways

  • Basic Git commands include git init, git add, git commit, git status, and git branch.

  • You can use the git push command to send your committed changes to a remote repository (repo) that you have access to.

  • If you have Git installed, you can see all available Git commands by running git help --all in your terminal.

To use Git as a beginner, first configure it on your machine, then use this basic Git commands cheat sheet to set up your first repository, clone existing repos, and make branches for your changes and updates. Explore more about Git, how to set it up, and some of the basic commands you need to know to start. Then, continue building your DevOps skills with the IBM DevOps and Software Engineering Professional Certificate. In just six months, you can learn to work with cloud-native applications, serverless technologies, and GitHub Actions.

Common Git commands cheat sheet

Some common Git commands you may use daily include:

  • git init: Create a new repository

  • git clone <url>: Clone an existing repository with a URL

  • git add.: Add all untracked files and changes to the repo

  • git commit: Make commits to the repository

  • git show <commit>: Show the difference between a commit and its parent

  • git diff <commit> <commit>: Show the difference between two commits

  • git diff <commit> <file>: Show the difference between files since the last commit

Note that sometimes in Git, different commands perform a similar action. For example, you can use both git switch <name> and git checkout <name> to move between branches in your repository.

Git setup and configuration

To start using Git commands, you need to set up and configure Git on your computer. To set up Git, install it on your machine using an installer or by compiling it. Depending on your operating system (OS), you will take different steps to install Git:

  • On Linux, run the command of your distribution’s package management tool: For example, Ubuntu distributions can run: apt-get install git

  • On Mac, run: xcode-select --install to prompt an install

Once you install Git on your machine, you need to configure it to work properly.

Git configuration

With Git installed, you can configure it on your machine by running git config, which will automatically read the Git configuration file downloaded during the installation. With this command, you can set your identity and email for all your Git projects. To do so, run the command:

git config --global user.name "Albert Einstein"

git config --global user.email "albert@einstein.com"

If you would like to specify a different text editor for Git to use than the default on your system, you can do so by running the command:

git config --global core.editor <your preferred editor>

With that, you are set up to create your first repository.

How to use Git as a beginner

To start using Git as a beginner, you can download it and set it up on your computer. Then create a new folder or use an existing project to practice with. Run git init to create the new repo. Run git add. (or git add --all) to add project files to the repo. You can use git status to see what files you are staging. After you make changes in your files, you can run git commit -m "initial commit" to save your new changes. These are the basic Git beginner commands.

Repository setup

You can create a new Git repository for an established project, or initialize one before you create a new project. First, navigate to your project folder using the command-line interface (CLI). The folder structure will vary depending on your operating system, but you will use the command cd, such as:

cd home/user/my_project

Once you are inside the project folder within the terminal, you can run the command:

git init

This will create a.git subdirectory in your project folder. From here, you need to tell Git which files you want to include in the repository. To do so, you run:

git add. (or git --all)

The “.” tells Git to add all files in the directory. To add all files, you can also use the commands -A or --all. You can also specify individual files. After running git add, you run:

git commit -m "Project Name Initial commit"

You’ve now created your first repository and can manage the changes to your code.

Staging and commits

Once you’ve made your initial commit, Git tracks your files so that as you make changes to your project, it recognizes them as modified. If you make changes or add a new file, you need to add your files to the staging area and make another commit. To check whether files in the repo have been modified, run git status, and it will show the status of the repository.

After you add or modify a file in the project folder, you also need to add that file to the Git repository. The command to do this, as you already know, is git add, which stages new and modified files in the next commit. If you wanted to add a “Patch_Notes” markdown file, you could follow this process:

git add Patch_Notes.md

If you use git status, you will now see that this new file is in a staging area. Git does not add this file to your repository until you run git commit again, which launches a text editor. Once you indicate which files you’d like to commit by removing the comment indicated by the # symbol, you save the text file to include the new and/or modified files. Git then creates your commit and returns information about it to you.

Branches

The first commit you make to a Git repository is often called the main branch, but it may be referred to as the master branch in older Git versions. As you develop a project, you will likely want to make branches off the main branch to test new features, fix bugs, and make improvements to your program without changing the main branch. Say you want to start testing a new feature in a branch off the main. To do so, you would use the command git branch <name of new branch>. Utilize this command as follows:

git branch features11

This command creates a new branch named “features11.” From the main (or master) branch, you can switch to this branch using:

git checkout features11

Git provides a shorthand for this: git checkout -b features11, which creates the branch with -b and switches to it at the same time.

Now you can work on your new website features while still being able to make fixes and or commits to the main branch without having to commit the features11 branch.

Some other branching commands to know:

  • git branch: Lists branches in the repo

  • git branch -d <branch name>: Deletes the named branch

Read more: 11 DevOps Tools

How do I see all commands in Git?

To see all commands in Git, you can run git help --all to see a list of all possible commands in Git without having to leave the terminal application.

Merging and rebasing

Using the example above, you’re done writing the code for features11, and you’re ready to merge it with your main. You can do so using the git merge command. To merge the branches:

git checkout main (or git checkout master)

After you switch to the main branch, you can merge features11:

git merge features11

Note that this process only merges the branches; you still need to follow the commit process above to publish the changes.

Rebasing

In Git, rebasing occurs when you need to make multiple merges at once, often to maintain a clean commit history when making a feature change, if changes have also been made to the main branch [1]. If you worked on features12 and features13 but they both diverged from the main, you can rebase them before merging to the main to keep your changes longer and easier to follow, instead of manually merging each branch. To do so, use:

git checkout features13

git rebase ma in

This will find the last common parent branch between features12 and features13. After rebasing, you switch to the main and can now merge the branches regularly.

Remote repositories

A remote repository in Git is a repo of yours stored somewhere on the internet or network, allowing you to collaborate or push changes to a project from anywhere [2]. To add a remote repository, use git remote:

git remote add <a shortname for your remote repo> <repo url>

After adding, if you run git remote, you will see origin, which is Git’s default name for the remote server. Note that if you use the git clone command to build your repository, run git remote, and it will usually, by default, have created the remote server named origin.

To download that remote repository on your local machine, you use the command git fetch <remote repo name>, and Git will build the repo on your machine. The command git fetch origin will download any updates from the remote repository, but you still need to merge them manually.

Git pull

To automatically get and merge code and data from a remote repository, you need to use the git pull command. To both fetch and merge changes from the remote main branch to your current branch, you can run:

git pull origin main

Git push

To push changes to a remote repo for sharing with others, you use the git push command. This command allows you to commit changes to any branch you’ve modified. To do so would look like:

git push origin main

Undoing changes

You can undo changes in Git, but you need to be careful because it is possible to lose work if you make a mistake or undo something and later want it back. The main command for undoing is git restore. If you accidentally committed two files, “Patch_Notesv1.1” and “Patch_Notesv2.0” but only wanted the newest Patch_Notes, v2.0, you could follow this process:

git restore --staged Patch_Notesv1.1.md

This will unstage the older Patch_Notes, and you can always use git status to see how to use git restore.

It’s worth noting that git restore can delete files in the local directory. Always check the Git documentation when running this command to ensure you no longer need any unsaved local changes.

Amending a commit

If you forgot to add a file or make a change, you can easily add it using the git commit --amend command. A situation where you forgot to include the Patch_Notes file in the commit may look like:

git commit -m "Initial commit"

git add Patch_Notes.md

git commit --amend

This adds to the last commit without adding an entirely new commit full of changes for one file.

Explore our free resources for developers

Subscribe to Career Chat on LinkedIn to get timely updates on popular skills, tools, and certifications. Then, build or refresh your coding skills with our other free resources:

With Coursera Plus, you can learn and earn credentials at your own pace from over 350 leading companies and universities. With a monthly or annual subscription, you’ll gain access to over 10,000 programs. Just check the course page to confirm your selection is included.

Article sources

1

Atlassian. “Git rebase, https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase/.” Accessed July 17, 2026.

Updated on
Written by:

Editorial Team

Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.