[Git] It is only a branch operation away from "three years of experience"

foreword

Yu Shengjun, the father of Java, once said that a programmer fan friend with "three years of development experience" was fired when he first joined the job because he couldn't solve the branch problem. Is this warning us?
For some uncommon operations of Git, let's demonstrate it through examples

1. Version rollback

1.1 Submitted but not pushed

Undo Commit
and commit once
insert image description here

View the submitted version number through git log, then git reset to undo commit
insert image description here
undo commit and add
first create a file
insert image description here

make a commit
insert image description here

Undo add and commit through git reset --mixed

insert image description here

1.2 Submit and push

Using git reset will completely erase the v4 version, and it cannot be queried in the log.
You can use this if you mistakenly commit to the main branch in the production environment
insert image description here

1. Submit two versions
insert image description here

2. Go back to version v1, reset the version number you want to go to
insert image description here

fallback effect

insert image description here

V1 version after push rollback
insert image description here

Using git revert to perform version rollback will regenerate a version, and the previous useless version will also exist and record
insert image description here

1. Find the revoked version number
insert image description here

git revert version number, after entering the vi interface to modify the revert submission information: wq exit
insert image description here

Push directly after the modification is successful, or add–commit–push after making adjustments to the current version

insert image description here

It can be seen that the old version still exists, and the generated "new version" is submitted, and the content is rolled back to v1
insert image description here

2. Branch operation

2.1 How to switch branches without losing the code of the previous branch?

Use git stash to temporarily store the code, and switch to this branch to release the code after the development is complete
insert image description here

Switch to the m2 branch, work on the m2 branch (fix bugs), and git checkout m2 after the work is completed and committed
insert image description here

Go back to the m1 branch and continue the previous development. After git checkout m1, the
temporary storage of the git stash pop branch is equivalent to a stacking operation. At this time, the record temporarily stored in the m1 branch is deleted, which is equivalent to popping the stack. Use git stash apply will not delete
insert image description here

2.2 How to get the code submitted by other branches?

You can get it by submitting the version number through cherry-pick
insert image description here

3. Branch problem

3.1 Compact branch

In project development, if the content of a module is committed multiple times, in order to make the branch more beautiful, we can compress the records of multiple submissions into one, and through getRebase, for example, three versions can be submitted, directly git rebase -i
HEAD ~3, press Enter
insert image description here
insert image description here

Of course, you can also use the shortcut keys that come with IDEA.

3.2 Merging branches

In actual scenarios, when other branches are to be merged into the main branch, just use git merge (commit push after resolving branch conflicts)
insert image description here

The branch was merged successfully
insert image description here

In order to avoid the main branch fork, we can perform a rebase operation git rebase dev
insert image description here

After rebasing, the main branch becomes a straight line
insert image description here

Then perform branch merging git merge, so that during the development process, the redundant branch has no intersection with the main branch, that is, it can be deleted directly
insert image description here
insert image description here

In this way, there is no problem in deleting more than branches, because they have been merged into the main branch

Guess you like

Origin blog.csdn.net/weixin_57535055/article/details/129095174