51

I found many questions with similar subject, but I didn't found any practical guidance about this issue: why git status informs me nothing to commit, working directory clean, even tough I have made a modification at my local branch?

Here are the steps which I followed:

  • git init [On branch master - Initial commit, nothing to commit (create/copy files and use "git add" to track)]
  • git remote add https://github.com/username/project.git
  • git pull origin master
  • touch test
  • git add test
  • git commit -m "Adding file for test purposes only."
  • git status [On branch master - nothing to commit, working directory clean]

If I do a git push, the modification is committed to the remote branch. I just want to perform "git status" after my modifications, and receive the information that I have changes on my local branch that must be pushed to the remote branch of the project.

What's going on?

starball
  • 20,030
  • 7
  • 43
  • 238
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
  • The error message is telling you that you have nothing to commit, since you just did a commit. However, your local branch is now ahead of the commit on the remote whence you initially did `git pull`. You can resolve this by merging or fast-forwarding the remote branch with your change. – Tim Biegeleisen May 17 '16 at 07:06
  • Your working directory **is** clean, as every change is commit. What you are looking for is the difference between your local repository and the remote origin. So you might want to do either `git log` or `git diff master origin/master` (or the branch you are on instead of master) – Karsten Koop May 17 '16 at 07:14

13 Answers13

70

Your local branch doesn't know about the remote branch. If you don't tell git that your local branch (master) is supposed to compare itself to the remote counterpart (origin/master in this case); then git status won't tell you the difference between your branch and the remote one. So you should use:

git branch --set-upstream-to origin/master

or with the short option:

git branch -u origin/master

This options --set-upstream-to (or -u in short) was introduced in git 1.8.0.

Once you have set this option; git status will show you something like:

# Your branch is ahead of 'origin/master' by 1 commit.
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • 1
    Perfect, Chris! Have no words. Thank you so much! I was thinking about that, before going to sleep but I wasn't sure about it.. All makes sense with this explanation :). – ivanleoncz May 17 '16 at 14:13
  • What about the error · the requested upstream branch 'origin/master' does not exist·? – rosefun Mar 17 '20 at 13:49
  • 1
    that is a different question: https://stackoverflow.com/questions/41412398/how-to-address-git-error-the-requested-upstream-branch-upstream-master-does-n – Chris Maes Mar 18 '20 at 07:28
  • I mistakenly did this for a new branch `test_evals` I had created locally which I did not want to be equivalent to master. I pulled and realized my mistake when there were a bunch of merge conflicts. For a new local branch separate from master, doing `git push --set-upstream origin test_evals` did the trick for me. (after doing `git reset --hard HEAD~1` to undo my pull from master as advised here: https://stackoverflow.com/a/2389423/4383594). – Simon Alford Jan 26 '21 at 18:48
4

git status output tells you three things by default:

  1. which branch you are on
  2. What is the status of your local branch in relation to the remote branch
  3. If you have any uncommitted files

When you did git commit , it committed to your local repository, thus #3 shows nothing to commit, however, #2 should show that you need to push or pull if you have setup the tracking branch.

If you find the output of git status verbose and difficult to comprehend, try using git status -sb this is less verbose and will show you clearly if you need to push or pull. In your case, the output would be something like:

master...origin/master [ahead 1]

git status is pretty useful, in the workflow you described do a git status -sb: after touching the file, after adding the file and after committing the file, see the difference in the output, it will give you more clarity on untracked, tracked and committed files.

Update #1
This answer is applicable if there was a misunderstanding in reading the git status output. However, as it was pointed out, in the OPs case, the upstream was not set correctly. For that, Chris Mae's answer is correct.

Community
  • 1
  • 1
dubes
  • 5,324
  • 3
  • 34
  • 47
  • 2
    The OP made a new repository, added a new remote to that new repository, and then obtained one branch from the named remote and made that his local `master`. Because of this somewhat peculiar sequence (it would have been much more normal to just `git clone` the upstream), he has no upstream set, so `git status` is not able to say that he is now ahead of that upstream. ([Chris Maes' answer](http://stackoverflow.com/a/37270273/1256452) is correct.) – torek May 17 '16 at 09:40
3

The problem is that you are not specifying the name of the remote: Instead of

git remote add https://github.com/username/project.git

you should use:

git remote add origin https://github.com/username/project.git
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Jonathan
  • 31
  • 1
2

I had the same issue because I had 2 .git folders in the working directory.

Your problem may be caused by the same thing, so I recommend checking to see if you have multiple .git folders, and, if so, deleting one of them.

That allowed me to upload the project successfully.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
2

Follow these steps for commits

1.making new branch for protect your files

git checkout --orphan latest_branch

2.add it into new branch

git add -A

3.commit

git commit -am "commit message"

4.check git status

git status

5.files or correct try to push in this step

git push -f origin main

Now your files are safe

Clear your branches if you don't want specific and create it what branch your want

eg:master or main

6.Delete the branch

git branch -D main

7.Rename the current branch to main

git branch -m main

8.Finally, force update your repository

git push -f origin main
Balaji
  • 9,657
  • 5
  • 47
  • 47
2

removing .git hidden folder helped me solving this issue. But make sure you don't have unstaged or unpushed changes

rm -rf .git
0

Small hint which other people didn't talk about: git doesn't record changes if you add empty folders in your project folder. That's it, I was adding empty folders with random names to check wether it was recording changes, it wasn't. But it started to do it as soon as I began adding files in them. Cheers.

0

just do the following steps

  1. remove .git folder

  2. add the git repo

    $ git init

    $ git add origin master repo.git

  3. then pull or push the files

Done!!!

Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
0

This solution might not be suitable for most situations but it is a lifesaver in a situation where; you are making major changes then the git commit is working but when you make a slight change in one file (let's say in CSS file) then the git commit won't work.

I faced this issue and I tackled this situation by making changes on GitHub and committing there then running git pull on my local repository.

0

In my case it was easily fixed by saving the code. In Visual Code there is an auto save option, that allows you to automatically save your files after a certain interval or when certain events occur. So every time a change is made it will show in the Source Control tab.

  • Hi Sandalu. Thanks for joining the community. Without having access to your environment and experiencing the same situation, I'm pretty sure that the situation you went through, was different than "git status (nothing to commit, working directory clean)" and all the scenario which I explained. People here on SO might be rough, specially with an answer like this, that is vague or depending on a specific aspect, like your IDE. Don't get surprised if someone gives a thumbs down to this kind of answer (words of someone who was prohibited of making questions at SO, years ago). Regards. – ivanleoncz Feb 19 '23 at 18:11
0

This is easily doable by doing the following: Checkout branch_x to work on it: git checkout branch_x Reset soft to master so that branch_x is now at the same place than master in the git history but all your changes are now staged (reset --soft doesn't touch files in the working directory and even add the changes directly to the staging area): git reset --soft master Commit the files so that it will create one commit containing just the changes made in branch_x compared to master git commit And now branch_x is one commit ahead of master and you can create your PR. You perhaps will have to push --force depending if you already pushed branch_x

Ojay
  • 1
  • 1
  • 2
    Hi Ojay. Thanks for proposing an answer for the issue. Would you mind to better structure your answer? Separating commands with bullet points for example. Here's a document that might help you on better presenting your proposal: https://stackoverflow.com/editing-help Thanks for contributing :). – ivanleoncz Aug 21 '23 at 16:59
-1

For me $ git fetch works. it helps to load data to master

Anorov Hasan
  • 123
  • 2
  • 2
-7

Delete your .git folder, and reinitialize the git with git init, in my case that's work , because git add command staging the folder and the files in .git folder, if you close CLI after the commit , there will be double folder in staging area that make git system throw this issue.

Mostdeep
  • 1
  • 1
  • 2
    Deleting the .git folder will delete the whole source control. Counterintuitive to what the actual question is. – Mudassir Razvi Feb 10 '21 at 07:00
  • I mean delete git and reinitialize .git source control, it will solve that , not just delete it , but also init it – Mostdeep Feb 16 '21 at 10:54
  • While your solution, in general, is not wrong, in the given context it can be damaging, as it can delete any unpushed or unstaged changes. You need to understand the context and suggest an appropriate solution and not just a generic one. – Mudassir Razvi Feb 17 '21 at 05:03