Essential Git Commands

A list of the most essential Git commands that every developer should know.


This content originally appeared on Telerik Blogs and was authored by Leonardo Maldonado

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.

Git Commands

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.

git init

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.

git clone

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.

git branch

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>

git checkout

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.

git status

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.

git add

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.

git commit

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.

git push

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.

git pull

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.

Conclusion

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.


This content originally appeared on Telerik Blogs and was authored by Leonardo Maldonado


Print Share Comment Cite Upload Translate Updates
APA

Leonardo Maldonado | Sciencx (2022-04-05T08:32:02+00:00) Essential Git Commands. Retrieved from https://www.scien.cx/2022/04/05/essential-git-commands/

MLA
" » Essential Git Commands." Leonardo Maldonado | Sciencx - Tuesday April 5, 2022, https://www.scien.cx/2022/04/05/essential-git-commands/
HARVARD
Leonardo Maldonado | Sciencx Tuesday April 5, 2022 » Essential Git Commands., viewed ,<https://www.scien.cx/2022/04/05/essential-git-commands/>
VANCOUVER
Leonardo Maldonado | Sciencx - » Essential Git Commands. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/05/essential-git-commands/
CHICAGO
" » Essential Git Commands." Leonardo Maldonado | Sciencx - Accessed . https://www.scien.cx/2022/04/05/essential-git-commands/
IEEE
" » Essential Git Commands." Leonardo Maldonado | Sciencx [Online]. Available: https://www.scien.cx/2022/04/05/essential-git-commands/. [Accessed: ]
rf:citation
» Essential Git Commands | Leonardo Maldonado | Sciencx | https://www.scien.cx/2022/04/05/essential-git-commands/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.