DEV Community

Cover image for The Essential Git Cheat Sheet: Master Version Control Like a Pro
Travis Ramos
Travis Ramos

Posted on • Originally published at travislramos.com

The Essential Git Cheat Sheet: Master Version Control Like a Pro

Git is an indispensable tool for developers, enabling you to track changes, collaborate with others, and manage your codebase efficiently. This cheat sheet covers essential Git commands that every developer should know, along with some advanced commands for more complex tasks.

1. Setting Up Your Git Repository

Initialize a New Repository

git init
Enter fullscreen mode Exit fullscreen mode

This command initializes a new Git repository in your project folder. Use it when starting a new project or to begin tracking an existing one.

Clone an Existing Repository

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

Clone an existing repository from a remote source like GitHub or Bitbucket. This is useful for contributing to a project or using an open-source library.

2. Configuring Git

Set Your Username

git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode

Sets your username for all Git repositories on your system.

Set Your Email

git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Sets your email address for commits.

View Your Configuration

git config --list
Enter fullscreen mode Exit fullscreen mode

Lists all the Git configurations currently set.

3. Working with Your Repository

Checking the Status of Your Repository

git status
Enter fullscreen mode Exit fullscreen mode

Displays the state of your working directory and staging area, showing which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

Viewing a Log of Commits

git log
Enter fullscreen mode Exit fullscreen mode

Shows a detailed history of commits, including author, date, and commit message.

Adding Changes to the Staging Area

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

Stages specific files for a commit. To stage all changes, use:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Commits the staged changes with a descriptive message. It’s good practice to write meaningful commit messages.

Commit All Changes Directly

git commit -am "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Stages and commits all tracked files in one step. This command does not include untracked files.

Viewing Changes

git diff
Enter fullscreen mode Exit fullscreen mode

Shows the changes between your working directory and the staging area. Useful for reviewing what you've modified before committing.

Viewing a Specific File’s Changes

git diff <file-name>
Enter fullscreen mode Exit fullscreen mode

Displays the changes made to a specific file.

Removing Files

git rm <file-name>
Enter fullscreen mode Exit fullscreen mode

Removes a file from your working directory and stages the deletion.

Moving/Renaming Files

git mv <old-file-name> <new-file-name>
Enter fullscreen mode Exit fullscreen mode

Moves or renames a file and stages the change.

4. Branching and Merging

Create a New Branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates a new branch. Branches are used to develop features or fixes in isolation from the main codebase.

List All Branches

git branch
Enter fullscreen mode Exit fullscreen mode

Lists all branches in your repository, highlighting the current branch.

Switch to a Branch

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switches to the specified branch.

Create and Switch to a New Branch

git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

Creates a new branch and immediately switches to it.

Merge Branches

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Merges changes from the specified branch into your current branch. This is typically used to integrate a feature branch into the main branch.

Delete a Branch

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Deletes a branch that has been merged. Use -D to force-delete a branch that hasn't been merged.

Rebase Branches

git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode

Rebases the current branch onto the specified branch. Rebasing can lead to a cleaner project history but should be used with caution, especially with shared branches.

5. Working with Remotes

Adding a Remote Repository

git remote add origin <repository-url>
Enter fullscreen mode Exit fullscreen mode

Connects your local repository to a remote server, typically a repository on GitHub, GitLab, or Bitbucket.

Viewing Remote Repositories

git remote -v
Enter fullscreen mode Exit fullscreen mode

Lists all remotes and their URLs.

Pushing Changes to Remote

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pushes your commits to the remote repository. If it’s your first push, you might need to set the upstream branch:

git push --set-upstream origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pulling Changes from Remote

git pull
Enter fullscreen mode Exit fullscreen mode

Fetches and merges changes from the remote repository into your current branch.

Fetching Changes from Remote

git fetch
Enter fullscreen mode Exit fullscreen mode

Downloads objects and refs from another repository. Unlike git pull, git fetch does not merge changes into your local branch automatically.

Removing a Remote Repository

git remote remove <remote-name>
Enter fullscreen mode Exit fullscreen mode

Removes a remote from your repository.

6. Undoing Changes

Unstage Changes

git reset <file-name>
Enter fullscreen mode Exit fullscreen mode

Removes changes from the staging area but leaves them in your working directory.

Discard Unstaged Changes

git checkout -- <file-name>
Enter fullscreen mode Exit fullscreen mode

Reverts unstaged changes in your working directory to the last commit.

Revert a Commit

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that undoes changes from a previous commit without altering the project history.

Reset to a Previous Commit

git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Rolls back your project to a specific commit, discarding all changes made after that point. Warning: This is irreversible.

Stash Changes

git stash
Enter fullscreen mode Exit fullscreen mode

Temporarily saves changes that you don’t want to commit yet, allowing you to work on something else.

Apply Stashed Changes

git stash apply
Enter fullscreen mode Exit fullscreen mode

Reapplies the most recently stashed changes to your working directory.

Drop a Stash

git stash drop
Enter fullscreen mode Exit fullscreen mode

Removes a specific stash from your list of saved changes.

7. Advanced Git Commands

Cherry-Pick a Commit

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Applies the changes from a specific commit to your current branch.

Squash Commits

git rebase -i HEAD~n
Enter fullscreen mode Exit fullscreen mode

Combines multiple commits into one. Replace n with the number of commits you want to squash.

View Commit History as a Graph

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

Displays the commit history in a graph format, showing branches and merges.

Cleaning Up Untracked Files

git clean -f
Enter fullscreen mode Exit fullscreen mode

Deletes untracked files from your working directory. Add -d to remove directories and -n to perform a dry run.

Conclusion

This cheat sheet covers a broad range of Git commands that will help you manage your projects efficiently. Whether you're committing changes, branching, merging, or working with remote repositories, mastering these commands will make you more effective as a developer. Keep practicing, and these commands will soon become second nature.

If you found this helpful, don’t forget to bookmark this post so you can come back to it later when you're wondering what command you should use. Also, please share it with your fellow developers so they can use it too!

Now if you really found it helpful, check out my weekly newsletter where I help hundreds of other developers level up in their careers and make more money! Also, give me a follow on X where I’m active daily!

Happy coding!

Top comments (2)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Good post! For anyone who wants to learn more check out this free ebook too:

GitHub logo bobbyiliev / introduction-to-docker-ebook

Free Introduction to Docker eBook

💡 Introduction to Docker

This is an open-source introduction to Docker guide that will help you learn the basics of Docker and how to start using containers for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use Docker at some point in your career.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Docker.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters

Collapse
 
tomasdevs profile image
Tomas Stveracek

Great overview for mastering Git! 👏 Also, writing good commit messages is key. For tips on that, check out my article Write Commits Like a Pro.