As developers, we are no strangers to managing and saving various copies of code before joining it to the main code.
Let’s discuss a better and efficient way to manage various code versions and merge them with the main code after testing.
Let’s dive in:
Introduction To Version Control Systems
We have mentioned that Git is a version control system. What exactly is a version control system, and how does it work?
A version control system is a system that allows developers to track file changes. Version control systems work by creating collections of various versions of files and the changes made to each version. They allow you to switch between various versions of the files seamlessly.
A version control system stores a collection of file changes in a location called a repository.
In most use cases, version control systems help track changes in source code files as they contain raw text. However, version control systems are not limited to text files; they can track even changes in binary data.
Types of Version Control Systems
There are various types of version control systems. They include:
- Localized Version control systems: This type of version control system works by storing various versions of the files locally by creating copies of the file changes.
- Centralized version control system: Centralized version control system includes a central server with various file versions. However, the developer still retains a copy of the file on their local computer
- Distributed Version control system: Distributed version control system does not require a server. However, it involves each developer cloning a copy of the main repository, and you have access to changes of all the files. Popular distributed VC systems are Git, Bazaar, and Mercurial.
Let us get started with Git.
Introduction to Git
Git is a distributed version control system developed by Linus Torvalds, the creator of the Linux Kernel. Initially developed to assist in developing the Linux Kernel, Git is powerful and easy to use. It supports linear development, which allows more than one developer to work on the same project concomitantly.
Let discuss how to install Git and use it to manage repositories:
How to Install Git on Linux
Depending on the system you are using, you will have Git installed by default. However, some systems may not have it installed. If that’s your case, use the following commands to install it on your system.
Debian/Ubuntu
Arch Linux
Install Git on Arch:
Fedora/RedHat/CentOS
Install on RHEL family:
sudo dnf install git
How to Configure Git
Once you install Git, you will get access to all its commands that you can use to work with local and remote repositories.
However, you need to configure it for first-time use. We will use the git config to set various variables.
The first config we set is the username and email address. Use the git config command shown to set the username, email address, and the default text editor.
git config --global core.editor vim
You can view the git configurations by using the git config –list command as:
user.name=myusername
user.email=username@email.com
core.editor=vim
How to Set up Repositories
We cannot mention Git and fail to mention the term repo or repository.
A repository, commonly called a repo, collects files and directories with their respective changes tracked by the version control system.
Changes in a repository are managed or tracked by commits, which are simple snapshots of changes applied to a file or directory.
Commits allow you to apply the changes or revert to a specific change within the repository.
Let’s now discuss how to set up a Git repository.
Suppose you have a project directory you would like to use as a git repo and track changes. You can initialize it using the command:
Once you run the git init command, Git initializes the directory as a repository and creates a .git directory used to store all the configuration files.
To start tracking changes using Git, you have to add it using the Git add command. For example, to add the file, reboot.c
To add all the files in that directory and start tracking changes, use the command:
After adding files, the next step is to stage a commit. As mentioned earlier, commits help track the changes to files in a repository.
Using the git commit command, you can add the message indicating the changes to the files.
For example, a message for the initial commit would be similar to:
NOTE: Adding descriptive and meaningful git messages helps other users using the repository identify file changes.
gitignore
Suppose you have some files and directories you do not wish to include in the main repository. For example, you may have configuration files for the development you are using.
To accomplish this, you need to use the .gitignore file. In the .gitignore file, you can add all files and directories that Git should not track.
An example of the .gitignore file typically looks like this:
node_modules/
tmp/
*.log
*.zip
.idea/
yarn.lock package-lock.json
.tmp*
Git Remote Repositories
Git is a powerful system that extends outside the scope of local repositories. Services such as GitHub, Bitbucket, and Gitlab offer remote repositories where developers can host and collaborate on projects using git repos.
Although some remote git services are premium—there’re many free services available—, they offer great tools and functionalities such as pull requests and many others that ensure smooth development.
NOTE: You can also build a self-hosted git service. Check our Gogs tutorial to learn how to accomplish this.
Let us now look at various ways to work with remote repositories.
Cloning a remote repository
A popular way to work with remote repositories is copying all the files in a remote repo to a local repo; a process called cloning.
To do this, use the git clone command followed by the URL of the repository as:
In services such as Github, you can download the zipped repository under the Download option.
To view the status of the files in the repository, use the git status command:
This command will tell you if the files in the repository have changed.
Update local repo from remote
If you have a cloned repository, you can get all the changes from the remote repository and merge them to the local one with Git fetch command:
Creating a new remote repository
To create a remote repository from the command line, use the git remote add command as:
Pushing local repo to remote
To push all changes from a local repository to a remote repository, you can use the git push command followed by the remote repository’s URL or name. First, ensure you have added the files, added a commit message as:
git commit -m “Added new function to shutdown. “ git push origin https://github.com/linuxhint/code.git
Deleting a remote repository
If you want to delete a remote repository from the command line, use the git remote rm command as:
Conclusion
We have covered the basics of setting up a Git version control system and how to use it to work with local and remote repositories.
This beginner-friendly guide is by no means a full-fledged reference material. Consider the documentation as there are a lot of features not covered in this tutorial.