Hongmu Notes

Git reset command

Reset moves the branch pointer. The only difference between the three modes is whether to change the temporary storage area and work area incidentally.
100% Browser local processing 0 Data upload

🌿 Command assembly

💡 Instructions for use

  • The branch name and remote name must be written correctly. Misspelled commands often do not report an error but take effect on other branches.
  • Commands involving reset, force push, and rebase will rewrite history. Please confirm the branch and target before execution.
  • Check Bind Upstream when pushing a new branch for the first time. You can omit the remote and branch names for subsequent pushes and pulls.
  • The command is output in a single line, making it easy to paste directly into the terminal; it is recommended to save the steps that need to be repeated into a script.

Reset moves the branch pointer. The only difference between the three modes is whether to change the temporary storage area and work area incidentally.

Three modes of division of labor

  • Soft mode: Only the pointer is moved, the temporary storage area and work area are retained as they are, and changes are still pending submission.
  • Mixed mode: Move the pointer and clear the temporary storage area. The file content in the work area remains unchanged, but the temporary storage area needs to be re-added.
  • Hard mode: The pointer, temporary storage area, and work area are rolled back together, and uncommitted modifications will be discarded directly.

How to choose mode

If you want to change several submissions into one and resubmit, use soft mode to retain the content and submit again; if you want to remove the files from the temporary storage area and re-select them, use mixed mode; if you want to completely discard all local changes and be completely consistent with a certain historical version, use hard mode. In most cases the first two are safer because the changes are still there and can be reverted at any time.

How to remedy misuse of hard mode

Hard mode discards only uncommitted content, and submitted changes can still be retrieved through the commit record: find the commit number before discarding, move the branch pointer back, or create a new branch on the original number. This is why it is recommended to submit it once before taking action. Even if the message is written casually, it is better than losing it completely.

Differences from restore operations

Reset is to change the history, and the pointer moves back, which is suitable for local commits that have not been pushed; restore is to add a new commit to offset the changes of a certain commit, and the history is kept intact, which is suitable for branches that have been pushed. Use restore when rolling back functions on shared branches, and reset when cleaning up locally, so as not to disrupt the history of collaborators.

Hard mode will discard uncommitted changes and commit or save them once before proceeding.

📖 Glossary

workspace
The directory where the file being edited is located. The modified content that has not yet been registered only exists in the workspace and may be overwritten by any rollback, so important changes must be submitted first.
Staging area
A preparation area between the work area and the warehouse. Submissions only record the contents of the staging area, so a batch of changes can be split into several submissions, or only part of them can be submitted.
Submit
Solidify the contents of the temporary storage area into a historical node, with description, author and time. Once a submission is generated, it has a unique number, which is used as the minimum unit for rollback and tracing.
branch
A movable pointer to a commit, used for parallel development of changes that do not affect each other. A new branch takes up almost no space. Switching branches means that the work area and temporary storage area follow the pointer.
head pointer
A pointer that marks the current location, usually pointing to a branch. If it points directly to a commit and is not on a branch, it is a detached head state, and the new commit at this time will not update any branches.
merge
Merge the history of another branch into the current branch to generate a new merge node. If the two branches do not diverge, they will be fast forwarded directly and no merge node will be generated.
Rebase
Move a series of commits on the current branch to another branch and replay, so the history becomes a straight line. Do not rebase the submitted commits that have been pushed out, otherwise the local history of the collaborators will be inconsistent.
reset
Move the current branch pointer to the specified commit. The three modes respectively determine whether to modify the staging area and the work area at the same time. The mode of changing the work area will directly discard uncommitted modifications.
Remote warehouse
A copy of the same repository located on the server, referenced locally by a name. You can change the address or name of the same remote. Both push and pull are addressed by name.
upstream branch
Establish a corresponding relationship between the local branch and the remote branch. You can omit the remote name and branch name when pushing and pulling after binding, and you can also directly see how many commits are ahead or behind.
label
A fixed name given to a commit, often used to mark versions. Tags with descriptions will generate an additional object containing the author and time. This is recommended for release versions.
storage
Put away unfinished changes temporarily to return the workspace to a clean state and retrieve them when needed. It is suitable for situations where you are called to fix an urgent problem midway but do not want to submit it hastily.

🔗 All keywords and tutorials

Keywords page of the tool12a

Return to tool home page

❓ FAQ

Can the changes be recovered after resetting??
Content that has been submitted can be retrieved with the submission number; changes that have never been submitted cannot be recovered, so it is safer to submit first.
Can shared branches be reset??
Not recommended. When rolling back on a shared branch, you should use the restore operation to add new commits to avoid rewriting the history that others have already pulled.