Git rebase 教學
=> http://rugkiddtica.nnmcloud.ru/d?s=YToyOntzOjc6InJlZmVyZXIiO3M6MjE6Imh0dHA6Ly9iaXRiaW4uaXQyX2RsLyI7czozOiJrZXkiO3M6MTc6IkdpdCByZWJhc2Ug5pWZ5a24Ijt9
The existing branches are not changed in any way. Take the current one instead. The major benefit of rebasing is that you get a much cleaner project history. To begin an interactive rebasing session, pass the i option to the git rebase command: git checkout feature git rebase -i master This will open a text editor listing all of the commits that are about to be moved: pick 33d5b7a Message for commit 1 pick 9480b3d Message for commit 2 pick 5c67e61 Message for commit 3 This listing defines exactly what the branch will look like after the rebase is performed.
Reviewing a Feature With a Pull Request If you use pull requests as part of your code review process, you need to avoid using git rebase after creating the pull request. This results in a forked history, which should be familiar to anyone who has used Git as a collaboration tool. For example, think about what would happen if you rebased master onto your feature branch: The rebase moves all of the commits in master onto the tip of feature. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks.
By periodically performing an interactive rebase, you can make sure each commit in your feature is focused and meaningful. Still a lot to learn! But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. Any changes from other developers need to be incorporated with git merge instead of git rebase. The existing branches are not changed in any way. If you continue browsing the site, you agree to the use of cookies on this website. This is something that git merge simply cannot do. There is no git merge alternative for cleaning up local commits with an interactive rebase.
使用 git rebase 避免無謂的 merge - The golden rule of git rebase is to never use it on public branches. Interactive Rebasing Interactive rebasing gives you the opportunity to alter commits as they are moved to the new branch.
The git rebase command has a reputation for being magical Git voodoo that beginners should stay away from, but it can actually make life much easier for a development team when used with care. Conceptual Overview The first thing to understand about git rebase is that it solves the same problem as git merge. Both of these commands are designed to integrate changes from one branch into another branch—they just do it in very different ways. Consider what happens when you start working on a new feature in a dedicated branch, then another team member updates the master branch with new commits. This results in a forked history, which should be familiar to anyone who has used Git as a collaboration tool. To incorporate the new commits into your feature branch, you have two options: merging or rebasing. The existing branches are not changed in any way. This avoids all of the potential pitfalls of rebasing discussed below. On the other hand, this also means that the feature branch will have an extraneous merge commit every time you need to incorporate upstream changes. The Rebase Option As an alternative to merging, you can rebase the feature branch onto master branch git rebase 教學 the following commands: git checkout feature git rebase master This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk. But, there are two trade-offs for this pristine commit history: safety and traceability. Interactive Rebasing Interactive rebasing gives you the opportunity to alter commits as they are moved to the new branch. Typically, this is used to clean up a messy history before merging a feature branch into master. To begin an interactive rebasing session, pass the i option to the git rebase command: git checkout git rebase 教學 git rebase -i master This will open a text editor listing all of the commits that are about to be moved: pick 33d5b7a Message for commit 1 pick 9480b3d Message for commit 2 pick 5c67e61 Message for commit 3 This listing defines exactly what git rebase 教學 branch will look like after the rebase is performed. This is something that git merge simply cannot do. The Golden Rule of Rebasing Once you understand what rebasing is, the most important thing to learn is when not to do it. The golden rule of git rebase is to never use it on public branches. For example, think about what would happen if you rebased master onto your feature branch: The rebase moves all of the commits in master onto the tip git rebase 教學 feature. The problem is that this only happened in your repository. All of the other developers are still working with the original master. The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes the original ones, and the ones from your rebased branch. Needless to say, this is a very confusing situation. Force-Pushing If you try to push the rebased master branch back to a remote repository, Git will prevent you from doing so because it conflicts with the remote master branch. But, you can force the push to go through by passing the --force flag, like so: Be very careful with this command. Take the current one instead. Workflow Walkthrough Rebasing can be incorporated into your existing Git workflow as much or as little as your team is comfortable with. The first step in any workflow that leverages git rebase is to create a dedicated git rebase 教學 for each feature. This gives you the necessary branch structure to safely utilize rebasing: Local Cleanup One of the best ways to incorporate rebasing into your workflow is to clean up local, in-progress features. By periodically performing an interactive rebase, you can make sure each commit in your feature is focused and meaningful. This lets you write your code without worrying about breaking it up into isolated commits—you can fix it up after the fact. We saw an example of the first option in the Git rebase 教學 Rebasing section. The latter option is nice when you only need to fix up the last few commits. For example, the following command begins an interactive rebase of only the last 3 commits. Note that this will not incorporate upstream changes into the feature branch. If you want to re-write the entire feature using this method, the git merge-base command can be useful to find the original base of the feature branch. The only thing other developers will see is your finished product, which should be a clean, easy-to-follow feature branch history. But again, this only works for private feature branches. There is no git merge alternative for cleaning up local commits with an interactive rebase. Incorporating Upstream Changes Into a Feature In the Conceptual Overview section, we saw how a feature branch can incorporate upstream changes from master using either git merge or git rebase. Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of master. This use of git rebase is similar to a local cleanup and can be performed simultaneouslybut in the process it incorporates those upstream commits from master. This can git rebase 教學 when collaborating on the same feature with another developer and you need to incorporate their changes into your repository. By default, the git pull command performs a merge, but you can force it to integrate the remote branch with a rebase by passing it the --rebase option. Reviewing a Feature With a Pull Request If you use pull requests as part of your code review process, you need to avoid using git git rebase 教學 after creating the pull request. Re-writing its history will make it impossible for Git and your teammates to track any follow-up commits added to the feature. Any changes from other developers need to be incorporated with git merge instead of git rebase. Integrating an Approved Feature After a feature has been approved by your team, you have the option of rebasing the feature onto the tip of the master branch before using git merge to integrate the feature into the main code base. This also gives you the chance to squash any follow-up commits added during a pull request. If you would prefer a clean, linear history free of unnecessary merge commits, you should reach for git rebase instead of git merge when integrating changes from another branch. On the other hand, if you want to preserve the complete history of your project and avoid the risk of re-writing public commits, you can stick with git merge. Either option is perfectly valid, but at least now you have the option of leveraging the benefits of git rebase.
aster This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch. The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk. But, there are two trade-offs for this pristine commit history: safety and traceability. Interactive Rebasing Interactive rebasing gives you the opportunity to alter commits as they are moved to the new branch. Typically, this is used to clean up a messy history before merging a feature branch into master. To begin an interactive rebasing session, pass the i option to the git rebase command: git checkout git rebase 教學 git rebase -i master This will open a text editor listing all of the commits that are about to be moved: pick 33d5b7a Message for commit 1 pick 9480b3d Message for commit 2 pick 5c67e61 Message for commit 3 This listing defines exactly what git rebase 教學 branch will look like after the rebase is performed. This is something that git merge simply cannot do. The Golden Rule of Rebasing Once you understand what rebasing is, the most important thing to learn is when not to do it. The golden rule of git rebase is to never use it on public branches. For example, think about what would happen if you rebased master onto your feature branch: The rebase moves all of the commits in master onto the tip git rebase 教學 feature. The problem is that this only happened in your repository. All of the other developers are still working with the original master. The only way to synchronize the two master branches is to merge them back together, resulting in an extra merge commit and two sets of commits that contain the same changes the original ones, and the ones from your rebased branch. Needless to say, this is a very confusing situation. Force-Pushing If you try to push the rebased master branch back to a remote repository, Git will prevent you from doing so because it conflicts with the remote master branch. But, you can force the push to go through by passing the --force flag, like so: Be very careful with this command. Take the current one instead. Workflow Walkthrough Rebasing can be incorporated into your existing Git workflow as much or as little as your team is comfortable with. The first step in any workflow that leverages git rebase is to create a dedicated git rebase 教學 for each feature. This gives you the necessary branch structure to safely utilize rebasing: Local Cleanup One of the best ways to incorporate rebasi