Rui Miguel
Rui Miguel

Reputation: 1

Your branch is ahead of 'origin/main' by X commits even after git push

I recently started using GitHub to manage my code, using git commit -a -m command to commit changes to all files (-a) added with git add <filename>, along with a message (-m). After that, I run git push https://rmiguelc:`cat token`@github.com/rmiguelc/myproj.git to save changes to the remote repository (the token file is in .gitignore). The project is small, so only the main branch exists.

After creating a few tags and releases on the GitHub website, I retrieved them.

$ git fetch https://rmiguelc:`cat token`@github.com/rmiguelc/myproj.git --tags
From https://github.com/rmiguelc/myproj  
 branch            HEAD       -> FETCH_HEAD  
 [new tag]         v0.2       -> v0.2  
 [new tag]         v0.3       -> v0.3 

Following that, I used git checkout, but was surprised with the following output:

Your branch is ahead of 'origin/main' by 17 commits.
(use "git push" to publish your local commits)

Several StackOverflow answers suggested using git reset --hard origin/master, so I tried it:

fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'

After some consideration, I got a clue on what's happening. https://[email protected]/rmiguelc/myproj.git and Origin/master are not the same entity. Git pushes have been directed to the first, but not to the second. Therefore, it is behind by 17 commits (myproj happens to have 18), because no pushes were ever made to it.

Is my interpretation correct? How should I proceed to make it so all Git commands stop considering Origin/master? Also, is there a better way to use a token than to paste it every time?

Upvotes: 0

Views: 65

Answers (1)

Useless
Useless

Reputation: 67802

Following that, I used git checkout, but was surprised with the following output:

Your branch is ahead of 'origin/main' by 17 commits.
(use "git push" to publish your local commits)

So you know your remote branch is called origin/main.

Several StackOverflow answers suggested using git reset --hard origin/master, so I tried it

fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree.

But you already know your remote branch is called main!

Branch names aren't magic, and they're not immutable: they're just whatever branches you happen to have in your repo.

Run git branch -r to see what your remote branch is called (ie, to confirm it's origin/main as we already established).

If you want to run the command you found, it should be git reset --hard origin/main. But don't do that, at least not yet.

Find out why your branches are out of sync despite pushing. Run git log --graph --all --oneline (or gitk) to see the version tree. Where is your local HEAD? Where is your origin/HEAD? Don't force reset until you understand what you're changing and why.

Upvotes: 0

Related Questions