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:
💡 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.
To download a copy of the eBook use one of the following links:
Git is designed to help programmers coordinating work with each other. Its goals include speed, data integrity, and support for distributed workflows.
All you need in order to be able to follow along is a
git terminal or a
bash shell with
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:
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"
The same would go for changing your email address associated with your commits as well:
git config --global user.email "email@example.com"
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.
In my case the output looks like this:
commit 45f96b8c2ef143011f11b5f6cc7a3ae20db5349d (HEAD -> main, origin/master, origin/HEAD) Author: Bobby Iliev <firstname.lastname@example.org> Date: Fri Jun 19 17:03:53 2020 +0300 Nginx server name for www version (#26)
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 .
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
In order to check the current status of your local git repository, you need to use the following command:
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.
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
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:
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
In order to remove a file from your git project use the following command:
git rm some_file.txt
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
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
This is a really handy command as you can really quickly revert a file back to its original content.
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:
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"
In order to list all of the available local branches just run the following command:
git branch -a
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
remotes keyword indicates that those branches are remote branches.
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
Note that this will also merge the new changes to the current branch that you are checked into.
To create a new branch all you need to do is use the
git branch command:
git branch branch_name
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
branch_name already exists, you would get a warning that the branch name exists and you would not be checked out to it,
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
git branch -d branch_name
git checkout branch_name
As mentioned above, if you add the
-b flag it would create the branch if it does not exist.
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!