Hongmu Notes

Git undo command

Undoing can be divided into three situations: the changes have not been submitted, the submission has not been pushed, and the submission has been pushed. The methods are different.
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.

Undoing can be divided into three situations: the changes have not been submitted, the submission has not been pushed, and the submission has been pushed. The methods are different.

Changes have not been submitted yet

When the modifications in the workspace have not yet been submitted, you can directly restore a single file to the last submitted state, or withdraw the wrong file from the temporary storage area and select it again. This type of undo only affects local files, is the safest, and can be done at any time. The only thing to note is that restoring the file will overwrite the current edits with the last submitted content, and unsaved content cannot be recovered.

The submission has not been pushed yet

When a submission is written incorrectly, a file is missing, or you want to merge several submissions, you can freely rewrite the history because it has not been pushed yet. A common practice is to withdraw the submission to the pending submission state, modify it and resubmit it, or use patching to append to the original submission. The historical adjustments at this stage are completely local and will not affect anyone. It is the best time to organize submissions.

Submission has been pushed

The commit that has been pushed to the shared branch should not rewrite history, but add a new commit to offset it. In this way, when the collaborator pulls, there is only one more record, and there will be no historical inconsistency. If you really want to rewrite it, you must first confirm it with the owner and bear the cost of synchronization. Once the history of a shared branch is rewritten, troubleshooting becomes very difficult.

Retrieve accidentally deleted commits

After resetting, rebasing or deleting a branch, the commit does not disappear immediately, but there is no name pointing to it for the time being. You can check the operation record to find out which commit you were on at that time, and then redirect the branch back accordingly. This is also a general remedy for misoperation: retrieve the number first, and then let the pointer go back. It is recommended to write down the current commit number before taking action, which will save the trouble when remediating.

Do not rewrite the history of pushed submissions. Undo them by adding an offset submission.

📖 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

Will restoring the file overwrite the changes??
Yes. Restoring will overwrite the current file content with the last submitted content, and uncommitted edits cannot be retrieved.
Can accidentally deleted branches be restored??
Can. Check the operation record to find the commit number that the branch originally pointed to, and rebuild the branch accordingly.