DEV Community

loading...
Cover image for Top 18 Git commands that you should know

Top 18 Git commands that you should know

bobbyiliev profile image Bobby Iliev Originally published at devdojo.com ・5 min read

Introduction

Git is without a doubt the most popular version control system for tracking changes in source code out there.

The original author of git is Linus Torvalds who is also the creator of Linux.

If you are new to Git, I would strongly recommend checking out this opensource eBook here:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

πŸ’‘ Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

πŸš€ Download

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

πŸ“˜ Chapters

  • About the book
  • Introduction to Git
  • Version Control
  • Installing Git
  • Basic Shell Commands
  • Git Configuration
  • Introduction to GitHub
  • Initializing a Git project
  • Git Status
  • Git Add
  • Git…

Git is designed to help programmers coordinating work with each other. Its goals include speed, data integrity, and support for distributed workflows.

Prerequisites

All you need in order to be able to follow along is a git terminal or a bash shell with git installed.

To make things even better you can use the following referral link to get free $100 credit that you could use to deploy your servers and test the guide yourself on an actual Linux server:

DigitalOcean $100 Free Credit

Git Configuration

Before you initialize a new git repository or start making commits, you should set up your git identity.

In order to change the name that is associated with your commits, you can use the git config command:

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

The same would go for changing your email address associated with your commits as well:

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

That way once you have the above configured when you make a commit and then check the git log you will be able to see that the commit is associated with the details that you've configured above.

git log
Enter fullscreen mode Exit fullscreen mode

In my case the output looks like this:

commit 45f96b8c2ef143011f11b5f6cc7a3ae20db5349d (HEAD -> main, origin/master, origin/HEAD)
Author: Bobby Iliev <bobby@bobbyiliev.com>
Date:   Fri Jun 19 17:03:53 2020 +0300

    Nginx server name for www version (#26)

Enter fullscreen mode Exit fullscreen mode

Initializing a project

To initialize a new local git project, open your git or bash terminal, cd to the directory that you would like your project to be stored at and then run:

git init .
Enter fullscreen mode Exit fullscreen mode

If you already have an existing project in GitHub for example, you can clone it by using the git clone command:

git clone your_project_url
Enter fullscreen mode Exit fullscreen mode

Current status

In order to check the current status of your local git repository, you need to use the following command:

git status
Enter fullscreen mode Exit fullscreen mode

This is probably one of the most used commands as you would need to check the status of your local repository quite often in order to be able to tell what files have been changed, staged, or deleted.

Add a file to the staging area

Let's say that you have a static HTML project and you have already initialized your git repository.

After that at a later stage, you decide to add a new HTML file called about-me.html, then you've added some HTML code in there already. In order to add your new file so that it is also tracked in git, you first need to use the git add command:

git add file_name
Enter fullscreen mode Exit fullscreen mode

This will stage your new file which essentially means that the next time you make a commit, the change will be part of the commit.

To check that you can again run the git status command:

git status
Enter fullscreen mode Exit fullscreen mode

You will see the following output:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   about-me.html
Enter fullscreen mode Exit fullscreen mode

Removing files

In order to remove a file from your git project use the following command:

git rm some_file.txt
Enter fullscreen mode Exit fullscreen mode

Then after that, if you run git status again you will see that the some_file.txt file has been deleted:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    some_file.txt
Enter fullscreen mode Exit fullscreen mode

Discard changes for a file

In case that you've made a mistake and you want to discard the changes for a specific file and reset the content of that file as it was in the latest commit, you need to use the command below:

git checkout -- file_name
Enter fullscreen mode Exit fullscreen mode

This is a really handy command as you can really quickly revert a file back to its original content.

Commit to local

Once you've made your changes and you've staged them with the git add command, you need to commit your changes.

To do so you have to use the git commit command:

git commit
Enter fullscreen mode Exit fullscreen mode

This will open a text editor where you could type your commit message.

Instead, you could use the -m flag to specify the commit message directly in your command:

git commit -m "Nice commit message here"
Enter fullscreen mode Exit fullscreen mode

List branches

In order to list all of the available local branches just run the following command:

git branch -a
Enter fullscreen mode Exit fullscreen mode

You would get a list of both local and remote branches, the output would look like this:

  bugfix/nginx-www-server-name
  develop
* main
  remotes/origin/HEAD -> origin/master
  remotes/origin/bugfix/nginx-www-server-name
  remotes/origin/develop
  remotes/origin/main
Enter fullscreen mode Exit fullscreen mode

The remotes keyword indicates that those branches are remote branches.

Fetch changes from remote and merge the current branch with upstream

If you are working together with a team of developers working on the same project, more often than not you would need to fetch the changes that your colleagues have made in order to have them locally on your PC.

To do that all you need to do is to use the git pull command:

git pull origin branch_name
Enter fullscreen mode Exit fullscreen mode

Note that this will also merge the new changes to the current branch that you are checked into.

Create a new branch

To create a new branch all you need to do is use the git branch command:

git branch branch_name
Enter fullscreen mode Exit fullscreen mode

Instead of the above, I prefer using the following command as it creates a new branch and also switches you to the newly created branch:

git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode

If the branch_name already exists, you would get a warning that the branch name exists and you would not be checked out to it,

Push local changes to remote

Then finally once you've made all of your changes, you've staged them with the git add . command and then you committed the changes with the git commit command, you have to push those changes to the remote git repository.

To do so just use the git push command:

git push origin branch_name
Enter fullscreen mode Exit fullscreen mode

Delete a branch

git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode

Switch to a new branch

git checkout branch_name
Enter fullscreen mode Exit fullscreen mode

As mentioned above, if you add the -b flag it would create the branch if it does not exist.

Conclusion

Knowing the above commands will let you manage your project like a pro!

If you are interested in improving your command line skills in general I strongly recommend this Linux Command-line basics course here!

Hope that this helps!

Discussion (3)

pic
Editor guide
Collapse
devparkk profile image
Dev Prakash

Wow ! Thanks a lot

Collapse
alexgeorgiev17 profile image
Alex Georgiev

This is really useful! Thanks for sharing it, Bobby!

Collapse
boiliev profile image
Boyan Iliev

Wow. I can't believe that there are more than 5 git commandsπŸ˜‚πŸ˜‚πŸ˜‚πŸ˜‚. These are really helpful. Thanks for sharing them with usπŸ™ŒπŸ™Œ