What is the need of any VCS?
In culture of coding, there are multiple problems which faced by coders.
Those problems are:
Backup of project
History & Tracking of the project (Like Google Docs)
Collaboration with the team.
So for these problem, coders came to an conclusion that they need a system which can maintain history and solve the above mentioned problems.
And this system is known as VCS (Version Control System)
Few well known examples of VCS are:
Git (build by Linus Torvalds, who also built Linux)
Subversion (Apache)
Mercurial
Perforce
What is Git?
Git is a CLI (Command Line Interface) based VCS.
Setting-up Git in our system
Download installer form this link - https://git-scm.com/downloads and install it in your system by just clicking next, next to successfully install it in your system.
Commands in Git
git init
This command tells git to track the particular project/file/folder.
Git makes hidden folder named ‘.git’, which has a simple file system where all changes in the project is stored.
U (Untracked) : Git by default doesn’t tracks any file. The untracked files are marked with U.
git add <filename>
Git starts version control on that file.
To add all files in git, we use
git add .
A (Index Added) : Inside .git folder there is a file ‘index’ which has index of which files are being tracked. And those files are marked as A.
git status
Gives the status of whole project, like New Files, Tracked Files etc.
Gives staging info.
git commit -m “Messages“
Creates a checkpoint of the project.
git log
Gives info of all commits (hash, author, date, time).
M (Modified) : Marks the file which is been modified & not yet version controlled.
git diff
- Gives all difference from last commit and current scenario.
git cat-file <type> <object>
Prints file content into terminal. It has variant like -p (print original content), -t (type of object), -s (size of object) these come in types and for <object> use Hash code of the particular commit.
git cat-file -p <Hash>
Gives info about that file. Where p means print. So, it shows the content of the file.
git cat-file -t Hash
Gives type of object.
Note: All the commits are stored in .git/objects/<first 2 letter of hash>
Log:- Log tells history (i.e. which commits are done in what sequence).
It is in Linked List where most recent commit will be head node. Every commit has reference of previous commit.
Hash of any commit : Read code file → Hash SHA1 Algo → Commit Hash. If same hash code generated that means the code file is same and hence no changes are there.
git-log -- oneline
shows log in systematic way.
git reset <Hash>
‘Hash’ → Revert back to that commit. By this, head comes to that hash commit. But all the further commits will show up in changes.
git reset --hard <Hash>
- Reverts with deleting all the changes.
Till here you learned how to handle git it your local system with all tracking and controlling. Now let’s learn how to connect this to get on Internet i.e. Git.
Collaboration in Git
For collaborating we need to just share .git folder along with all files of project. But there is an problem, if this collaboration is done manually. We need to share .git folder every time for the collaboration.
To fix this problem, Github enters.
Firstly go to https://github.com. Login/ Signup with your id.
Then create your repository by clicking on
+
icon on top right corner.There after by giving name to your repository, scroll down and click
Create Repository
git remote add origin <link>
To connect the local machine to Github, we need to run this. The link will be pr
git remote -v
For knowing where our project is connected. Where will the push and fetch/pull will done.
git push -u origin <branch>
where u means ‘upstream’ & this is to be done on first time.
- git push: this is usually done.
For more detailed information, refer Github Education Cheat sheet