Summary of using version control git in personal work

Summary of git usage

1. The basic steps of code modification at work

1. Before submitting the code and switching branches, you must first pull and submit it in the switching branch (because if you do not pull the latest code, if someone else modifies the content of the file you modified, you will have a conflict when you submit it).

2. Create a new branch after the code is modified, and execute the add, commit, and push operations in the newly created branch

3. View the local commit record
git log

4. View all local operation records
git reflog

5. Delete the remote branch
git branch -a View the remote branch
git push origin --delete (deleted branch)

2. How to deal with the situation in the project

1. How to solve the conflict between the pull operation and the local changes?

The first method: resolve conflicts locally, and determine whether to use the remote version or the locally modified version.

The second: undo the pull operation or commit operation ( using reset )

Method 1:
git reset --hard HEAD^ (or HEAD~1) undoes the operation, restores the code to the last submitted version, and local modifications will not be saved! ! (It is recommended to use the following two methods for pull after local modification)

Method 2:
git reset --soft HEAD^ (or HEAD~1), undo the operation, restore the code to the last submitted version, restore to the location of git add, the contents of the temporary storage area are saved,

Method 3:
git reset --mixd HEAD^ (or HEAD~1), undo the operation, restore the code to the last submitted version, and the modified file has not been added

2. If the file is added to the temporary storage area, the undo will not be added to the temporary storage area?
git reset HEAD file name revokes the specified file from the temporary storage area

3. How to deal with code problems after push?
If you encounter this kind of problem, if you merge remotely, first revert, then reset the local, and then add commit push a version

4. Reason for error handling after reset
:
reset deletes all versions after the specified version, causing the local point to be older than the remote point
to solve: add -f to push
git push -f

Three, git version management, and the understanding of HEAD

For each commit using git, Git will automatically string them into a timeline, which is a branch. If there is no new branch, then there is only one timeline, that is, only one branch. In Git, this branch is called the main branch, that is, the master branch. There is a HEAD pointer pointing to the current branch (if there is only one branch, it will point to master, and master points to the latest commit). Each version will have its own version information, such as unique version number, version name, etc. As shown in the figure below, suppose there is only one branch:
xlsk

4. Explanation of the difference between reset and revert

git reset

Principle: The function of git reset is to modify the position of HEAD, that is, change the position pointed by HEAD to a previous version, as shown in the figure below, assuming we want to roll back to version 1: write the picture description
here

insert image description here

Applicable scenario: If you want to revert to a previously submitted version, and we don't want any versions submitted after that version, you can use this method.

git revert

Principle: git revert is used to "reverse" a certain version, so as to achieve the purpose of undoing the modification of this version. For example, we have committed three versions (version 1, version 2, and
version 3), and suddenly found that version 2 does not work (for example: there is a bug), and want to cancel version 2, but do not want to affect the submission of version 3, you can use The git revert
command is used to reverse version 2 and generate a new version 4. In this version 4, the things of version 3 will be retained, but the things of version 2 will be revoked. As shown below:

insert image description here

Applicable scenario: If we want to revoke a previous version, but want to keep the version after the target version, and record the entire version change process, we can use this method

Summary:
The reset operation will roll back the version to the specified branch. After the rollback, all versions behind the specified branch will be deleted. Revert is to redo a version and generate a new version on the rolled back version.

Replenish:

1. How to restore the workspace file to the state before modification

2. Restore and modify a single file
git checkout [filepath]

3. Restore and modify all files
git checkout .

4. Roll back the add file
git reset HEAD [file name]

Guess you like

Origin blog.csdn.net/wei1359765074410/article/details/122070377