This page is under regular updates. Please check back later for more content.
Git & GitHub
Git Branches and HEAD

Git branches and HEAD

Most common operation

image

  • We have three git areas i.e., Working directory, Staging area and Git repository.
  • To move our files from working directory to staging area (index) we use git add command.
  • To move our files from staging area (index) to Git repository we use git commit command.

A commit consist multiple information such as - Author Name, Date, Message

  • To move between/certain "version" in our git repository we use git checkout command

File States in Git areas

image

  • When we add new file to working directory it becomes untracked (only exist in working directory area).
  • Once the file added in staging area it becomes staged
  • Once we make commit to that file it get into Git Repository.
  • Once we make new changes after commit the state turn into modified in all the three areas.

Branch

  • A branch is essentially a pointer to a specific commit in your project's history.
  • It's like creating a parallel timeline where you can make changes independently without affecting the main project.
  • Default branch is master
  • Multiple branches can exist in the same repository
  • Pointers for all branches are located in .git/refs/head folder
  • Current branch tracks new commits
  • Branch pointer moves automatically after every new commit
  • Change branch git checkout <branch_name>
  • Each branch has it's own isolated commit history
Git Branch
ubuntu $ git branch 
* master
ubuntu $ ls -l .git/refs/head
total 4
-rw-r--r-- 1 root root 41 Nov  3 16:44 master
ubuntu $ cat master 
beeaef1a2978c59e530723f7466da668f10243bd
ubuntu $ git log       
commit beeaef1a2978c59e530723f7466da668f10243bd (HEAD -> master)
Author: Xander <xanderbilla@gmail.com>
Date:   Sun Nov 3 16:44:14 2024 +0000

    First commit

HEAD

  • HEAD is used by branch to point the checkout commit or a branch
  • There will be only one HEAD
  • HEAD is locally significant
  • Pointer is located in the .git/HEAD file.
  • Default pointer is ref: refs/heads/master
  • Change reference to specific branch git checkout <branch>
  • Change reference to specific commit git checkout <sha1>