GIT | branch
Article directory
Create a branch
Check which branches are currently in the local repository
git branch
The branch pointed to by HEAD is the branch currently being worked on.
cat .git/HEAD
Create a branch
git branch dev
Created, but currently still pointing tomaster
You can also use the tree command to see that the branch has been created successfully.
The branch created is the same as the latest record of the main branch
Switching branches means letting HEAD point to our dev branch.
git checkout dev
We made changes to the ReadMe file on the dev branch
Submit again
Switch back to the master branch at this time
View files
You can see that the newly added line of file is missing~~
Then let’s switch back to the dev branch and take a look.
I found that the newly added line is still there
We looked here and found that it has changed
Let’s check the records
The dev branch is the latest record, and the master branch is the second one
Our final effect is to see our effect on the master branch. How do we operate?
merge branches
This requires us to merge branches. Before merging branches, we need to switch to our master branch.
git merge dev
delete branch
This branch can only be deleted on other branches
git branch -d dev
merge conflicts
When merging branches, we have modified the files on the master branch, and I have also modified the files on the dev branch. Then an error will appear when merging. Let's demonstrate it.
Quickly create branches and enter branches
git checkout -b dev1
We changed the original aaa to bbb
Then submit it
We switch to the master branch to check the file content and find that it is still aaa because it has not been merged yet.
Next we will continue to change this aaa to ccc, and then merge the branches
If you merge again at this time, you will be prompted for merge conflicts.
git merge dev1
Let’s open the ReadMe file and check it out
At this time, you need to manually choose which codes to keep
Suppose we just keep this code
and then submit
Check if it is the latest commit
Visual viewing method
git log --graph --abbrev-commit
Branch management strategy
Not usedFast forward
Mode
Create a new branch
git checkout -b dev2
Modify the ReadMe file and submit it
Merge after switching back to the master branch
Not usedFast forward
Mode
git merge --no-ff -m "merge with no-ff" dev2
bug branch
Suppose we are currently developing on the dev2 branch. Halfway through the development, we suddenly find a bug on the master branch, which needs to
be resolved. In Git, every bug can be fixed by creating a new temporary branch. After fixing, merge the branches and then delete the temporary branch
- At this time, a bug appears in the master branch. At this time, it is necessary to switch to the master branch.
Then we don’t want this, we can do this
Save the contents of the workspace
git stash
After fixing the bug, we need to return to the dev2 branch to continue development.
git stash pop
Now that we are on the dev2 branch, we continue development
Then commit, a new commit was made on the dev branch
At this time, it is necessary to merge, but conflicts will occur during the merge. The master has just fixed the bug, and this time we need to merge the branches again. We need to solve the error.
We need not to merge branches on master, but merge master branches on dev, and solve the problem locally before taking the next step.
We merge on the dev2 branch
Manually modify conflicts
Then you can merge~~
Finally, don’t forget to delete the temporary branch and development branch you just created~~
Force deletion of branch
If during development, if the code has been developed on a branch and the code is submitted, it cannot be deleted using the traditional method at this time. We need to use -D
Delete~~
git branch -D dev3
- What is the practical use of branches? Suppose you plan to develop a new feature, but it takes two weeks to complete. You wrote 50% of the code in the first week. If you submit it immediately, because the code has not been written yet, the incomplete code base will prevent others from working. If you wait until all the code is written before submitting it, there is a huge risk of losing daily progress.
- Now that there are branches, there is no need to be afraid. You create a branch of your own. Others cannot see it. You continue to work normally on the original branch. You work on your own branch and submit it when you want. Until the development is completed, merge it into On the original support, it is safe and does not affect other people's work.
- And whether Git creates, switches, or deletes branches, Git can complete it within 1 second! Whether your repository is 1 file or 10,000 files.