git making two commits when branch is behind

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
I have made a local commit to the repo.
The remote branch is ahead by one commit. So I forgot to take the pull and commit my changes to local.
I did a git pull and git push
I saw the following 2 commits in the branch. The last one is weird.
whats the need for that to be created. how can i avoid that ?
So here my local branch was on 1f6a commit
and the origin was at 0ca commit

when i tried to push i got the error message :
On branch stage
Your branch and 'origin/stage' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

after performing git pull i did git commit
and the top two commits were produced. Ideally only one commit should have
produced that of Remove nanosecond message.
How to avoid this top commit.
commit treeOne way is to uncommit your latest change and then take a git pull
How to uncommit the lates change that is in local ?

This problem is mainly coming when i am committing something and the origin branch is
ahead of local branch.
The Merge branch 'stage' of into stage commit
which was automatically created has this in it. It shows two parents !!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The messages I see make sense to me.
- The merge to get your local branch in sync wins because it was the last in.

The multiple parents also makes sense to me.
- As far as stage is concerned, there were two parent branches that led to the child branch you were merging into stage.
- At least, that's how it works in the environments I've used.

The only way I can immediately think of to avoid the situation is to squash the final commit so that it forces it to have only one parent. However, you'll lose the metadata and history from the parent that loses... so if that's important to your organization, you might simply want to leave yourself a sticky note on your monitor reminding you to always pull first and commit often.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial