22

Everything is in the title...

I read this question: Viewing Unpushed Git Commits

But I don't understand why git status displays all unpushed commits in the master branch, but does not display anything in other branches.

Can someone explain it?

EDIT

Here is what commands and outputs I did/get:

aurelien@server:/home/repo/$ git branch
  new_feature
* master

aurelien@server:/home/repo/$ git checkout new_feature
Switched to branch 'new_feature'

aurelien@server:/home/repo/$ echo test > newfile.txt
aurelien@server:/home/repo/$ git add newfile.txt
aurelien@server:/home/repo/$ git commit -m "Test commit"
[new_feature 51c6a64] Test commit
1 file added
aurelien@server:/home/repo/$ git status
# On branch new_feature
nothing added to commit

Why doesn't my commit appear when using git status?

Community
  • 1
  • 1
Aurelien
  • 277
  • 2
  • 8
  • possible duplicate of [How do you make an existing Git branch track a remote branch?](http://stackoverflow.com/questions/520650/how-do-you-make-an-existing-git-branch-track-a-remote-branch) – arnorhs Jun 06 '13 at 09:01
  • 4
    @arnorhs, you're technically correct but I presume the OP just wasn't aware about all that tracking branch machinery so they have no chance of finding that question. Hence I'd not vote to close the answer. – kostix Jun 06 '13 at 09:14
  • cool. that makes sense to me. I can also imagine people trying to google for the question worded in a similar manner as the OPs question if they're not familiar with the correct git term for tracking. It's hard to get into git with all the jargon. – arnorhs Jun 06 '13 at 09:18
  • `git branch --set-upstream-to=origin/master master` worked for me – falsarella Nov 23 '16 at 16:41

1 Answers1

24

The reason is that your master branch actually has a remote branch, on origin/master that your branch has been set up to track.

What this means is that each time you do a commit to master, and then do a git status git will tell you which commits are different between your local branch and the remote branch.

When you create a new branch, there's no corresponding remote branch by default. You can see this by doing git branch -a. that will show you all the remote branches that are set up.

So there are two things at play:

1) You don't have a remote branch for your local branch 2) Your branch isn't set up to track changes from the remote branch

One simple way to make a remote branch and set up tracking for your local branch, is to push the local branch to a remote branch:

git checkout new_feature
git push -u origin new_feature

Normally, when you just push without the -u switch, no tracking will be set up, but your branch will still be pushed. But when you pass in the -u switch it will tell git that you also want to set your branch to track changes from the remote branch.

After you do this and then make changes and commit them, then do a git status you'll get the expected result of "Your branch is a head of origin/new_feature by 1 commit"

arnorhs
  • 10,218
  • 2
  • 31
  • 38
  • I tried what you suggested: git branch -u origin/my_other_branch and got in return: error: unknown switch `u'... – Aurelien Jun 06 '13 at 08:58
  • I also found a duplicate question, which I added a flag for – arnorhs Jun 06 '13 at 09:02
  • @Aurelien, be sure to [read up on remote branches](http://git-scm.com/book/en/Git-Branching-Remote-Branches) to not just blindly apply the provided answer to your case. – kostix Jun 06 '13 at 09:15
  • @arnorhs Now I got : fatal: Not a valid object name: 'origin/my_branch'. – Aurelien Jun 06 '13 at 09:48
  • Two questions: 1) Do you have an upstream called origin? if so 2) Have you pushed the branch to a remote branch? – arnorhs Jun 06 '13 at 09:56
  • I don't know if I have an upstream called origin, but I know that I don't have any remote branch. – Aurelien Jun 07 '13 at 07:35
  • in which case it doesn't make sense for git to tell you how far behind you are, since there's nothing to be behind of. It's possible I was misunderstanding the question entirely... You could you clarify by writing out the command you execute and what the output looks like for master and your other branch? – arnorhs Jun 07 '13 at 08:01
  • I edited my question with the commands I executed... Hope this will be more understandable :-) – Aurelien Jun 07 '13 at 09:21
  • Ok, cool. I basically re-wrote my entire answer. Hope it helps :) – arnorhs Jun 07 '13 at 09:43
  • Many thanks @arnorhs . Now, I won't create a remote branch for all my local branches because I don't want to share them with all other developers. So how can I see on my local branch the unpushed commits? Assuming this branch has no remote branch. – Aurelien Jun 07 '13 at 14:06
  • np.. for your second question, it doesn't really make a lot of sense. unpushed commits are commits that haven't been pushed to a corresponding remote branch. if there is no remote branch, git doesn't have anything to compare your local branch against. so i'm a bit confused by what you mean. – arnorhs Jun 08 '13 at 05:13
  • Ok, what I mean is that when I commit something, it means that I will push it later. So, when I have for example 5 commits, it's nice to be able to know what I'm gonna push before doing it. And I thought that "git status" meant "show me the status of my pending commits". – Aurelien Jun 10 '13 at 07:36
  • 1
    ahh.. no, that's actually what you use git log for... Generally, you'll do something like `git diff origin/master` (tells you what's changed since latest origin's master) or `git log origin/master..` (tells you which commits your branch has that origin/master doesn't have) or just `git log master..` if your master branch is up to date – arnorhs Jun 11 '13 at 01:14
  • ooooook ! I understand it better now. I got confused between "git status" and "git log". Many thanks for all your answers and help. – Aurelien Jun 11 '13 at 13:11