Distributed Version Control Git

What is Git?

  • Git is a popular open source distributed version control system created in 2005 by the Linux development community and used extensively in the Rails community.
  • Git provides means for comparing the differences between the various versions of a project (think of them as project snapshots), and for merging them accordingly.
  • You should think of these versions as forming a tree:
    • – Main development branch = master .
    • – New versions may be created along the master branch, or new branches can be created off of it.
    • – These branches can possibly be merged back into the master branch.

Git Basics

  • Git tracks files, which may reside in three “locations”:
    • – Working directory
    • – Staging area
    • – Git repository
  • Furthermore, the files in your working directory can be in one of two states:
    • – Tracked: files that were saved to the Git repository by the last commit operation.
    • – Untracked: all other files in your working directory.
  • Finally, tracked files can be either:
    • – Modified
    • – Unmodified
    • – Staged

Git Workflow–Local Repository

Here are the three directories that I just talked about. The working directory, the staging area, and the local Git repository.

Git Workflow–Local Repository

If you issue the command git add . it takes everything in your working directory, assuming that you've got Git installed, and it moves those files into the staging area. Actually just notes them as being staged. They're not actually moved. And then if you perform a git commit , that puts those files into your local Git repository. You can do this all in one step by typing git commit -a , and then you don't have to do the add operation. Now if you want to check out a particular branch, you just issue the checkout command, and you have to name the branch e.g. git checkout <branch> and then you get all of the files associated with that version in your working directory, then you can get to work on that. If you'd like to merge two branches you issue, git merge <branch> , it'll merge that into the current branch that you're working on within your working directory.

Git Workflow–Remote Repository

In the case when you have a remote repository.

Git Workflow–Remote Repository

In many cases you want to push those changes up to a remote repository so that others can grab them and use them. The command to do this is called push. You push to the remote repository and you have to specify which branch you'd like to push up to that remote repository e.g. git push <remote> <branch> . In order to retrieve a remote repository, you issue the git fetch command, and then you have check out a particular branch from that. So the fetch command brings that repository into your local repository, including all of the branches, and then you have the specify the branch you want to work on. Alternatively, you can issue this pull command, and this pull command is a fetch and it checks out the most recent branch and makes that your master e.g.git pull <remote> . Clone step is shown here e.g. git clone <remote> . The clone step is used to create the repository initially on your system. You do that once, and afterwards you can simply use pull commands to pull the remote Git repository onto your machine.