- Git file hierarchy is a way to understand , how files are handles in git branch creation , checkout
- From parent branch files and commits are copied to child branch
- The files created, commits are in child branch are not shown/copies to master branch
- if we need that changes in master branch we need to follow advanced commands like merge, cherrypick
- now, will see git basic branch creation flow with 1 master branch and two child branches
(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
(iv) Adding 2 files and committing :
Task | command | observation |
Adding first file | touch feature-1-file-1 | File created |
Adding to staging area | git add feature-1-file-1 | Added to staging |
Committing | git commit -m “F1” | Commit added |
Adding Second file | touch feature-1-file-2 | File created |
Adding to staging area | git add feature-1-file-2 | Added to staging |
Committing | git commit -m “F2” | Commit added |
(iii) Creating a Second branch (Child branch) :
Task | command | observation |
Creating branch | git branch -b feature-2 | Branch created |
Adding file 1 and committing | touch feature-2-file-1 && git add feature-2-file-1 && git commit -m “F3” | |
Adding file 2 and committing | touch feature-2-file-2 && git add feature-2-file-2 && git commit -m “F4” |
Observations :
(i) After checkout, Head is points to selected/checkout branch
(ii) Files are copied from which branch you created(here we created from feature-1)
(iii) if we check the log , the commits , files from parent branch(feature-1) copied to newly created child branch(feature-2)
(iv) Creating another file in master branch :
Task | command | observation |
Checkout to master | git checkout master | Moved to branch |
Add file | touch Master-file-3 && git add Master-file-3 && git commit -m “M3” | Created and committed |
Logs | git log –oneline | Commit history |
Observations:
(i) After checkout, Head is points to selected/checkout branch
(ii) Once checkout you can only see two files because in master, we created two only, that files only shows, the child branch files wont shows/added here
(example : child have rights to parents assets (so child branch(feature 1) copies parent branch(master) files and commits
(iii) After adding file, committing
(iv) if we check the log , the commits also copied to newly created branch
Here we can see graphical representation :