- 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) :
Task | command | observation |
Creating branch | git branch feature-1 | Branch created |
Move into that created branch | git checkout feature-1 | Moved 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