20 Essential Git commands every developer should know

20 Essential Git commands every developer should know
Contents

Git has evolved to become more than just a version control system; it’s now an indispensable tool in the toolkit of any serious developer. Understanding Git allows you to collaborate effectively, maintain a clean codebase, and track changes made to your projects over time. In this guide, we’ll explore 20 Git commands that you absolutely need to know, whether you’re a beginner or a seasoned pro. These commands will enable you to execute basic tasks, navigate repositories, and solve common problems you might encounter during your developer journey.

Why Git?

In the world of software development, chaos is always just a wrong line of code away. Imagine you’re working on a project with a team. Different team members make changes to the same files, and suddenly, things don’t work as they used to. That’s where Git comes in. It’s a version control system that helps you keep track of changes, manage branches, and coordinate with your team in a sane way. Git ensures that you have a safety net to fall back on and an efficient method to collaborate with others. And let’s not forget, almost all employers look for Git skills while hiring, making it essential for career advancement.

Setting Up Git

Before you can start using Git, you’ve got to install it on your system. The process is fairly straightforward. For Windows users, you can download the installer from the official Git website.

Mac users can use Homebrew to install Git

$ brew install git

while Linux users can get it via their package manager, eg using apt:

sudo apt install git-all

Once installed, open your terminal and run

git --version

to ensure it’s installed correctly. Next, you’d want to configure your username and email because Git attaches this information to each commit you make. Use the commands

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

to set these up. Now, you’re all set to dive into the world of Git.

Fundamental Git Commands for Every Developer

git init

Starting off with the basics, git init initializes a new Git repository in your local directory. It creates a hidden .git folder that contains all the information required for version control. This is typically the first command you’ll run when starting a new project.

git clone

Need to work on a project that’s already hosted somewhere else, like GitHub? Use git clone [URL] to copy that repository onto your local machine. This not only downloads the current version of all files but also maintains the history and version control data.

git status

Being aware of your repository’s status is crucial. git status shows you which files have been modified, which are staged to be committed, and which are untracked. It’s a command you’ll find yourself using frequently to double-check your changes.

git add

Before any files can be committed, they need to be staged, and that’s where git add comes into play. Use git add [file-name] to stage a specific file or git add . to stage all the files that have been modified or created.

git commit

After staging your changes, the next step is to commit them. git commit -m "Your commit message here" will take your staged changes and record them in the repository’s history, along with a descriptive message that explains what you’ve done.

git pull

When you’re collaborating with others, it’s essential to keep your local repository up-to-date with the remote one. The git pull command fetches changes from the remote repository and merges them into your current branch.

git push

The opposite of git pull, this command pushes your committed changes to a remote repository. After you’ve made and committed your changes locally, use git push to share those changes with your teammates.

Intermediate Git Commands for Streamlined Workflows

git branch

Managing multiple versions of your project? git branch allows you to create, list, or delete branches in your repository. A branch is essentially a unique set of code changes with a unique name. This is particularly useful for feature development or bug fixes.

git checkout

Switching between branches is a common operation, and git checkout [branch-name] is how it’s done. It updates your working directory to reflect the state of the specified branch, allowing you to focus on separate tasks without messing up your main codebase.

git merge

So, you’ve completed your work in a separate branch and want to incorporate it into your main project? git merge [branch-name] takes the contents of a source branch and integrates it with the target branch.

git stash

Sometimes you need to switch branches, but you’re not ready to commit your current changes. Enter git stash. This command takes your modified, tracked files and saves them away for later use, reverting your working directory to the last commit so you can freely switch branches.

git log

Tracking the history of your changes can get messy. git log gives you a detailed list of previous commits along with information like author, date, and commit messages. This is especially useful for tracking down when a bug was introduced or understanding the context of specific code changes.

git diff

Curious about what actually changed between commits or between the staged changes and the repository? git diff shows the exact lines that have been added or deleted, helping you understand the granular modifications made in the code.

git remote

Working with multiple remote repositories? git remote allows you to view or manage your set of remote repositories. You can add, rename, or delete remotes, making it easier to manage collaborations or multiple deployment platforms.

Advanced Git Commands for the Pros

git rebase

Rebasing is one of Git’s most powerful features, albeit a bit tricky to newcomers. What git rebase does is move or combine a sequence of commits to a new base commit. It’s a way to clean up a messy history and reapply changes from one line of work onto another without creating unnecessary merge commits.

git cherry-pick

Ever made a commit on the wrong branch? git cherry-pick allows you to apply the changes introduced by some existing commits. Simply put, it picks a commit from another branch and applies it onto your own branch.

git bisect

Finding out where a bug was introduced can be like finding a needle in a haystack. That’s where git bisect comes in. It uses a binary search algorithm to quickly identify the commit that introduced an issue.

git reflog

If you ever find yourself thinking, “What did I just do?”, git reflog is your friend. This command shows you a log of where your HEAD and branch references have been, making it easier to identify what changes you’ve made and effectively undo them if necessary.

git tag

Releasing a new version? git tag is used to pin down specific commits as “milestones” or versions of your project. This command lets you label commits with simple tags like “v1.0” or more complex annotations.

git submodule

Working on a big project that requires external code? git submodule allows you to nest one repository inside another. This is super useful for including libraries, plugins, or other projects without having to merge the codebases.

Mistakes to Avoid

  1. Not Using Branches: One common mistake is working directly on the main branch. This can cause a lot of confusion and chaos, especially in a team setting. Always create a new branch when working on a feature or fix.
  2. Poor Commit Messages: Commit messages like “fixes” or “updated files” aren’t helpful in understanding what the commit actually accomplishes. Stick to descriptive commit messages that document your changes.
  3. Forgetting to Pull Before Push: Before you push your changes, always make sure to pull the latest changes from the remote repository. This helps to avoid unnecessary merge conflicts.
  4. Ignoring .gitignore: The .gitignore file helps you specify files and folders that should not be tracked by Git. Ignoring this can lead to unnecessary files like log files or cache files being included in the repo.
  5. Not Reviewing Code Before Committing: It’s easy to commit changes in a rush. However, it’s important to review your code to check for errors, formatting issues, or other potential problems before committing them.

Conclusion

Man, Git is more than just a version control system: it’s a developer’s best friend when used right. By understanding and effectively using these 20 commands, you’re setting yourself up for success in any software project. But remember, Git is a tool, and like any tool, it’s only as good as the craftsman wielding it. So, keep practicing and stay curious!

Additional Resources

  • Pro Git Book: An in-depth guide covering all things Git.
  • GitHub Learning Lab: An interactive platform to sharpen your Git and GitHub skills.
  • Git Cheat Sheet: A quick reference guide for the most common Git commands.
  • Stack Overflow: For those tricky Git questions that you can’t seem to find the answer to
Author headshot
linkedingithubtoptal

Nick Trayze

Software Engineer, Toptal insider

Nick is a freelancer with over 10 years of experience, having contributed to nearly a hundred different projects across various industries.