Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git says I am ahead by 2 commits

Tags:

git

gitignore

I have a git repo on Github, and a git repo on my web server. Whenever I do git pull origin master to get the updates made on Github, it says Your branch is ahead of 'origin/master' by 6 commits. Funny thing is every time I pull a new change from Github, the number goes up by 2! I also have to 'Merge' each time.

This began after I updated the .gitignore on my web server, and deleted two files that I ignored. I updated the .gitignore and deleted the two files in my Github repo as well.

Git masters, why is this happening? How can I correct it?

like image 383
Don P Avatar asked Jan 15 '23 01:01

Don P


1 Answers

This is happening because you keep creating local merge commits that don't exist remotely. The default behavior of git pull is to merge in new changes, and you keep doing that, but you never pushed your merges. Your history probably looks something like this:

                    o - [master]
                   /|
[origin/master] - o o
                  |/|
                  o o
                  |/|
                  o o
                  |/|
                  o o
                  |/|
                  o o
                  |/
                  o
                  |
                  o

See all those merge commits? Every time you ran git pull you created a new one.

The solution here is to figure out how you want to deal with the local commit that you have. When you say you updated the server and the github repo, did you update them independently? If so, you created two distinct commits that represent the same change. In this case you can just git reset origin/master to get rid of your local merges.

On the other hand, if your local commit doesn't actually represent something that's in the github repo, then you need to decide if you want to keep it or get rid of it. If you want to get rid of it, use the same git reset origin/master. If you want to keep it, you may want to git rebase origin/master to pull it to the tip of your history and then push it to the server.

like image 60
Lily Ballard Avatar answered Jan 19 '23 00:01

Lily Ballard