Interactive Rebase
Goal
- This project is about the interactive rebase.
Hints
- Try to solve the following tasks with the help of the slides and the Cheatsheets.
- If you still run into problems, you'll find an expandable hint block for every task with a detailed description of the solution.
Preparation
Search in Gitlab for the project interactive-rebase
and clone it:
-
Search in Gitlab in the left upper corner
Menu->Projects
for the projectinteractive-rebase
. If you don't find the project directly here, you can find it underYour projects
. -
Copy the link from
Clone with HTTPS
. -
Switch back to the terminal of the VSCode instance.
-
Ensure the current working directory is
/root/workspace
. Move upwards from previous repositories withcd ..
or alternatively it is possible to switch directly into the directory with:cd /root/workspace
. -
Clone the project with the command
git clone {URL}
.
Task 1
A developer has written a small hello-world python script in the branch feature-1
.
Your task is to get the changes of this feature-branch into the main
-branch.
For a better understanding later there is some cleanup needed in the branch.
So you will need to 'squash' all the commits in the branch to a single commit.
-
Switch to the branch
feature-1
. -
Look at the history of the branch to see what has been done.
-
Use the interactive rebase so there is only a single commit with one useful commit message left.
-
Check the history to see if the state of the branch is exactly how you want it to be.
-
Merge the branch
feature-1
into themain
-branch without fast-forward.
Solution (Click on the arrow if you are stuck)
- Part:
- Switch into the
interactive rebase
folder. - Use
git switch feature-1
to switch to this given branch.
- Switch into the
- Part:
- Look in the filebrowser. There is a new file
hello-world.py
. - Look at the history with
git log --graph
. - Look at the single commits with
git show HEAD
orgit show HEAD~1
andgit show HEAD~2
.
- Look in the filebrowser. There is a new file
- Part:
- The feature is containing 3 commits, which we want to get into a single commit.
Therefore run
git rebase -i HEAD~3
. - A list of commits will be shown. We now change the list so it looks like this:
Now the lower 2 commits will be integrated into the first commit.
pick cf3b080 Add file where the feature should be implemented squash b88e281 Adds hello-world functionality squash 1d6f112 Fit typo in hello-world output
- You will now be presented with a list of combined commit messages. Remove all lines that have not been commented out and insert a useful message.
- Save and close your commit message.
- The feature is containing 3 commits, which we want to get into a single commit.
Therefore run
- Part:
- Check the history to see if the state of the branch is now how you want it to be.
To do this use the command
git log --graph
.
- Check the history to see if the state of the branch is now how you want it to be.
To do this use the command
- Part:
- Run
git switch main
and thengit merge feature-1
to merge the changes.
- Run
Task 2
-
Use the command
git reflog
to undo your interactive rebase. Search in the reflog for the linerebase -i (start)
. This is where the rebase was started. The line underneath is the reference to the state before. -
Make a
git reset
to get to this state before the rebase.
Solution (Click on the arrow if you are stuck)
- Part:
- Run
git reflog
. - Search for the line where
rebase -i (start)
is stated. This is where the rebase has started. - The line underneath is the reference to the state before. Remember or copy the given refence (
HEAD@{??}
).
- Run
- Part:
- Run
git reset --hard HEAD@{??}
and substitute the??
with the number from the reflog. - Run
git log --graph
and you are now back in the state before the changes.
- Run
Task 3
If you have some spare time you can check out the feature-2
-branch and take a look at it.
This branch contains a small command line tool.
-
Switch to the branch
feature-2
. -
Create a new branch so you can easily jump back to this state if something goes wrong after a rebase.
-
Take a look at the history.
-
Try different variants of
rebase -i
.