SAGARFIVE

1. Introduction

2. Git Basics

3.  Git Terminology

4.  Git Branching

5.  Git - Github

6.  Git Add (Git staging)

7.  Git Commit

8.  Github Advanced

9.  Git Remote

4.3 Git checkout branch

  • The git checkout command is used to switch between branches in a repository.
  • git checkout switched to branch , then head points to latest commit in that branch
  • git checkout shows the files of particular checkout branch only in working tree

(i) Default branch is master on git:

  • After creating and initialising the project
  • If we run git log –oneline, the response retrieves your current branch is master

Observations:

  • In master branch, we did not created any files
  • So there we do not have commits

Creating files in master branch

touch Master-file-1
git add Master-file-1
git commit -m “m1”
touch Master-file-2
git add Master-file-2
git commit -m “m2”

(ii) Creating a First branch (Child branch) :

Taskcommandobservation
Creating branchgit branch feature-1Branch created
Move into that created branchgit checkout feature-1Moved into branch

Observations :

(i) After checkout, Head is points to selected/checkout branch

(ii) Files are copied from which branch you created(here we created from master)

(iii) if we check the log , the commits also copied to newly created branch

Advanced checkout observation:

(i) If we create a file in master and commited

(ii) next if we create multiple branches the files from master copies to newly created child branches

(iii) if we checkout branches and if we check the logs

(iv) **** we can see , HEAD pointed to all the branches , because the all branches has same file as latest file and latest commit(copied from master)

(v) if we checkout to one branch : git checkout feature-2

(vi) Added a file and committed :

touch feature-2-file-1 && git add . && git commit -m “feature-2-file-1-commit”

(vii) if we check the log : we can see HEAD is pointed to latest commit in with branch feature-2

ff04cb4 (HEAD -> feature-2) feature-2-file-1-commit
c239785 (master, ls, feature-3, feature-1) master-c-1

because HEAD always points to latest commit in the branch

the commit is latest in all branches so it is showing

git log shows commit history time lienier manner so we got feature-2 commit on top

(viii) If we checkout to another branch, git log shows us all commit from that branch,latest one on top and old one at last

and not showing feature-2 latest commit on master

so it won’t show other branch commits , if we checkout to one branch