“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
Create a local branch
Git branch Specifies the branch nameCopy the code
Using the Git branch command, you can quickly create a local branch that creates a pointer based on the currently committed object. This command creates only one branch and does not automatically switch to a new branch (in this case, we are in the Master branch).
HEAD is an identifier that points to the current branch
The branch switch
To switch branches, use the gIt checkout command
% git branch -a master * testingCopy the code
HEAD now points to the Testing branch
When we make changes to the Master branch and testing branch, let’s see what the branch branch looks like
You can see that the two branches have taken different paths from where they started
Merging branches
If the testing branch is your colleague’s branch and now needs to merge into the master branch and then push into the remote branch, use the merge command to merge
% git merge testing
Copy the code
This command will merge the testing branch into the current branch. My current branch is master. After merging, the codes of the two branches will be together.
If you don’t want the Testing branch after the merge, you can remove it
Delete the branch
git branch -d testing
Copy the code
How to merge in case of branch conflicts
If you have two branches that make changes to the same file, there will be conflicts when you merge one branch into the other
% git merge testing
Auto-merging test.md
CONFLICT (content): Merge conflict in test.md
Copy the code
Because both the Testing and master branches have made changes to the test.md file, a conflict occurs when the testing branch is merged into the master branch
Any files that have pending resolution because they contain merge conflicts are identified as unmerged. Git adds a standard resolution flag to files that have conflicts, so you can open those files and resolve them manually. Conflicting files contain special sections that look like this:
sssssTEST
<<<<<<< HEAD
ssss
ssssssssasasa
=======
sss
ssssss
>>>>>>> testing
Copy the code
This indicates that the version indicated by HEAD (which is where your master branch is, because you already checked it out when you ran the merge command) is in the upper part of the section (the upper part of =======), The version indicated by the Testing branch is in the lower part of =======. To resolve the conflict, you must choose to use one of the two sections divided by =======, or you can combine the contents yourself. For example, you can resolve the conflict by replacing the text with something like this:
sssssTEST
ssss
ssssssssasasa
ssssss
Copy the code
After you resolve conflicts in all files, use git add for each file to mark it as resolved. Once the conflicting files are temporarily saved, Git marks them as resolved.
Branch management
View all branches
% git branch
* master
test
Copy the code
All branches are displayed, with * representing the branch that is now checked out (that is, the branch to which the current HEAD pointer points).
See the last commit of each branch
% git branch -v
* master bc5b518 [ahead 5] sss
test 1be830c ss
Copy the code
View remote branches that correspond to all local branches
% git branch -vv
master d230c50 [origin/master: ahead 6] ss
* test 1be830c ss
Copy the code
In this case, you can see that the remote branch corresponding to the master is Origin or master, and test has not been associated with the remote branch
Set an existing local branch to track a remote branch
% git branch -u origin/master
% git branch -vv
master d230c50 [origin/master: ahead 6] ss
* test 1be830c [origin/master] ss
Copy the code
This command can be used to map the local branch to a remote branch, or to modify the remote branch
Deleting a Remote Branch
% git push origin --delete remote branch nameCopy the code