Create branch
$ git branch newBranch
Git has a special pointer called HEAD, and its pointing branch is the current branch.
After the branch is created, it will not move to the new branch, which requires us to move manually. Or add the -b parameter
$ git checkout newBranch to
view the current branch pointing to the object:
git log --oneline --decorate --graph --all
we create a new file in the newBranch branch: the
tree becomes like this:
merge
If we want the merged branch to be the master branch: execute the following command
git checkout master
git merge newBranch
The above picture shows the merge of the example, the master is the ancestor of newBranch, so the master only needs to move to newBranch:
Then if the master branch is also modified before the merge: the
same command after the merge: Connection merged into 6)
conflict
In the last merged case above, it is likely that the modification of the master and the modification of newBranch are in the same position, which will cause conflicts. When we execute the merge command, Git will pause, let us resolve the conflict
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview/solution (master)
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
After executing the merge command conflict, you can use to git status
view the conflict content:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview/solution (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)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview/solution (master|MERGING)
Check the test.txt at this time
to see the difference
resolution process:
vim test.txt
- Then remove
<<<<<HEAD
=======
<<<<<newBranch
- Modify as you wish
git add test.txt
git commit -m "merge complete!"
Remote branch operation
In order to avoid the need to enter the username and password every time you connect to the remote warehouse, we can perform a global configuration:, git config --global credential.helper cache
and then you can save user information for us within a few minutes
Clone the warehouse:
96916@DESKTOP-D7SKL2J MINGW64 ~/Desktop/GitReview (master)
$ git clone https://github.com/HLZ278/GitTest.git
Enter the cloned warehouse directory and check the branch:
we found that there is a orgin/master
branch, this branch is the master branch on the remote warehouse, origin / has no special meaning, Git clone will automatically name us, just to distinguish the master of the remote warehouse Branch, and we also found that git will also automatically create the local master branch pointing to the version pointed to by the remote branch. And we are currently on the master branch.
Then we try to submit a new file locally:
we found that only the local master branch was moved, and the remote master branch will not be moved. That is to say, as long as we pull or clone, the local warehouse is us and the remote warehouse. The final state of the communication. If someone modified something in the remote warehouse at this time, we would not know. Unless you re-pull the remote warehouse (pulling only pulls different data) git fetch <remote>
, we can perform operations such as merge after the pull. Draw a picture:
The picture above shows that we cloned the remote warehouse and submitted the update once locally.
If someone operates in the remote warehouse at this time,
then we will not know that
we want to fetch the new data:
How to submit the new data to the remote warehouse after modifying the new data locally?
Just rungit push <remote> <branch>
Rebase
Before rebase:
performing git rebase master newBranch
the variable group:
In a word: compare the previous commits of the current branch relative to the ancestor, extract the corresponding changes and save as temporary files, then point the current branch to the target base C3, and finally apply the changes previously saved as temporary files in order.
Pay attention to the usage of rebase, see the official documentation: https://www.git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%8F%98% E5% 9F% BA
Other branch management commands
View the current branch and version location:
git branch -v
View the merged branch
git branch --merged
View the unmerged branch
git branch --no-merged
Create a branch at a branch location
git checkout -b 新分支 已有分支
Delete the branch on the remote warehouse:
git push <remote> --delete <远程分支>