DEV Community

Cover image for Git Cheat Sheet: A Beginner's Guide to Git Commands and Best Practices
Harish Kumar
Harish Kumar

Posted on

Git Cheat Sheet: A Beginner's Guide to Git Commands and Best Practices

Git is one of the most widely-used version control systems in the world, trusted by individual developers and large teams alike to manage source code, track changes, and collaborate on projects. Whether you're a beginner just starting out or an experienced developer, knowing the most useful Git commands is essential. In this guide, we will introduce you to the fundamental Git commands and best practices to help you master version control and streamline your development workflow.


Why Git is Essential for Developers

Git enables developers to:

  • Track changes in their code over time.
  • Collaborate with others on the same project without overwriting each other’s work.
  • Revert to previous versions of code in case something goes wrong.
  • Create different branches to work on new features without affecting the main codebase.

These features make Git an indispensable tool for anyone working on software projects, from solo developers to large teams.


Setting Up Git

Before you start using Git, you need to set up your environment by configuring your username and email address, which will be associated with all your commits.

# Set your Git username
git config --global user.name "Your Name"

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

Once configured, you can check your setup with:

# Display your Git configuration
git config --list
Enter fullscreen mode Exit fullscreen mode

Setting up your Git profile helps to ensure that your commits are properly attributed to you, especially when working in teams or open-source projects.


Spec Coder AI


Creating or Cloning a Repository

You can start a new Git repository or clone an existing one to work on an established project.

Initializing a New Repository

To start from scratch, initialize a new Git repository in your project directory:

git init
Enter fullscreen mode Exit fullscreen mode

This will create a .git directory in your project folder, which will start tracking changes.

Cloning an Existing Repository

If you're contributing to an existing project, you can clone a remote repository onto your local machine:

git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

Cloning will download the entire project, including its history, and allow you to work on it locally.


Tracking and Committing Changes

Once you've initialized or cloned a repository, you can begin making changes. Git helps you keep track of these changes through the staging area, which allows you to select what gets committed.

Checking the Status of Your Repository

Always start by checking the status of your repository:

git status
Enter fullscreen mode Exit fullscreen mode

This will show you which files have been modified and whether they’ve been added to the staging area.

Staging Changes

To track changes, you need to stage them first:

# Stage a single file
git add <file>

# Stage all modified files
git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes

Once your changes are staged, commit them with a message:

git commit -m "Describe what you changed"
Enter fullscreen mode Exit fullscreen mode

Commit messages are essential for documenting the changes, so it’s a good practice to write meaningful and concise messages.


Working with Branches

Git’s branching model allows developers to work on new features or bug fixes in isolation without affecting the main codebase. Branching makes development more efficient and organized, especially in collaborative projects.

Creating and Switching Branches

To create a new branch for a feature or bug fix, use:

git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

Switch to the new branch:

git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

Or create and switch to a new branch in a single step:

git checkout -b <branch_name>
Enter fullscreen mode Exit fullscreen mode

Branches are essential for parallel development and safe experimentation.

Merging Branches

Once your work on a branch is complete, you can merge it back into the main branch:

git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

Merging integrates your changes into the target branch. However, if there are conflicts between the changes, Git will notify you, and you’ll need to resolve them manually.


Collaborating with Remote Repositories

Git’s remote repository capabilities enable teams to collaborate on projects by sharing code and tracking contributions.

Adding a Remote Repository

If you're setting up a remote repository, use the following command to add it:

git remote add origin <remote_url>
Enter fullscreen mode Exit fullscreen mode

This allows you to link your local repository to a remote one, enabling push and pull operations.

Pulling Changes from Remote

To fetch updates from the remote repository, use:

git pull
Enter fullscreen mode Exit fullscreen mode

This will download and merge changes from the remote repository into your local branch.

Pushing Changes to Remote

After committing your changes, you can push them to the remote repository for others to access:

git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

This is crucial when collaborating with teammates, as it ensures everyone has access to the latest changes.


Undoing Changes

Git provides various commands to undo changes and recover from mistakes.

Discarding Local Changes

If you’ve made changes to a file but want to discard them:

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

This resets the file back to its previous state.

Resetting Commits

If you've made a commit and need to undo it, you can reset to a previous commit:

# Soft reset: Keep changes in the working directory
git reset <commit_hash>

# Hard reset: Discard all changes
git reset --hard <commit_hash>
Enter fullscreen mode Exit fullscreen mode

Use these commands carefully, especially when working in a shared repository, as they can modify commit history.


Stashing Work for Later

If you're working on something but need to switch tasks before committing your changes, you can stash your work. This temporarily saves your changes, allowing you to come back to them later.

Stashing Changes

git stash
Enter fullscreen mode Exit fullscreen mode

Applying Stashed Changes

Once you’re ready to resume, apply the stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Stashing is perfect for temporarily pausing your work without needing to commit unfinished changes.


Viewing History and Logs

Git’s log commands allow you to review the project’s history and track progress.

Viewing Commit Logs

To view a detailed log of all commits:

git log
Enter fullscreen mode Exit fullscreen mode

For a more condensed view:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

This shows a history of all changes made to the repository, helping you track changes and revert to previous states if necessary.

Comparing Changes

You can view the differences between two commits or between the working directory and the last commit:

# Show changes in the working directory
git diff

# Show differences between two commits
git diff <commit1> <commit2>
Enter fullscreen mode Exit fullscreen mode

This allows you to identify what has been modified between different states of your project.


Using Tags

Tags are used to mark specific points in a project's history, such as releases or major updates.

Creating a Tag

To create a tag for a specific commit:

git tag <tag_name>
Enter fullscreen mode Exit fullscreen mode

Tags help you keep track of milestones and can be shared with others.

Pushing Tags to Remote

To push your tags to the remote repository:

git push origin --tags
Enter fullscreen mode Exit fullscreen mode

This ensures that everyone on the team is aware of these significant points in the project’s history.


Advanced Git Tips and Aliases

To speed up your workflow, Git allows you to create shortcuts for commonly used commands, known as aliases.

Creating Aliases

# Create an alias for git checkout
git config --global alias.co checkout

# Alias for git commit
git config --global alias.ci commit

# Alias for git status
git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

Using aliases helps save time, especially if you frequently run the same commands.


More Cheatsheets for Other Languages

If you're looking for more detailed Git commands or cheatsheets for other programming languages like JavaScript, PHP, C++, React, and more, try out our VSCode extension: Ctrl+Alt+Cheat. This extension provides quick and easy access to cheatsheets for multiple languages directly in your editor.

Want to see it in action? Watch our tutorial video to learn how to use Ctrl+Alt+Cheat to boost your productivity!

👉 Download Ctrl+Alt+Cheat today and start coding like a pro!

🚀 Special Offer:

Enjoy a 50% discount on Ctrl+Alt+Cheat! Use the coupon code "YT-FAMILY" at checkout. Don't miss out on this limited-time offer!


Conclusion

Mastering Git is an essential skill for any developer, whether you’re just starting out or working on large-scale collaborative projects. This cheat sheet covers the basics and will help you manage your codebase efficiently, from committing and branching to undoing mistakes and collaborating with others. Keep practicing, and soon Git will become a natural part of your development workflow.

And don’t forget—if you need more help, check out Ctrl+Alt+Cheat for more in-depth cheatsheets on Git and other programming languages! Happy coding!

Top comments (0)