as

Friday 8 May 2015

GIT - the most popular version control tool user guide

Introduction to version control system (VCS)
Version control systems are used to control the changes to the files. With VCS, we can easily find out who changed which files and when. We can also recover the files at certain point of time in the past. There are mainly 2 types of version control systems as mentioned below.

  1. Centralized version control system (SVN, Clear Case, Perforce)
  2. Distributed version control system (GIT, Mercurial, Bazaar, Darcs, BitKeeper)

Before GIT came up, we had a centralized version control systems like SVN, ClearCase which stored the versions of the files on central server. The issue with the centralized VCS is that if the server goes down, we can not check out the files from the server and we are blocked. Distributed VCS like GIT solves this problem by having the entire repository on the client machines. No matter if server is down or up, you can keep working on your local copy of the files and later push your changes to the server. Multiple people can work on different branches of the repository and merge these branches later which is not possible in case of centralized VCS.

What is GIT?
GIT is free distributed version control tool that can be used to manage any software project of any size.

There are 3 states of project in GIT.

  1. Working Directory
  2. Staging Area (index)
  3. Git Directory

When we modify any files, we need to add it to the staging area. Then we need to commit the files to the git directory.

What are the features of GIT?
GIT offers many features that are not available in other version control tools like SVN, Clear Case etc.
  1. Distributed
  2. Branching 
  3. Merging
  4. Staging Area
  5. Multiple Workflows
  6. Cloning
  7. Re-basing
GIT Installation and set up

GIT can be installed from the official website. Once GIT is installed you can fire below commands to set up the user for the GIT commands.

  1. git config --global user.name "G Bush"
  2. git config --global user.email gbush@washington.com

To see your settings are applied correctly, execute below command.

  1. git config user.email
  2. git config user.name
To view all keys, you can use below command.
  1. git config --list
To view the help for any command (verb), use below command.
  1. git help config
  2. git help <verb>
  3. git help <add>
To clone the repository from server, you can use below command.
  1. git clone <url>
To track new files, to stage the modified files and to mark the files to be staged after merge conflict , we use below command.
  1. git add <file name>
To remove the file from git, fire below command.
  1. git rm <file name>
  2. git rm cached <file name>
To view who committed what and when, use below command.
  1. git log --pretty=format:"%h - %an, %ar : %s"
To unstage file, use below command.
  1. git reset HEAD <file name>
To discard the changes in file, use below command.
  1. git checkout -- <file name>
  2. Stash or branch them
To view remote repositories that are configures, use below command.
  1. git remote -v


Frequently used GIT commands
GIT has got many commands as mentioned below.

  1. git -init     #Initializes empty git repository in current directory.
  2. git status  #Check the status of the project
  3. git add xyzfile  #add a file into the repository to track in staging area.
  4. git commit -m "comments"  #commit a file from staging area to repository.
  5. git add '*.txt'  #add all text files into staging area.
  6. git log  #to view our commit history
  7. git remote add origin https://github.com/xyz.git  #to add remote repository
  8. git push  #commit changes to remote repository
  9. git pull   #pull changes from the remote repository 
  10. git diff head  #view differences from last commit.
  11. git diff --staged  #view changes that are to be committed
  12. git reset filenamexyz  #remove file from staging area  
  13. git checkout -- octocat.txt  #get rid of last changes in the file
  14. git branch ABC  #creates the branch with name ABC
  15. git branch  #shows total branches
  16. git checkout ABC  #switches to the branch with name ABC
  17. git rm '*.txt'   #remove all files from current branch and disk as well
  18. git merge ABC  #merge changes from branch ABC to Master barnch
  19. git branch -d ABC   #delete the branch ABC
Steps to push the changes to remote repository like on GITHUB or Stash.

  1. git remote add <custom name of repo>  <url of repo>
  2. git push --set-upstream <nameofrepo-say-testrepo>  <nameofbranch-say-master >
To pull the changes from the repository, you need to use below command.
  1. git remote add <custom name of repo-say-testrepo>  <url of repo>
  2. git pull <nameofrepo-say-testrepo> <nameofbranch-say-master>

GIT with GITHUB
http://rogerdudler.github.io/git-guide/

GIT with Stash
Stash is used to manage GIT repositories.

Common Questions / Issues in GIT
  1. How to recover the last committed file ?
  2. How to untrack the file that's currently being tracked?
  3. I  do not want to push some files - git update-index --assume-unchanged Localization/el-GR.js 
  4. I do not want to pull some files like .csproj and settings.settings and references 
  5. git update-index --no-assume-unchanged .gitignore
What do you think on this topic? Please express your opinion or ask any question through comment below. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Leave your valuable feedback. Your thoughts do matter to us.

Sponsored Links

Popular Posts

Comments

ShareThis