Git : What, Why and How..
2.2 Learning Git | Hasura Internship
Step 1 : What is version control and git?
For this internship, our two-member team will be making a website for the startup “Green Strips”. If we want to edit the same file without disrupting each other’s work, we will have to:
- Lock a file while working on it so that the other person cannot edit it at the same time.
- Share the files to a common location so that both have access to the latest version.
Besides being error prone and risky, this file locking scheme would mean big hold ups as the team size increases. Many developers would want to work concurrently on the same files, be able to track code edits and roll back to a previous version if something goes wrong.
This is where version control comes in.
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.
Version control can be of three kinds:
- Local version control system (VCS): Keeping version history on the local machine itself.
- Centralized version control system (CVCS): Keeping version history on a single server from where a number of clients can check out files.
- Distributed version control system (DVCS): When clients check out files from the remote repository, complete version history is also backed up locally.
Git is an example of a mature, open source DVCS originally developed in 2005 by Linus Torvalds, the famous creator of the Linux operating system kernel. Check this for a short history of its development.
Step 2 : Why use git?
- Distributed Development: Git gives each developer a local copy of the entire repository along with the full development history.
- Data stored as snapshots: Every time there is a commit, git takes a snapshot of the file contents at that moment and saves a reference to it.
3. Local Operations:With locally stored version history, most git commands appear almost instantaneous and does not require connections to remote servers.
4. Security and integrity: All objects in the Git repository are secured with a cryptographically secure hashing algorithm called SHA-1.This ensures the change history is fully traceable and protected against accidental or malicious alteration.
5. Flexibility: Git is scalable and compatible with existing protocols and systems. Repositories can be published over HTTP, FTP or using SSH. CVS repositories can also be used with git-svn.
6. Parallel workflow: With branching and merging git allows simultaneous development of different features along different branches.
Step 3 : How to use git?
- Open git bash terminal from your folder by right clicking and choosing ‘Git Bash Here’.
- To initialize git:
- To add files to the staging area so that git can track changes :
git add <filename>
- To see the current status of files:
- To commit changes:
git commit -m "Your commit message"
- To view commit history:
- To create a local working copy of a remote repository:
git clone <url>
- To add a new remote Git repository:
git remote add <repo-name> <url>
- To share local branch on remote server
git push [remote-name] [branch-name]only if you have write access and if nobody has pushed in the meantime.
- To get data from your remote projects:
git fetch <remote-name>and then merge it with your local branch by
git merge <branch>
- If our local branch is tracking the remote repository we can directly use
git pull <remote-name> <branch-name>
- To create and change to a new branch:
git checkout -b <branch-name>
- To delete branch:
git branch -d <branch-name>
- To check differences with the last commit :
git diff HEAD
- To tag your code versions:
git tag <version> <commit-id>
Try out these commands online at:
Additional Free Resources:
The entire Pro Git book written by Scott Chacon and Ben Straub is available to read online for free. Dead tree versions…git-scm.com
Git is a distributed version control system. Every dev has a working copy of the code and full change history on their…www.atlassian.com
In case you ever mess up while using git, check out http://justinhileman.info/article/git-pretty/git-pretty.png