Last updated on March 16, 2021 by Alfonso Silva
Developing applications is not as easy as many might think. It requires coding skills and the use of productivity tools that make the development process fast and efficient. One of those handy coding tools is a version control system called Git. Since its debut in 2005, Git has become the de-facto standard for maintaining code versions, and now it is a must-have tool for any developer who is participating in an open-source project. However, at first, it can be a bit difficult to learn about it, and that is why this post has been written to provide a basic guide to Git.
Git is a version control system created by Linus Torvalds to initiate and coordinate the massively collaborative development of the Linux kernel, that has been contributed by more than ten thousand developers worldwide. Git-based version control is distributed in a sense that every developer has the entire development history for a given Git repository. This means less chance for data loss and less reliance on a remote server. Git is open-source and cross-platform, making possible a lot of additions such as third-party GUI applications that provide platform-specific user-friendly experience. With Git, you can add, delete or modify files in a software project and compare or retrieve the status of the files at any given time.
If you are just starting with Git, it is best for you to get familiar with
git-bash which is the tool provided by Git to use in a terminal environment, instead of directly jumping to a fancy GUI-based Git frontend. In this tutorial, all the examples and practice with Git will be demonstrated in the Linux terminal environment.
Before we start using commands and examples to demonstrate how Git works, it's a good idea to first check some of the terminology used in Git.
.gitfolder that holds all of your information.
On Linux, Git is included in the official repositories of almost all distributions. This makes the installation process quite easy, even though we may not have the latest stable version.
$ sudo apt update $ sudo apt install git
$ sudo dnf update $ sudo dnf install git
$ sudo zypper in git
When you have installed the
git package on any distribution, you will know it is ready to be used if you run the following command:
$ git --version
And you get output that looks something like this:
$ git version 2.20.1
git command is ready for you to use.
Once you have installed Git on whatever your favorite distro is, you can start using it from the terminal.
As a first step, you need to create a local repository, which you can do by running the
git command in this syntax:
$ git init [repo_name]
You have to replace
[repo_name] with the name you want to give to the repository. For example, I'll use the name
$ git init first-repo
Running the above command will generate the following output:
Initialized empty Git repository in /home/user/first-repo/.git/
As a consequence, a new folder will be created with the name of the repository, and it will start as a Git repository at once. To do this, a hidden folder named
.git/ is created where the Git information will be stored, and the default branch called
master is created.
$ cd first-repo
Once the repository has been successfully created and initialized. The second step is to configure Git with the name and email address of the user who will use the repository.
Doing this is very simple. Just run the
git command and the
config subcommand next to the config
name parameter, as follows.
$ git config user.name [your-username]
Of course, replace
[your-username] with a real name.
Also, the email address of the user has to be configured so that Git can better keep track of changes and this is necessary to work together.
$ git config user.email [email]
And to verify the applied changes, you can run the command:
$ git config --list
core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true user.name=user [email protected]
Now you have the repository set up, and you can start doing your work as a contributor for the repository. Typically you contribute to a repository in the form of making commits that will be added to the repository.
Before making a commit, you create new files or modify existing files. When you are ready, you have to add them to staging which is a previous state of the commit.
For demonstration, I will create a new file called
$ touch README.md
And it is possible to check the status of the repository by running:
$ git status
On branch master Changes to be committed: (use "git reset HEAD
..." to unstage) new file: README.md
In the sample output, you'll notice that Git still doesn't track the newly created file.
After the file has been created or modified, then add it to staging with the following syntax.
$ git add [file-name]
In this case:
$ git add README.md
But what if you have modified many different files? Well, use the
--all option so that from a single command, you can add them all to staging.
$ gi add --all
If you add a file to staging and you regret it or consider that it's still not worth it, you can remove it from staging with the
$ git reset [file-name]
When you've got the files ready, we can do a commit:
$ git commit
Running the above command opens a default text editor for us to add a comment about the commit. This step is necessary for Git to effectively keep track of the changes you've made.
Another option is to add the comment in the same command with the
-m option followed by the comment. For example:
$ git commit -m "My First commit"
Where you can type whatever comment you need or suits your needs.
[master (root-commit) b43e7c0] My First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md
Finally, you can check the status of Git with the
$ git log
Where you can check which files are about to commit.
You could keep working and keep committing, but if there are other people working on this same repository, they still won't have the changes. For that you have to use the
$ git push
With this, if someone clones the repository or updates, they will have the latest changes made by you.
Version control systems like Git can do development branches. When a repository is started, the master branch is created by default, but it is possible to create a new branch.
First, check which branch you are in with:
$ git branch
And to create a new development branch, you can run the following command:
$ git branch [new-branch-name]
In this case, I will create a new branch called
$ git branch alpha
And to show if it has been successfully created, you can run:
$ git branch
alpha * master
* sign indicates which branch is the current development branch. To switch to another branch, you need to use the
checkout subcommand and indicate the branch name.
$ git checkout alpha
A README.md Switched to branch 'alpha'
After that, you can check out the active branch again.
$ git branch
* alpha master
From here, you can add new files, make new commits and continue working.
But there comes a point where the changes become so robust and stable that this branch can become the master branch. More or less, what is required is to merge the development branches.
In this case, we will merge the
alpha branch to the
master branch. So switch to the
master branch by running:
$ git checkout master
And now perform the merge process by running:
$ git merge alpha
This process, while simple, is not error-free. So be careful.
Sometimes you may need to rename a branch. This process is fairly straightforward. First, switch to the branch you wish to rename. For this example, I will rename the
alpha branch to
$ git checkout alpha
And from there, make the change by running:
$ git branch --move alpha milestone
This way, it's possible to easily rename a development branch.
It is also possible to clone a remote repository and have it on your system to work with. This is quite common on any work computer.
The process is quite simple thanks to the
$ git clone [url-repository]
Once you have it on your computer, you have to set up the username and email as explained above.
After that, you can start working and add the necessary commits. And at the end, you can do
git push to have the changes reflected in the remote repository.
Git is a well-known tool for developers nowadays. It provides more precise control over the changes that are made to any collaborative coding project. In this post, you have learned the basics of Git, whose basic operations are not complicated to learn, although it is quite flexible and can have more complex operations that meet the demands of many workgroups. Any comment or feedback on missing features is welcome.
This website is made possible by minimal ads and your gracious donation via PayPal or credit card
Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.
Xmodulo © 2021 ‒ About ‒ Write for Us ‒ Feed ‒ Powered by DigitalOcean