Git rebase command
🌿 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.
Rebasing is to move a series of commits to a new starting point and replay them, in exchange for a straight line of history. The cost is that all the numbers of these commits are changed.
What exactly does rebasing do?
Rebase will find those unique commits on the current branch and reapply them in order on the latest commit of the target branch, so that the history changes from a fork to a straight line. The newly generated submission content is the same but the number is different, which can be understood as "the content has moved and the identity has changed." This is also the fundamental reason why it cannot be used for pushed commits.
Interactively organize submissions
Interactive mode lists pending commits, allowing you to reorder, merge, rewrite descriptions, and even delete a commit. It is very useful when organizing local history, such as merging scattered commits such as "write half of it" and "make up for it again" into a complete record. Organizing is only done locally, and it is safest to do it before pushing.
What to do in case of conflict
- If you encounter a conflict, follow the prompts to resolve the file. After the conflict is resolved, add it to the temporary storage area and let the rebase continue.
- A certain submission is obviously no longer needed, so you can skip it and continue with subsequent submissions.
- If you are not sure how to handle it, just give up and the warehouse will return to the state before the rebase.
When not to change your gender?
Do not rebase commits that have been pushed to the shared branch, otherwise collaborators will see the history being rewritten when pulling, and it is easy to have duplicate submissions or misdirected directions. The judgment criterion is simple: whether others may have pulled this commit locally. As long as the answer is "possible", use merge instead of rebase, or only operate on your own branch.
Do not rebase pushed commits, only organize the history on local unshared branches.
📖 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
❓ FAQ
- Will changes be lost when rebasing??
- Normal operation does not, the submission will be re-applied. It may be lost if the conflict is not handled properly or forced to quit midway. It is safer to back up first.
- Push was rejected after rebasing?
- Because the number has changed and is inconsistent with the remote. Personal branches can be forced to push, and shared branches should be synchronized by merging instead.