Search code examples
gitsquash

Squashing in git after many commits


I am naive in github ,I have been trying to squash the commits i have done previously on remote .But there are two problems 1)Other contributors are contributing continuously ,hence there are many commits above my commit 2)My commits are not seen in order on git hub.In the snapshot ,it is the four commits I have to merge enter image description hereAny help will be appreciable .


Solution

  • I would recommend always working on a local git branch that is not tracking an upstream branch. That is, when beginning new work on a remote branch named gh-pages, create two local branches, as follows:

    # Create local gh-pages branch to track upstream
    $ git co -b gh-pages origin/gh-pages
    # Create personal branch for changes to be pushed to gh-pages
    $ git co -b aayusharora-gh-pages
    

    As new commits to gh-pages are pushed to upstream, pull them into your local gh-pages branch, and then rebase them on top of your aayusharora-gh-pages branch. That is, periodically do this:

    # Check for new changes upstream
    $ git co gh-pages
    $ git pull
    # Rebase your changes on top of any upstream changes
    $ git co aayusharora-gh-pages
    $ git rebase gh-pages
    

    This will ensure that your work is always 'on top of' any upstream changes. When you're finally ready to push your changes, do:

    $ git co gh-pages
    $ git merge aayusharora-gh-pages
    $ git push
    # If finished, delete personal branch
    $ git br -d aayusharora-gh-pages
    

    Working this way will ensure that any changes you accumulate while working on a task will appear to be the most recent commits when you finally decide to push them. Until then, you can rebase/squash any commits on aayusharora-gh-pages to make the commit sequence look any way you want.