bodokaiser bodokaiser - 4 years ago 184
Git Question

How to use git rebase properly?

I currently use git the following way:


  1. clone, fetch repo

  2. create topic branch for specific issue

  3. do some commits on it

  4. checkout master branch

  5. merge topic branch to master

  6. push new master branch



Unfortunately this is getting messy from time to time. What I would like to do is to merge some smaller commits to a bigger one.

Also it would be sometimes a good idea to merge all commits in a new branch to one commit and merge this to master.

example:

git branch update-docs
git checkout update-docs
git add -A .
git commit -m 'updated networking doc'
git add -A .
git commit -m 'updated manager doc'
git checkout master
git merge update-docs


As you know now I would have two commits in master branch "updated networking doc" and "updated manager doc".

If I would now want only have one commit in master branch called "updated docs" for example which contains the changes of these two commits what do I have to do?

I think you have to use git rebase for this sort of task. I just do not know how to use it.

I already tried:

git checkout master
git rebase update-docs


but that merged the changes to master without leaving any commits and now the repo is messed up.

So would somebody share the commands you have to use?

Solution:

git checkout -b update-readme
git commit -am 'rewritten readme'
git commit -am 'added author name'
git rebase -i master


opens text editor:

pick 6dbcbf1 rewritten readme
pick b815bbf added author name


now you change this to:

pick 6dbcbf1 rewritten readme
squash b815bbf added author name


then you save with ":w" and ":q" if using vim and it will open another editor where you can define the new commit name

rewritten readme


then you merge the repo:

git checkout master
git merge update-readme

Answer Source

After git merge update-docs, execute git rebase -i origin/master. This will pop up an interactive rebase editor, which allows you to squash commits and otherwise rearrange them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download