Git commands: A comprehensive cheat sheet (with examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a popular version control system that helps to track changes in your code throughout the development process. Whether you’re new to programming or an experienced developer, knowing how to use Git is an essential skill. This comprehensive cheat sheet will take you through the basic to advanced Git commands with examples and expected outputs, helping you navigate and use Git more effectively.

Setting Up and Configuration

Before you dive into using Git, you first need to configure it. Setting up your user name and email address is essential as it’s used in every Git commit. Here’s how you do it:

git config --global user.name 'Your Name'
git config --global user.email '[email protected]'

To check your configuration, use:

git config --list

Initializing a Repository and First Commit

To start using Git, you need to create a new repository or clone an existing one. Here’s how to initialize a new Git repository:

git init

After you have a repository, add a file and commit it:

git add <filename>
git commit -m 'Initial commit message'

Checking the Status and Log

You can check the status of your repository and see which files have changes:

git status

To view the commit history, use:

git log

Branching and Merging

Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository. To create a branch:

git branch <branch-name>

To switch between branches:

git checkout <branch-name>

Merging brings the changes from one branch into another:

git merge <branch-name>

Stashing and Cleaning

Sometimes, you need to save your changes without committing them and move to a different branch. The stash command helps with this:

git stash
git stash apply

To clean your working directory by removing untracked files, use:

git clean -f

Remote Repositories

Working with remote repositories enables collaboration with others. To add a remote repository:

git remote add <remote-name> <remote-url>

Fetching the changes from remote without merging:

git fetch <remote-name>

To pull the changes and directly merge:

git pull <remote-name> <branch-name>

Pushing your changes to remote:

git push <remote-name> <branch-name>

Undoing Changes

If you make a mistake, Git has several features to help you undo your changes. The most common are ‘git revert’ and ‘git reset’:

git revert <commit-hash>
git reset --hard <commit-hash>

‘git revert’ creates a new commit that undoes the changes, while ‘git reset –hard’ will discard changes up to the specified commit.

Advanced Git Commands

As you become more comfortable with the basics, there are advanced Git features you can leverage:

To rebase your feature branch onto the tip of the main branch:

git rebase main

Using interactive rebase to edit, remove, or squash commits:

git rebase -i HEAD~<number-of-commits>

Cherry-picking a commit to apply it to a different branch:

git cherry-pick <commit-hash>

Using tags to mark a significant point in your repository’s history, such as a release:

git tag <tag-name>

Creating and applying Git patches:

git format-patch -1 <commit-hash>
git am <patch-file>

Conclusion

Understanding and mastering Git commands is invaluable for any software development and version control process. Through this cheat sheet, you should now have a clear roadmap for the most essential Git commands with examples to guide you. Continue to practice these commands, and you’ll be able to manage your repositories with ease and confidence.