Introduction
Introduction (简介)
Introduction
Introduction (简介)
Git has different mechanisms for rewriting history, which include git commit –amend, git rebase and git reflog commands. Reading our tutorial will help you to better understand the functions of these commands and its particularities.
Changing the last commit with git commit – amend
Changing the last commit with git commit – amend
You can often forget to format your commit, stage files or make mistake in your commit log message. Here git commit –amend comes to help. Executing this command will help you to correct such minor mistakes. In general , this command is used for changing the last commits. Execute git commit –amend command to combine staged changes with the previous commit. It’s important to remember that for avoiding the collaboration problems, you shouldn’t amend commits which are being developed by other users.
Git reflog command
Git reflog command (Git reflog命令)
The “reflogs”, which stand for reference logs, are used for recording updates that are made to the tip of branches. With thegit reflog command, you can go back to all the commits, including the ones that haven’t been referenced by any branch. Git reflog command has its own subcommands, which include git reflog show, git reflog expire and git reflog delete.
Git rebase
Git rebase
The git rebase command is used for moving or combining a series of commits to an entirely new base commit. The most important benefit of this command is the clear history, which is important for an effective work on Git. There exist two modes of git rebase command: standard and interactive. In standard mode you can use git rebase to apply commits in the current working branch to the passed branch head. With the help of interactive mode you can maintain the project history clean. To run the interactive mode add the -i option to git rebase.
Multiple messages
Each of Git commits has a log message, which explains what happens in the commit. While rebasing run the following commands on the commits for editing commit messages. (每个Git提交都有一个日志消息,用于解释提交中发生的事情。在重新设定基础时,在提交上运行以下命令以编辑提交消息。)
Use “r” command, which stands for reword, to pauses the rebase playback and rewrite the individual commit message. (-使用“r”命令(代表重写)暂停重写播放并重写单个提交消息。)
“s” command, standing for squash, will pause all the commits marked s, and you will receive a prompt for modifying the separate commit messages into a combined one. (- “s”命令(代表squash )将暂停所有标记为s的提交,您将收到将单独的提交消息修改为组合的提交消息的提示。)
“f”, which stands for fixup, is similar to squash. The difference is that it will not stop rebase playback for opening an editor to combine commit messages. (- “f”代表固定,类似于壁球。不同之处在于,打开编辑器以合并提交消息不会停止重构播放。)
Squash commits for a clean history
The squash command specifies the commits that you want to merge into the previous ones. Here Git opens the text editor and prompts for combining the specific commit message. The following infographic perfectly demonstrates the process. (Squash命令指定要合并到以前的提交中的提交。在这里, Git打开文本编辑器并提示合并特定的提交消息。以下信息图表完美地演示了这一过程。)
The commits that have been modified with a git rebase command have different IDs than the original ones. If the previous commits are rewritten, the commits which are marked with pick will have an entirely new ID.