Dec 24, 2014



Usually when you work on a specific issue or a new feature you could end up with a lot of local commits in order to track your progress as you try different things (even have multiple local branches). At the end of your day you have a final solution that you feel confortable with and you want to push that into master.


But when you list your commits, git log --oneline --decorate -10 there might be many unnecessary commits. You probably do not want all that commit history in your working branch as that would blur your view on what was really done.
In that case one option is to merge the local commits and make a new set of commits that truly describe your work.


After some investigation, I found this StackOverflow question: how to merge local commits at a develop branch in git
Here is what has worked for me:

git checkout mybranch
git merge-base mybranch origin/master
git reset commitId
git commit -m "merge commits"
git merge master mybranch
git push
Where the commitId is something like ac8fbef. This is the commit id of the most recent commit before you start your work. To get this id you probably want to list the last x commits: git log --oneline --decorate -10
So if I make 4 commits:
git commit -m "start work on foo"
git commit -m "new method"
git commit -m "refactor"
git commit -m "changing foo to be foobar instead"
And my git log would look like this:
git log --oneline --decorate -5
80a16bc changing foo to be foobar instead
987589e refactor
16d224d new method
2246fc6 start work on foo
ac8fbef fixes error on bar
In this case I want to make a git reset ac8fbef. This will put all the changes that were done on mybranch into the modified files and remove the commits I made locally.


If you do a git status, you will see all the files that you changed during your day of work (during the 4 commits I took as examples). You can now split those changes into separate and concise commits and push that into master or any branch you are working on. Your commit history is clean and does not have unnecessary commits!

Santiaago