Reputation: 13310
I am getting the following when running git status
Your branch is ahead of 'origin/master' by 3 commits.
I have read on some other post the way to fix this is run git pull --rebase
but what exactly is rebase, will I lose data or is this simple way to sync with master?
Upvotes: 608
Views: 774608
Reputation: 16235
You get that message because you made changes in your local master and you didn't push them to remote. You have several ways to "solve" it and it normally depends on how your workflow looks like:
git push origin
assuming origin is your remotegit reset --hard origin/master
Upvotes: 1278
Reputation: 4046
The following command will change nothing in your local or your remote. It will just show you the differences:
git log -p {branch_name}..origin/{branch_name}
If I'm on the dev
branch, for example, I would enter git log -p dev..origin/dev
.
Seeing what's different on your remote may remind you why there is a difference, and it may affect your decision about how to resolve the situation.
Making them identical without checking could really complicate things more. Keep in mind, you may want
Upvotes: 1
Reputation: 11
In my case none of the suggested answers worked. When I tried
git reset --hard main/main
I was taken back to some commits from months ago. I ended up deleting my local repo, cloning the remote repo and then adding any ignored files from the initially deleted repo for a clean start
Upvotes: 0
Reputation: 4438
As others said, you have local changes that you committed but didn't push.
If you're unsure about which changes are there, and you want to check before doing a push, the command below will tell you which files changed:
git diff --stat origin/master..
And if you remove --stat
, it will show the diff of all files.
Upvotes: 2
Reputation: 21
I had this problem and I used 'git reset --hard origin/master' without quotes to rebase my local master to the remote master branch.
Upvotes: 1
Reputation: 805
This happened to me once after I merged a pull request on Bitbucket.
I just had to do:
git fetch
My problem was solved. I hope this helps!!!
Upvotes: 8
Reputation: 958
If your git says you are commit ahead then just First,
git push origin
To make sure u have pushed all ur latest work in repo
Then,
git reset --hard origin/master
To reset and match up with the repo
Upvotes: 25
Reputation: 4333
Usually if I have to check which are the commits that differ from the master I do:
git rebase -i origin/master
In this way I can see the commits and decide to drop it or pick...
Upvotes: 18
Reputation: 1644
Came across this issue after I merged a pull request on Bitbucket.
Had to do
git fetch
and that was it.
Upvotes: 29
Reputation: 19771
Step 1 : git checkout <branch_name>
This is obvious to go into that branch.
Step 2 : git pull -s recursive -X theirs
Take remote branch changes and replace with their changes if conflict arise.
Here if you do git status
you will get something like this your branch is ahead of 'origin/master' by 3 commits.
Step 3 : git reset --hard origin/<branch_name>
Step 4 : git fetch
Hard reset your branch.
Enjoy.
Upvotes: 71
Reputation: 44593
This message from git
means that you have made three commits in your local repo, and have not published them to the master
repository. The command to run for that is git push {local branch name} {remote branch name}
.
The command git pull
(and git pull --rebase
) are for the other situation when there are commit on the remote repo that you don't have in your local repo. The --rebase
option means that git
will move your local commit aside, synchronise with the remote repo, and then try to apply your three commit from the new state. It may fail if there is conflict, but then you'll be prompted to resolve them. You can also abort the rebase
if you don't know how to resolve the conflicts by using git rebase --abort
and you'll get back to the state before running git pull --rebase
.
Upvotes: 11
Reputation: 59841
There is nothing to fix. You simply have made 3 commits and haven't moved them to the remote branch yet. There are several options, depending on what you want to do:
git push
: move your changes to the remote (this might get rejected if there are already other changes on the remote)git pull
: get the changes (if any) from the remote and merge them into your changesgit pull --rebase
: as above, but try to redo your commits on top of the remote changesYou are in a classical situation (although usually you wouldn't commit a lot on master in most workflows). Here is what I would normally do: Review my changes. Maybe do a git rebase --interactive
to do some cosmetics on them, drop the ones that suck, reorder them to make them more logical. Now move them to the remote with git push
. If this gets rejected because my local branch is not up to date: git pull --rebase
to redo my work on top of the most recent changes and git push
again.
Upvotes: 49