Skip to Content

Basic Git Commands: A Comprehensive Cheat Sheet

Git is hands down the most popular open-source version control system available for developers and project managers to date, making it essential to learn and understand the basics for managing your projects.

In this comprehensive guide, we'll cover all of the basic Git commands so you can get a jump-start on your projects quickly and with ease.

Git Configuration

When moving around in your repositories, you'll need to tell Git who you are, something especially useful when collaborating with other developers. They need to know who you are, how to contact you, and who made the changes. These two commands tell Git your name and email address:

git config --global user.name "Josh Rowe"
git config --global user.email hello@orangeable.com

Specifying the --global flag instructs Git to set these parameters for all your repositories so you don't have to in future projects.

Keep in mind that Git will strip some characters from the values you pass in.

Create A New Local Repository

To create a new repository on your local machine, use the following command:

git init

Running this command will generate a .git subdirectory in your current working directory, containing all of the necessary Git metadata for your new repository.

Check Out an Existing Repository

The following command will clone an existing repository to your local machine, providing a direct copy from the source location that you can then make updates to and commit to, provided you have the necessary permissions:

git clone /path/to/repository

If you're cloning from a remote server, the command will look something like this:

git clone username@hostname:/path/to/repository

You'll need to ensure you have at least read-only access to the remote repository before cloning it.

Pull Changes from a Remote Repository

Fetch and merge changes from a remote repository to your local working directory:

git pull

This command will overwrite any changes you've made or alert you of any potential merging issues, so make sure you have everything saved and look out for warning messages after running this command.

Add Files to Staging

You can add individual files to your local index:

git add [filename]

You can add files from an entire directory to your local index:

git add /directory/path

Or, you can add all files within your working directory instead of listing them out manually one by one, which is useful when creating a new project with a lot of files:

git add *

Committing Changes

To commit changes made to existing files to head, use the following command:

git commit -m "List changes"

To commit any files you've added with git add and commit changes made to existing files, run this command:

git commit -a

You can also combine the -a and -m flags into a single command:

git commit -am "List changes"
Pro Tip: Keep your commit messages short and to the point, so you can easily find past commits later if you need to go back.
Note: The git commit command only commits changes to your local index, not to the remote repository. You'll also need to run a git push command, which we'll cover shortly.

Connect to a Remote Repository

If you need to connect your local working directory to a remote server, add the server with this command so you can push changes to the connected repository:

git remote add origin [server]

Push Changes to a Remote Repository

Once you've committed your changes to head on your local machine, you'll need to push them out to the remote repository. The following command will upload your changes to your currently checked-out branch.

git push

If you need to upload your changes to a branch that doesn't currently exist in your remote repository, you can specify the branch name from your local working directory so that it gets created:

git push origin [branch-name]

Branch Management

Create a new branch in your local working directory and switch to it automatically once created:

git checkout -b [branch-name]

Switch to an existing branch:

git checkout [branch-name]

List all available branches in your local working directory and display the name of the branch you're currently working in:

git branch

Delete a branch from your local working directory:

git branch -d [branch-name]

Push all branches to your remote repository:

git push --all origin

List Files Added or Changed

To list the files you've added or changed since your last commit, use the following command:

git status

Review Files, Branch Changes & Conflicts

You can review any additions, changes, or deletions of file contents between your current working file versions and the committed versions in your local index. View all changes made:

git diff

Review changes or conflicts within a single file:

git diff --base [filename]

Preview changes between two branches before merging them:

git diff [source-branch-name] [target-branch-name]

Where [source-branch-name] is the name of the branch you've made changes to and [target-branch-name] is the name of the branch you'll be merging into.

Merge Changes

Now that you've reviewed your changes, you can merge changes from an existing branch into your active branch:

git merge [branch-name]

Revert Changes

If you make a mistake or need to drop all of your local changes that haven't been committed yet, you can fetch the latest history from the remote server, replacing your local files with the latest from the remote repository:

git fetch origin
git reset --hard origin/master
Note: Running these commands will overwrite any unsaved and uncommitted changes and changes that have not yet been pushed to the remote repository. Make sure you double-check your work to prevent unwanted loss of work.

Search for Text

You can search for a string throughout your entire working directory. The following command will search for the string "foobar":

git grep "foobar"

Conclusion

This tutorial covered the basic Git commands needed to successfully manage a project. Try creating a test project on your local machine and run through each command individually.

I'd also advise you to set up a free GitHub account to test remote repository management.

Created: August 21, 2022

Comments

There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.