Git principle and practice

During the work process, I found that many small partners do not have a deep understanding of git. During the operation, they often use some methods of deleting branches and file backups to ensure their own code. This article will explain the principle of git based on engineering practice. This article has been updated for a long time. Welcome to discuss and exchange.

One: git forced rollback

I accidentally submitted more code to the master branch, what should I do?

At this time, the remote code has been submitted to the remote warehouse, which requires a forced rollback.

There is no corresponding operation menu in idea.

Cause of:

Idea has uncommitted code when the test branch is switched to the master branch

Idea chose the smart mode to switch branches, which caused the code to be brought to the master branch locally and then accidentally committed.

Use the command to achieve:

1. Get the id of a certain historical version (ie change-id, unique for each version)

Method 1: Use the git log command to view all historical versions, and enter q to exit.

git log

Method 2: Use the gitk graphical interface to view node information. (When installing Git, you also installed the visualization tools it provides, gitk and git-gui.)

—>Assuming that the id of the historical version is 124bb0f757e661ef12cdbe99a805c156297d1f11

2. Locally restore to the state of the node

git reset --hard 124bb0f757e661ef12cdbe99a805c156297d1f11

3. Force push to remote branch

<At this time, if the branch is far away or there are many changes, the use of git push origin may report an error failure, and you can use force push at this time>

git push -f -u origin master

This operation requires a user name and password

Principle analysis: git version record can be understood as recording each operation node

No matter what kind of operation will be recorded in the git operation log.

2. Comparison of git revert and git reset

git revert cancels an operation, the commit and history before and after the operation will be retained, and the withdrawal is regarded
as the latest commit
* git revert HEAD cancels the previous commit
* git revert HEAD^ cancels the previous commit
* git Revert commit (for example: fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff) revokes the specified version, and the revoke will also be saved as a commit.
Git revert is to submit a new version. The content of the version that needs to be reverted will be modified back in the reverse direction. The
version will be incremented and will not affect the previously submitted content.


The difference between git revert and git reset

  1. Git revert uses a new commit to roll back the previous commit, and git reset directly deletes the specified commit.
  2. Looking at the operation of rollback, the effect is similar. But there is a difference when you continue to merge the old version in the future. Because git revert uses a reverse commit to "neutralize" the previous commit, so when the old branch is merged in the future, this part of the change will not appear again, but git reset deletes some commits on a certain branch. Therefore, when you merge with the old branch again, these rolled back commits should still be introduced.
  3. Git reset moves the HEAD back a bit, while git revert moves the HEAD forward, but the content of the new commit is the opposite of the content to be reverted, which can offset the content to be reverted.

Guess you like

Origin blog.csdn.net/keep_learn/article/details/107080290