Many of us have heard about GIT and as a developer, most of you have even used it. Today I will just recap briefly about GIT as a version control system. We will also look in the popular function and commands available in GIT.
Version Control System:
It is a technology which helps you to maintain your codebase efficiently and provide a mechanism to work in a group or organization in most efficient manner.
Popular Types of version control:
a) Centralized Version Control System
b) Distributed Version Control System
Benefits of using GIT
a) Faster commands
b) Stability
c) Isolated Environments
d) Efficient Merging
Basic Concepts about GIT
Every Git repository has 4 components
a) The working directory
b) The staging Area
c) Committed history
d) Development branches
The Working Directory:
This is the directory where you edit files and compile your code. It is just like a normal folder but you can use all sort of git commands on it.
The Staging Area:
It is an intermediate stage/area where you keep your files which need to be added as a changeset. Git allows you to keep a version of your files in different changesets. When a user is creating a changeset he/she could select files which need to be added in changeset by adding them in staging area.
The Committed History:
Once you have added your files in the staging area, you are ready to commit them in the project history. This history is safe and will not be altered without user’s permissions.
The Development Branches:
Using above 3 concepts of Git, a user will be able to create a linear workflow for his/her codebase. So he/she will be adding different changesets on top of one another. With Git we can also create different branches and maintain changesets parallel to the main development. This provides a lot of freedom to developers and teams to work together on a single project.
GIT installation
GIT is available for all popular platforms (Linux, Mac & Windows). It could be easily downloaded from its website (https://git-scm.com/book/en/v2/Getting-Started-Installing-Git )
GIT Configuration
Once you have installed git you need to configure it. This configuration adds your identification for the changesets which will create in any repository. Follow these commands
git config –global user.name “your name” git config –global user.email “your email id”
you can also create your aliases for git commands very easily.
git config –global alias.st status git config –global alias.ci commit git config –global alias.co checkout
git config –global alias.br branch
Help Command:
git help config
Initializing Repositories: In the following command path is optional and if not mentioned it will consider the current directory as a path.
git init <path>
Cloning Repositories: In the following code you need to mention the repository address. The address could be of type FTP or HTTP/ HTTPS or File address.
git clone <repository address>
Recording changes
Once you have initialized git repository you are ready to make you first commit. As we have already discussed git stores history is set of changesets and we create these changesets using commit command.
After you have added certain files to your git folder you need to stage them. Files could be individually staged by this command
git add <file name>
If you need to add all files at once
git add –all
To delete files from the staging area.
git rm –cached <file>
Inspecting the Staging area
git status
Generating Diffs
If you need detailed information about the changes in working directory
git diff
Once files have been added to staging area you can see changes by the following command
git diff –cached
Following displays repository history or snapshots committed so far
git loggit log –oneline (for short & concise history)
Committing Changes
Once you have added your files in the staging area, they are ready to be committed.
git commit –m “ your message”
For visualizing repository history we can use the following command
gitk
Tagging Commits
Tags are like pointers to commit and they help in bookmarking useful revisions in the repository.
git tag –a <yourTag> –m “Stable release”
Displaying all tags
git tag
Undoing Changes
As you know the point maintaining software history is to get peace of mind. So accidently the code breaks we should be able to revert the last changes.
Undo changes in Working Directory
If you need to go back to the last commit and revert all the changes you added after last commit use following command.
git reset –hard HEADgit clean –f
Individual Files:
We can undo individual file changes by this command.
git checkout HEAD <file>
Undoing changes in staging area
Following command undo the changes for the individual file from the staging area. Note that it will not change the content of the file instead of that, it will only remove it from staging area.
git reset HEAD <file>
Undoing Commit
You can undo your last commit by the following command. Your content will not be changed instead, they will be left as it is in working directory.
git reset HEAD~1
Reverting
We can undo last commit changes in another method also. We can add a new commit which actually undo the last commit file changes.
git revert <commit-id>
Amending
Instead of completely removing the last commit we can also amend the most recent commit by staging files as usual and then running following command.
git commit --amend
we will be looking branching and other advanced concepts in another post. Stay connected.
