Please generate a root-commit to proceed with Git branches.
-
Create branch
(master) $ git branch {branch_name} -
Switch branch
(master) $ git checkout {branch_name} -
Create and switch branch
(master) $ git checkout -b {branch_name} -
Delete branch
(master) $ git branch -d {branch_name} -
List branches
(master) $ git branch
-
Merge branch
(master) $ git merge {branch_name}
- Merge {branch_name} from master branch
Branch-related commands are simple.
You should be able to understand what situation you're in and use them freely in various scenarios.
Fast-forward is a situation where there are no changes to the master branch after the feature branch is created
-
Create and switch to feature/crud branch
git branch feature/crud git checkout feature/crud
- or
git checkout -b feature/crud
-
Commit after completing work
- Create arbitrary files and commit
add,commit
-
Switch to master
(feature/crud) $ git checkout master Switched to branch 'master' (master) $ -
Merge to master
$ git merge feature/crud Updating d66ff92..f219c8c # Fast-forward!!! Fast-forward crud.html | 0 urls.py | 1 + views.py | 1 + 3 files changed, 2 insertions(+) create mode 100644 crud.html create mode 100644 urls.py create mode 100644 views.py
-
Result -> fast-forward
$ git log --oneline f219c8c (HEAD -> master, feature/crud) Complete CRUD) d66ff92 Init -
Delete branch
git branch -d feature/crud
In the process of merging different histories (commits), different files are modified
Git performs auto merging and a commit occurs.
-
Create and switch to feature/signout branch
(master) $ git checkout -b feature/signout Switched to a new branch 'feature/signout' (feature/signout) $ -
Commit after completing work
touch signout.txt git add . git commit -m 'Complete'
-
Switch to master
(feature/signout) $ git checkout master Switched to branch 'master' (master) $ -
Generate additional commits on master!!
- Please modify or create different files!
-
Merge to master
$ git merge feature/signout Merge made by the 'recursive' strategy. signout.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 signout.txt
-
Result -> merge commit automatically occurs
-
Check graph
$ git log --oneline --graph * 2125d5b (HEAD -> master) Merge branch 'feature/signout' |\ | * fb74094 (feature/signout) Complete signout * | 75f0abd README @ master |/ * f219c8c Complete CRUD) * d66ff92 Init
$ git log -1 --oneline 2125d5b (HEAD -> master) Merge branch 'feature/signout'
-
Delete branch
$ git branch -d feature/signout Deleted branch feature/signout (was fb74094).
In the process of merging different histories (commits), the same part of the same file is modified
Git cannot perform auto merging and shows a conflict message.
It marks the location in the file according to standard format.
You must manually fix it to the desired form of code and manually create a commit.
-
Create and switch to feature/signup branch
(master) $ git checkout -b feature/signup Switched to a new branch 'feature/signup' -
Commit after completing work
add,commit
-
Switch to master
(feature/signup) $ git checkout master Switched to branch 'master' -
Generate additional commits on master!!
- Please modify or create the same file!
$ git log --oneline
3c81134 (HEAD -> master) Hotfix signout urls.py
2125d5b Merge branch 'feature/signout'
75f0abd README @ master
fb74094 Complete signout
f219c8c Complete CRUD)
d66ff92 Init-
Merge to master
(master) $ git merge feature/signup Auto-merging urls.py CONFLICT (content): Merge conflict in urls.py Automatic merge failed; fix conflicts and then commit the result. (master|MERGING)
-
Result -> merge conflict occurs
You can check conflicted files with git status command.
(master|MERGING) $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Changes to be committed: new file: signup.txt Unmerged paths: (use "git add <file>..." to mark resolution) both modified: urls.py
-
Check and resolve conflicts
<<<<<<< HEAD # signout ======= # signup >>>>>>> feature/signup -
Proceed with merge commit
git commit
-
Vim editor screen will appear.
-
Check the automatically written commit message, press
escand type:wqto save and exit.w: writeq: quit
-
Let's check the commit.
-
-
Check graph
(master) $ git log --oneline --graph * 4e55ba7 (HEAD -> master) Merge branch 'feature/signup' |\ | * fc7f0d8 (feature/signup) Complete signup * | 3c81134 Hotfix signout urls.py |/ * 2125d5b Merge branch 'feature/signout' |\ | * fb74094 Complete signout * | 75f0abd README @ master |/ * f219c8c Complete CRUD) * d66ff92 Init
-
Delete branch
git branch -d feature/signup