Branching in Git

Nope, there’s no harm in running and playing around in Git. Branches? Nope, not an issue! They are easy to master. All you need to keep in mind while thinking of Branching in Git are :

  • HEAD pointer
  • Commit history ( A linked chain of commits )
  • A few commands
  • To understand branching we should first understand how Git stores data. It is stored in the form of Snap shots that is actually a chain of commits from the first commit to the most recent commit.

    So, think of a commit as a trigger. As we make a commit the following events occur :
  • A new commit object is created
  • Previous commit object points to the new commit object
  • HEAD pointer points to the branch with the new commit object 

    What is HEAD ? It is just a special pointer in Git that’s used to point to the most current branch. So, if you made a commit , say, in branch master,HEAD points to branch master. But, if you perform the following command git checkout -b testing Git will create a new branch testing and will move HEAD to point to the new branch which basically points to the latest commit object of branch A. Similar stuff happens to an existing branch too. Now, if you commit from testing, HEAD will still point to this branch but this branch will move forward in the project. This means that from the previous commit object which was being pointed to by both branch master and testing points to the new commit object which is in branch testing now and is being pointed to by HEAD while branch master still points to the previous commit. Apparently, it means that this doesn’t in any way affect master. But, now if we run the commands :

  • git checkout master
  • git add changed_file
  • git commit -m "Just committed"
  • a new diverging commit object created. This means that the first commit object now points to two different commit objects belonging to two different branches master and testing where HEAD still points to the most recent branch, in this case master. The image below illustrates a similar situation for two branches master and testing situation.

    Now I am quite sure that you are more comfortable with Git Branching than you have been 10 minutes earlier. To view the current status of the branch pointers and HEAD just type the following command :

  • git log --oneline --decorate

So you wanna GIT ?

Why GIT ?

Every time you’re asked to submit that crucial programming assignment what comes to your mind after completion of the program ? Uh, not a movie. It’s GIT I know ! So, that’s exactly how you proceed towards storing your lovely little program forever so that you can hug your code every time you miss your ex right?

Git is a terrific way to store not just code but almost every kind of file you can think of that can be stored online. It employs altogether a different approach of controlling versions of your files. While other similar tools like SVN are Centralized Revision Control Systems (CRVS), Git employs Distributed Revision Control System(DRVCS). So, every person having access to your repository can clone your code and maintain a local copy of exactly the same data as in GitHub and can make changes locally with full control. So, in some catastrophical situation, god forbid, some client who cloned your code from Peru can help you to recover your data !!!

There can be nothing better than Git’s own website but I am here to help you skip some contrived details and dive right into basic usage of Git.

Basic Commands

        • git init – Initializes a local Git repository in your current local directory
        • git clone <remote_repository_name> – Clones a remote repository to your local git repository
        • git add <file_name1,file_name2...> or git add * or git add . – Stages (consider it analogically as a local buffer where you store files that are to be committed) your marked files or all for commit
        • git commit -m "<message>" – Commits the staged files with ‘message’ as commit message
        • git push <remote_name> <remote_branch_name> – Pushes your code to the remote named ‘origin’ and its branch ‘master’
        • git remote add <remote_name> <remote_repository_URL> – Adds a remote repository of given name and given URL
        • git remote -v – Displays all remotes with URLs
        • git checkout -b feature_x – Creates a new branch ‘feature_x’ and switches to the branch
        • git branch -a or git branch -r or git branch– Shows branches ; ‘r’ for remote only
        • git checkout -- <file_name> – Discards changes to file
        • git log – Gives commit history
        • git status – Gives status of staging area and working directory
        • git checkout <branch_name> – Moves the HEAD to the specified branch
        • git pull – Pulls code from remote repository’s tracker branch (default /master) to current local branch
        • git fetch origin – Fetches code first from Remote repository’s tracker branch to local branch without merging th code. Gives a chance to check the code before merging
        • git merge <branch_name> – Merges fetched code from specified branch

Note : I will be covering a topic on Basic Branching and Merging in GIT including Merge conflicts soon

Don’t play around with the commands below !!!

      • git reset --soft HEAD~ – Move HEAD to previous commit, Staging Area stays the same
      • git reset --mixed HEAD~ – Move HEAD to previous commit, Staging area also gets erased, Working Directory unaffected
      • git reset --hard HEAD~ – Move HEAD to previous commit, Staging area erased, Working directory moved to previous commit DANGEROUS !!!
      • git reset HEAD <file_name>– Unstages the specified file