[Git 2] Level 1. Rebase

REBASE RECAP

So you've been working on your feature branch for a couple days and you realize you need to bring commits back in from master. Now you could just merge in the branch but that would create an ugly merge commit. Use rebase to bring those changes into your branch without the merge commit.

git rebase master

 

INTERACTIVE REBASE

You're getting ready to merge in your feature branch, but first you want to clean up your commits a bit. Go ahead and use interactive rebase to edit your last 4 commits.

git rebase -i HEAD~4

 

COMMIT ORDERING

You notice a critical error on your part. The commit where you wrote tests comes after the commit where you made the actual changes. Let's swap these 2 commits so that it looks like you wrote the tests first. Swap the 'Add tests' commit so that it comes before the 'Implement poodles' commit.

复制代码
pick b3f1649 Add unicorn
pick f239187 Implement poodles
pick 4b65a5a Add tests
pick c3f863f Added a title to the homepage

# Rebase b3f1649..c3f863f onto b3f1649
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
复制代码

Answer:

pick b3f1649 Add unicorn
pick 4b65a5a Add tests
pick f239187 Implement poodles
pick c3f863f Added a title to the homepage

 

REWORDING A COMMIT I

As you're editing your commits, you notice that the commit that says 'Add tests' is a little vague, maybe you should change it to something more specific. Use the reword command to change the message of this commit.

pick b3f1649 Add unicorn
pick 4b65a5a Add tests
pick f239187 Implement poodles
pick c3f863f Added a title to the homepage

Answer:

pick b3f1649 Add unicorn
reword 4b65a5a Add tests
pick f239187 Implement poodles
pick c3f863f Added a title to the homepage

 

REWORDING A COMMIT II

After setting the 'reword' command in the interactive rebase editor another editor window has opened up. This is where you actually change the commit message. Go ahead and change the commit message to: Add tests for poodles to be more descriptive.

Add tests
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD^1 <file>..." to unstage)
#
#       new file:   poodle_spec.txt

Answer:

Add tests for poodles

 

SPLIT COMMITS I

You remember that the 'Add unicorn' commit also contains the testing changes. Since we have separate commits for adding tests for poodles it makes sense to split out the test changes into a separate commit. Tell git to stop at this commit so we can reorganize it a bit.

复制代码
pick b3f1649 Add unicorn
pick 4b65a5a Added tests for poodles
pick f239187 Implement poodles
pick c3f863f Added a title to the homepage

# Rebase b3f1649..c3f863f onto b3f1649
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
复制代码

Answer:

edit b3f1649 Add unicorn
pick 4b65a5a Added tests for poodles
pick f239187 Implement poodles
pick c3f863f Added a title to the homepage

 

SPLIT COMMITS II

After saving the interactive rebase text file, git has stopped at the 'Add unicorn' commit. The first thing we need to do is to reset back to the previous commit, so the changes in the most recent commit are back in the file and unstaged.

git reset HEAD^

 

SPLIT COMMITS III

Great! You've already re-committed the changes needed in 2 different commits. All that's left to do is to resume the rebase with the continue option.

git rebase --continue

 

SQUASHING COMMITS I

You noticed another bug with those pesky poodles, so you've gone ahead and fixed it, making another commit in the process. Now you have 2 commits that both affect the poodles page. You should squash them into one commit to simplify your git logs.

复制代码
pick b3f1649 Add unicorn
pick 4b65a5a Add tests
pick f239187 Implement poodles
pick c3f863f Add title to poodle page

# Rebase b3f1649..c3f863f onto b3f1649
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
复制代码

Answer:

pick b3f1649 Add unicorn
pick 4b65a5a Add tests
pick f239187 Implement poodles
squash c3f863f Add title to poodle page

 

SQUASHING COMMITS II

After designating a commit to be squashed, another editor pops up enabling you to set a commit message for the resulting commit. Go ahead and set to: squashed some poodles.

复制代码
# This is a combination of 2 commits.
# The first commit's message is:

squashed some poodles

# This is the 2nd commit message:

Add title to poodle page

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   poodles.txt
#       new file:   unicorns.txt
复制代码

 

posted @   Zhentiw  阅读(466)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示