I usually submit a list of commits for review. If I have the following commits:
HEAD
Commit3
Commit2
The best option is to use "Interactive rebase command".
The
git rebase
command is incredibly powerful. It allows you to edit commit messages, combine commits, reorder them ...etc.Every time you rebase a commit a new SHA will be created for each commit regardless of the content will be changed or not! You should be careful when to use this command cause it may have drastic implications especially if you work in collaboration with other developers. They may start working with your commit while you're rebasing some. After you force to push the commits they will be out of sync and you may find out later in a messy situation. So be careful!
It's recommended to create a
backup
branch before rebasing so whenever you find things out of control you can return back to the previous state.
git rebase -i
-i
stand for "interactive". Note that you can perform a rebase in non-interactive mode. ex:
#interactivly rebase the n commits from the current position, n is a given number(2,3 ...etc)
git rebase -i HEAD~n
HEAD
indicates your current location(can be also branch name or commit SHA). The ~n
means "n beforeé, so HEAD~n
will be the list of "n" commits before the one you are currently on.
git rebase
has different command like:
p
or pick
to keep commit as it is.r
or reword
: to keep the commit's content but alter the commit message. s
or squash
: to combine this commit's changes into the previous commit(the commit above it in the list).... etc.
Note: It's better to get Git working with your code editor to make things simpler. Like for example if you use visual code you can add like this git config --global core.editor "code --wait"
. Or you can search in Google how to associate you preferred your code editor with GIT.
git rebase
I wanted to change the last 2 commits I did so I process like this:
#This to show all the commits on one line
$git log --oneline
4f3d0c8 (HEAD -> documentation) docs: Add project description and included files"
4d95e08 docs: Add created date and project title"
eaf7978 (origin/master , origin/HEAD, master) Inital commit
46a5819 Create README.md
Now I use git rebase
to change the 2 last commits messages:
$git rebase -i HEAD~2
It opens the code editor and show this:
pick 4d95e08 docs: Add created date and project title
pick 4f3d0c8 docs: Add project description and included files
# Rebase eaf7978..4f3d0c8 onto eaf7978 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
...
Since I want to change the commit message for this 2 commits. So I will type r
or reword
in place of pick
. Then Save the file and close the tab.
Note that rebase
is executed in a multi-step process so the next step is to update the messages. Note also that the commits are displayed in reverse chronological order so the last commit is displayed in that one and the first commit in the first line and so forth.
Update the messages: Update the first message:
docs: Add created date and project title to the documentation "README.md"
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
...
save and close Edit the second message
docs: Add project description and included files to the documentation "README.md"
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
...
save and close.
You will get a message like this by the end of the rebase: Successfully rebased and updated refs/heads/documentation
which means that you succeed. You can display the changes:
5dff827 (HEAD -> documentation) docs: Add project description and included files to the documentation "README.md"
4585c68 docs: Add created date and project title to the documentation "README.md"
eaf7978 (origin/master, origin/HEAD, master) Inital commit
46a5819 Create README.md
I wish that may help the new users :).