What is Verion Control?
Version control or source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help software teams manage changes to source code over time.
It keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.
Great version control systems facilitate a smooth and continuous flow of changes to the code rather than the frustrating and clumsy mechanism of file locking - giving the green light to one developer at the expense of blocking the progress of others.
Version control software is an essential part of the every-day of the modern software team's professional practices.
Git is a free and open-source version control system, originally created by Linus Torvalds in 2005. Unlike older centralized version control systems such as SVN and CVS, Git is distributed: every developer has the full history of their code repository locally. This makes the initial clone of the repository slower, but subsequent operations such as commit, blame, diff, merge and log dramatically faster.
It also has excellent support for branching, merging, and rewriting repository history, which has lead to many innovative and powerful workflows and tools. Pull requests are one such popular tool that allows teams to collaborate on Git branches and efficiently review each other's code. Git is the most widely used version control system in the world today and is considered the modern standard for software development.
Note: To understand this article, visit Git to know more about it.
The git init command is the first command that you will run on Git. The git init command is used to create a new blank repository. It is used to make an existing project a Git project. Several Git commands run inside the repository, but the init command can be run outside of the repository.
Creating the first repository
A repository is a directory that contains all the project-related data. There can also be more than one project on a single repository.
$ git init
The above command will create an empty .git repository. Suppose we want to make a git repository on our desktop. To do so, open Git Bash on the desktop and run the above command. Consider the below output:
The above command will initialize a .git repository on the desktop. Now we can create and add files on this repository for version control.
To create a file, run the touch command as follows:
$ touch <file name>
To add files to the repository, run the git add command as follows:
$ git add <file name>
Note: To learn more about the git add command visit Git Add.
An empty repository .git is added to my existing project. If we want to start version-controlling for existing files, we have to track these files with the git add command, followed by a commit.
We can list all the untracked files by git status command.
$ git status
Consider the below output:
In the above output, the list of all untracked files is displayed by the git status command. To learn more about the status command, visit Git Status.
After tracking our files, we can add our files using the git add command.
Branches are highly important in the git world. By using branches, several developers are able to work in parallel on the same project simultaneously. We can use the git branch command for creating, listing, and deleting branches.
Creating a new branch:
$ git branch <branch-name>
This command creates a branch locally. To push the new branch into the remote repository, you need to use the following command:
$ git push -u <remote> <branch-name>
Deleting a branch:
git branch -d <branch-name>
To learn more about the git branch *command, visit *Git Branch.
This is also one of the most used Git commands. To work in a branch, first, you need to switch to it. We use git checkout mostly for switching from one branch to another. We can also use it for checking out files and commits.
git checkout <name-of-your-branch>
There are some steps you need to follow for successfully switching between branches:
- The changes in your current branch must be committed or stashed before you switch
- The branch you want to check out should exist in your local There is also a shortcut command that allows you to create and switch to a branch at the same time:
git checkout -b <name-of-your-branch>
This command creates a new branch in your local (-b stands for the branch) and checks the branch out to new right after it has been created.
To learn more about the git checkout command, visit Git Checkout.
This is maybe the most-used command of Git. Once we reach a certain point in development, we want to save our changes (maybe after a specific task or issue).
Git commit is like setting a checkpoint in the development process which you can go back to later if needed.
We also need to write a short message to explain what we have developed or changed in the source code.
git commit -m "commit message"
To learn more about the git commit command, visit Git Commit.
NB: Git commit saves your changes only locally.
After committing your changes, the next thing you want to do is send your changes to the remote server. Git push uploads your commits to the remote repository.
git push <remote> <branch-name>
However, if your branch is newly created, then you also need to upload the branch with the following command:
git push --set-upstream <remote> <name-of-your-branch>
git push -u origin <branch_name>
To learn more about the git push command, visit Git Push.
NB: Git push only uploads changes that are committed.
Remember, the more you use git, the more comfortable you’ll… git with it. (I couldn’t resist.)