[Git] Git Branch
Git Branch
- View Branch
- Create Branch
- Delete Branch
- Rename Branch
- Merge Branch
View Branch
# view existing branches
git branch
Create Branch
- It is important where you branch from
- could branch from master/main or another branch
- If there is a staged file when switching branches, it must be commited or stashed before switching
- If there is a untracked file when swithcing branches, it follows to the switched branch
# create new branch git branch {branch_name}
git branch test
# change to another branch git switch {branch_name}
git switch test
# make & switch with one go
git switch -c test
# git checkout does the same thing with million of additional things # git checkout {branch_name}
git checkout test
# make & switch with one go
git checkout -b test
Delete Branch
- Must be not on the branch to delete
# delete branch(after merged) git branch -d {branch_name}
git branch -d test
# delete branch by force git branch -D {branch_name}
git branch -D test
Rename Branch
- Must be on the branch to rename
# rename branch git branch -m {branch_name}
git branch -m main
Merge Branch
- Merging is done on branches, not specifc commits
- Merge is always done to the current HEAD branch
Fast-forward Merge
- There are two branch master & test branch
- test branch is based on master and master branch is behind few commits compared to test branch
- From git’s perspective, the pointer(HEAD) just has to catch up a few commits forward
- After merging the two branches are not synchronized
# switch to the destination branch(master branch) that needs merging
git switch master
# merge branch git merge {branch_name}
git merge test
Non Fast-foward Merge without conflict
- There are two branch master & test branch
- test branch is based on master and there is a new commit on master branch that test branch does not include(and there is no conflict, meaning that other changes were made to the same file)
# switch to the destination branch(master branch) that needs merging
git switch master
# merge branch(commit message pops up)
git merge test
Non Fast-foward Merge with conflict
- There are tree branch master, test1 and test2 branch
- test1 and test2 branch is based on master and on each branch there is a commit that made a different change to the same file(there is a conflcit)
# make a new branch to resolve & include the two branch
# from test2
git branch -c combo
# conflict is done when trying to merge
git merge test1
# open & edit the file(s) with merge conflicts
# have to remove the conflict markers(below) in the document
#<<<<<<< HEAD
#=======
#>>>>>>>
# can select among the four choices in VSC
# modifying is also possible
# add changes and make a commit
git add .
git commit