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
View the submitted version number through git log, then git reset to undo commit
undo commit and add
first create a file
make a commit
Undo add and commit through git reset --mixed
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
1. Submit two versions
2. Go back to version v1, reset the version number you want to go to
fallback effect
V1 version after push rollback
Using git revert to perform version rollback will regenerate a version, and the previous useless version will also exist and record
1. Find the revoked version number
git revert version number, after entering the vi interface to modify the revert submission information: wq exit
Push directly after the modification is successful, or add–commit–push after making adjustments to the current version
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
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
Switch to the m2 branch, work on the m2 branch (fix bugs), and git checkout m2 after the work is completed and committed
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
2.2 How to get the code submitted by other branches?
You can get it by submitting the version number through cherry-pick
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
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)
The branch was merged successfully
In order to avoid the main branch fork, we can perform a rebase operation git rebase dev
After rebasing, the main branch becomes a straight line
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
In this way, there is no problem in deleting more than branches, because they have been merged into the main branch