A list of the most essential Git commands that every developer should know.
When we’re starting to learn to program, the first initiative that we make is to create our projects and manage them in folders. When we need to make a change, we go to the file and change it. As we learn more about it, we start to question if this is the most efficient way of programming and managing our source code.
Imagine that we have to create a modern application and use a shared folder with our teammates. We can see that this could quickly become chaos. Someone else could overwrite our changes, and it would be very hard to keep track of the actual code in production. We need to be able to change a piece of code without having to worry about it affecting anything else. Putting something in our code that shouldn’t be there is something we do often—and if this mistake happens in production code, it can quickly spell disaster. We need to keep track of our code—we need version control.
A version control system is a tool that helps us to keep track of changes in our code over time. It helps modern teams to work faster and smarter. A version control system gives independence to developers to work on a specific piece of code without having to worry about their changes being overwritten.
There were many version control systems before the creation of Git, but Git has become so popular that it is rare for a developer not to use it every day. Every company is using Git nowadays daily to ship and deliver code.
We’re going to explore the most essential Git commands that we should know and how exactly each one of them works.
There are many Git commands that can be useful on a daily basis. We’re going to explore here the most important commands—those commands that you’re probably going to use every single day of your life as a developer.
Whenever you have a project that is not under a version control system and you want it to become a Git repository, this is the command that you’re going to use.
git init
The git init
command creates a new .git file and makes your project a Git repository. Inside the .git file is all the information that your project needs for version control.
You can run git init
in an initialized Git repository and it will not overwrite things that are already there.
When you already have the Git project that you want to work on, you will definitely need to use this command for starting to work on it safely.
The git clone
command clones the source code from a remote repository and creates a new directory. The command will copy all the code from the specified repository, create remote tracked branches and check out an initial branch locally.
To clone a repository from GitHub, you need to go to the project page and click on the green “Clone” button. A popup will open and all you go to do is copy the link to the remote repository. Now, after the git clone
command, you paste the link of the remote repository and a new directory will be created for you.
git clone <remote repository link>
The git clone
command is a very important one because it gives you freedom to clone a repository and start to work on it without affecting the original code.
When you first clone your remote repository and a new directory is created for you, by default a branch with the name “master” will be created.
Whenever you want to work on a new feature or fix a bug, you’re going to create a branch for it. Branches are a way to push changes to the source code without having to work on the master branch and risk making a dumb mistake and ruining everything.
To create a new branch, you need to use the git branch
command and give your branch a name:
git branch <branch name>
After creating a branch, it will only be available locally. To push the new branch created to your remote repository, you need to use the git push
command and pass the origin name and the branch name:
git push <origin> <branch name>
Branches are very important in a Git workflow. To be able to work on a specific branch, we need to create one or switch to an existing branch. This is what the git checkout
command does.
We can simply switch to an existing branch by using the git checkout
command and passing the name of the branch we want to switch to:
git checkout <branch name here>
Before you try to switch to an existing branch, you need to make sure that all your changes are committed and the actual branch that you want to switch to exists in your local repository.
What about creating a new branch? We can use the git checkout
command and pass a -b
to create a new branch and switch to it:
git checkout -b <branch name here>
The “-b” stands for “branch”—it will create a new branch and immediately switch to it.
Sometimes we want to have information about our current work. The git status
command gives us all the information about our work on our current branch.
git status
We can use this file to know whether our branch is updated or not. We can use it to know if there’s anything to commit, pull or push to our remote repository. We can use it to know which files are staged, unstaged or untracked and which ones were created and deleted.
Whenever we make changes to our current branch, these changes are only on our local repository. We need to make sure to include our changes into our next commit.
The git add
command includes our changes it our next commit. We can add a singe by typing git add
and the name of the file, like this:
git add <name of the file>
Or we can add all changes that we made using the -A
:
git add -A
Simply using the git add
command does not mean that your changes are saved. We need to use the git commit
command to make sure we saved our changes.
After including our changes into our next commit using the git add
command, we need to save our changes.
The git commit
command is responsible for locally saving our changes. After the git commit
, we can write a message. Whenever you’re committing something, try to write the cleanest and simplest message possible. It helps to make the commit easier to read and understand what exactly the commit is.
git commit -m "your message..."
This command sets a breakpoint into our Git history. Whenever we want to track and go back to our code, we can do that using git commit
ids.
The git commit
command only saves our changes locally. We need to use a command for pushing our changes to our remote repository.
The git push
command is responsible for pushing our changes to our remote repository.
git push origin <branch name>
We can only push changes to our remote repository after saving our changes using the git commit
command.
The git pull
command is responsible for fetching new information from our remote repository. When we use this command, it updates our local repository with the latest changes in the remote repository.
git pull <remote>
This command is a simple way of synchronizing your local repository with the latest changes in the remote repository and keeping everything updated.
Learning and knowing how Git works plays an important role in our career. Whether we’re a beginner or an experienced developer with years of experience, knowing how to use the essential Git commands makes a huge difference.
Leonardo is a full-stack developer, working with everything React-related, and loves to write about React and GraphQL to help developers. He also created the 33 JavaScript Concepts.