A branch,

1. Brief introduction of branches

Simple use:



Can be
git branch new_branchand
git checkout new_branchTwo commands are combined into one command:
git checkout -b new_branch. This command means to create a branch and switch to it.


Local branch name changed:
Git branch -m Old branch name New branch name

2. Merge branches

During this process, the next version records a parent ID, which is the commit ID of the previous version.



Note that when we make changes to the file in dev, we must add and commit first, otherwise we will synchronize with the master content. What we need is that the master will not be the same as dev after the changes are made in dev, before the merge is performed.



3, Fast forward

Git reset HEAD test.txt git reset HEAD test.txt

  • HEAD refers to the current branch;
  • Master refers to the commit (essentially just the current commit);

That is, as follows:



If we execute git checkout -b dev, we create a dev branch and point to the new branch.

This is based on the master branch, but instead of making a copy as SVN does, it simply creates a pointer dev that points to the same commit as master. But HEAD points to dev (the current branch).



View the contents of the HEAD file:



Then I build on the above, and if I make a commit under the Dev branch, the graph looks like this:



At this point, master is pointing to commit 3, and Dev is pointing to commit 4.

Next, if I merge the changes above dev onto master (operating in master), the graph above will look like this:



There is no conflict in this situation.

Actual combat demonstration:



To summarize the situation, this is a direct jump from the master branch to the last dev change, which is the equivalent of a pointer jump.

4. Conflicts occur when master and dev modify the same file location at the same time

Test.txt (dev, master); test.txt (dev, dev);



The above process is like the following:



Note that the arrow points back because the last commit contains one
parent-idPoints to the previous submission
commit-idAs already mentioned.

Note the contents of the test.txt file in the master branch and how we will resolve this conflict (i.e. we intend to save the changes for master and discard the changes for dev) :



5, deep into the fast-forward mode

In other words, in fast-forward mode:

  • If it is possible (there are no conflicts when merging), Git will adopt itfast-forwardMode;
  • In this mode, branch information is discarded when a branch is deleted;
  • Add to merge-- no-ffParameter is disabledfast-forwardPattern, so there will be one morecommit - idThat is to say, infast-forwardUnder the modemergeThere won’t be one morecommit-id;
git merge -- no-ff devCopy the code

  • A better way to view logs
git log --graphCopy the code

In fast-Forward mode and non-fast-Forward mode, one less commit-ID and one more commID-ID are used.



In fast-forward mode, commit-id is the same as the other branch:



Fast-forward mode is not used:



2. Git version Rollback

Another great thing about Git is that you can go back to any previous version:



Look at the following commands:

  • git reset --hard HEAD^, back 1 version;
  • git reset --hard HEAD~3, back 3 versions;
  • git reset --hard commit-id, directly back to acommit-id; (If you are in the front, you can passgit logView);
  • If you’re in the back, you can’t see the frontGit log to get commit-idWhat to do? You can usegit reflogView your own operation logs.

Actual combat demonstration:



View change and commit logs:



Here is how to rollback:


Original: Java Architecture Notes

Free Advanced Java materials need to be collected by yourself, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo high concurrency distributed tutorials, a total of 30G.


Portal:
Mp.weixin.qq.com/s/JzddfH-7y…