Merge request là gì và squash request vs rebase năm 2024

The git rebase command allows you to easily change a series of commits, modifying the history of your repository. You can reorder, edit, or squash commits together.

Typically, you would use git rebase to:

  • Edit previous commit messages
  • Combine multiple commits into one
  • Delete or revert commits that are no longer necessary

Warning: Because changing your commit history can make things difficult for everyone else using the repository, it's considered bad practice to rebase commits when you've already pushed to a repository. To learn how to safely rebase on GitHub.com, see "About pull request merges."

To rebase all the commits between another branch and the current branch state, you can enter the following command in your shell [either the command prompt for Windows, or the terminal for Mac and Linux]:

git rebase --interactive OTHER-BRANCH-NAME

To rebase the last few commits in your current branch, you can enter the following command in your shell:

git rebase --interactive HEAD~7

There are six commands available while rebasing:

pick pick simply means that the commit is included. Rearranging the order of the pick commands changes the order of the commits when the rebase is underway. If you choose not to include a commit, you should delete the entire line. reword`The `reword command is similar to pick, but after you use it, the rebase process will pause and give you a chance to alter the commit message. Any changes made by the commit are not affected.

git rebase --interactive HEAD~7

1 If you choose to

git rebase --interactive HEAD~7

1 a commit, you'll be given the chance to amend the commit, meaning that you can add or change the commit entirely. You can also make more commits before you continue the rebase. This allows you to split a large commit into smaller ones, or, remove erroneous changes made in a commit.

git rebase --interactive HEAD~7

3 This command lets you combine two or more commits into a single commit. A commit is squashed into the commit above it. Git gives you the chance to write a new commit message describing both changes.

git rebase --interactive HEAD~7

4 This is similar to

git rebase --interactive HEAD~7

3, but the commit to be merged has its message discarded. The commit is simply merged into the commit above it, and the earlier commit's message is used to describe both changes.

git rebase --interactive HEAD~7

6 This lets you run arbitrary shell commands against a commit.

No matter which command you use, Git will launch your default text editor and open a file that details the commits in the range you've chosen. That file looks something like this:

Thay vì thêm commits 3 và 4 với commit “Merge branch feature-branch”, chúng ta gộp 3 và 4 và kết quả cho ra duy nhất commit gộp là 3**[squashed]**.

Chúng ta cùng quan sát điều gì xảy ra trong Insights -> Network:

Tùy chọn squash trên Github UI cho phép chúng ta thay đổi các thông điệp commit mặc định. Các commit dưới đây được “squash” dưới dạng thông điệp mặc định như sau:

Nếu bạn muốn sử dụng nó , đừng quên edit các thông điệp !

Chúng ta sử dụng tùy chọn merge này khi 1 feature branch có rất nhiều các commit nhỏ được add. Khi đó, chúng ta có thể nén chúng vào 1 commit. Commit nén có thể chứa một thông điệp với các thông tin mô tả về commit được nén dưới nó.

3. Rebase and Merge

Hình bên dưới là tùy chọn “Rebase and merge”:

Rebase the commits individual onto the base branch [tập hợp tất cả những commit riêng lẻ vào trong một branch cơ sở] Trong trường hợp chỉ cần sửa đổi một số lượng nhỏ commit cũng như một đoạn code cụ thể thì rebase and merge sẽ là lựa chọn hàng đầu. Cách làm này sẽ làm cho quá trình merge được đơn giản cũng như giữ lịch sử Git gọn gàng hơn. Ta cần tạo 2 commit trên nhánh “feature-branch” thêm commits 1 và 2. Rebase and Merge sẽ chuyển các commits lên đầu nhánh chính master branch - thay đổi HEAD của master branch mà không merge “feature-branch” như khi sử dụng Merge pull request:

Chúng ta cùng quan sát điều gì xảy ra trong Insights -> Network:

Kết Luận

Github cung cấp cho ta rất nhiều chức năng để quản lý dự án một cách hiệu quả. Do vậy bạn cần hiểu rõ bản chất của mỗi chức năng để có thể quản lý dự án của chúng ta được tốt hơn.

Nếu như bạn muốn quản lý được tất cả những commit ID trong những branch sau khi chúng đã được merge/xóa bản phải dùng Create a merge commit. Mặt khác bạn làm một dự án mã nguồn mở với những người đóng góp thì Squash and merge là tốt nhất. Còn bạn làm một private repo mà cần kiểm soát những nhân viên thì Rebase and merge là một lựa chọn tốt tuy nhiên Squash and merge vẫn hoạt động ổn.

Chủ Đề