What does "Changes not staged for commit" mean
Asked Answered
S

12

331

I thought if you want to track the files you should git add [files you want to track]

I don't know why I got the messages Changes not staged for commit.

If those files were not staged, shouldn't git shows me those files were Untracked like that

enter image description here

All I've done was create a new feature from develop branch and worked in feature/change_excel_format branch

I thought Those files should be in staged status,

But git status told me Changes not staged for commit

enter image description here

To brief, I only know there are 3 stages in git untracked, staged, committed Can any one tell me , what was the stage in for Changes not staged for commit enter image description here

So if I modified the file a (already in the repo)

and type git st , the git will tell me Changes not staged for commit

if I git a then the file a will be in staged status

if I modified the file a now, there will be two status of file a in git, right ?

So I have to decide if make the staged a be commit or make the not stage a to be staged, and then the previous staged file awill be discard ?

enter image description here

Seafarer answered 15/1, 2014 at 10:29 Comment(1)
if get this message, and do you want this file (a) needs to commit, make sure you are in the correct directory path (project root path for add all) while you git addKinna
T
441

when you change a file which is already in the repository, you have to git add it again if you want it to be staged.

This allows you to commit only a subset of the changes you made since the last commit. For example, let's say you have file a, file b and file c. You modify file a and file b but the changes are very different in nature and you don't want all of them to be in one single commit. You issue

git add a
git commit a -m "bugfix, in a"
git add b
git commit b -m "new feature, in b"

As a side note, if you want to commit everything you can just type

git commit -a
Theosophy answered 15/1, 2014 at 10:31 Comment(8)
So, If those files are alreday in repo. and I have changed them, I also can git commit THEM directly without add them , right ?Seafarer
of course. And in that case only the listed files would be committed, regardless of the presence of staged changes in different files.Theosophy
I've got same issue but my problem was also about submodule cause of untracked files. So in my sub directory I also add all changes with git and the problem has gone.Sharyl
I've already done git clean -fd and I'm still getting this. I want to get rid of all the changes. What else should I be doing?Clothe
git commit -a doesn't commit everything, only the updated files, not the new ones...Katharyn
is git add . enough to stage everything at once?Snowman
Did the default behavior change in a recent git version? I could swear I used to be able to just do "edit file" + "git commit" without running "git add" every time.Bedroll
@Z.Cochrane I have never seen the behaviour you describe and I've been using git for 10 years. You are probably thinking of "git commit -a", which commits all unstaged changes.Theosophy
S
93

You have to use git add to stage them, or they won't commit. Take it that it informs git which are the changes you want to commit.

git add -u :/ adds all modified file changes to the stage git add * :/ adds modified and any new files (that's not gitignore'ed) to the stage

Sarto answered 15/1, 2014 at 10:32 Comment(0)
P
22

It's another way of Git telling you:

Hey, I see you made some changes to your files, but keep in mind that when you write pages to my history, those changes won't be in these pages.

Changes to files are not staged if you do not explicitly git add them (and this makes sense).

So when you git commit, those changes won't be added since they are not staged. If you want to commit them, you have to stage them first (ie. git add).

Plemmons answered 15/1, 2014 at 10:34 Comment(5)
But I didn't change the file it's referencingPogey
I don't understand how exactly this behaviour makes sense. If I've already told Git to track a file by doing add . when I set up the repostiry, and it can detect I've made changes to it, why do I need to explicitly add files every time I commit? Just to make a change to a file, I have to add, commit and push. The demarcation between commit and push makes sense to me, the demarcation between add and commit seems ridiculously redundant.Bosky
@Hashim I agree, I really don't understand the logic.Pigment
@Prometheus; caveat: I'm still a total newbie when it comes to git, so this may be way wrong. "Committing" in git is a 2-step process: 1. Tell git what will be included in the "commit" (the default is NOT all changed files) using "git add", and, 2. Do the "commit". When committing, git ignores files that aren't explicitly "added"; even if that file is in the local repository and has been changed. Not "adding" a file is a way to tell the "commit" that you don't want to send it to the local repository at this time.Standard
@Prometheus It's because Git basically has three "spaces". 1. The working area, which is viewed as temporary and prone to deletion. 2. The staging area, which is where files are placed before being placed in the repo. 3. The repo itself. This allows you to do things like move to a different commit in your history before committing the files in your staging area to the repo (and probably lots of other things I don't fully understand). If you can find the tutorials by Paola Perrotta, I'd strongly recommend themAkee
T
15

Try following int git bash

1.git add -u :/

2.git commit -m "your commit message"

  1. git push -u origin master

Note:if you have not initialized your repo.

First of all

git init 

and follow above mentioned steps in order. This worked for me

Trussing answered 24/1, 2020 at 8:46 Comment(1)
This issue worked for me, on a git conflict where I was unable to delete a file from my local repository and commit from the local repository to the remote master branch.Chlorine
P
14

With Git Version 2.x.x(Current Version)

"Changes not staged for commit" means Some changes are not staged.

So, to stage all changes perfectly, run this command:

git add -A

Then, run this command:

git commit -m "Update"

In addition, when getting "Changes not staged for commit", just run this command as I've already said above to stage all changes perfectly:

git add -A

If you run these commands, all changes won't be staged perfectly:

git add .
git add --ignore-removal .
git add -u  

This table shows the differences of the commands which I mentioned above in Git Version 2.x.x(Current Version):

Command New Files Modified Files Deleted Files Description
git add -A ✔️ ✔️ ✔️ Stage all (new, modified, deleted) files
git add . ✔️ ✔️ ✔️ Stage all (new, modified, deleted) files in current folder
git add --ignore-removal . ✔️ ✔️ Stage new and modified files only
git add -u ✔️ ✔️ Stage modified and deleted files only
Prince answered 8/3, 2022 at 6:48 Comment(0)
B
5

Suposed you saved a new file changes. (navbar.component.html for example)

Run:

ng status
modified:   src/app/components/shared/navbar/navbar.component.html

If you want to upload those changes for that file you must run:

git add src/app/components/shared/navbar/navbar.component.html

And then:

git commit src/app/components/shared/navbar/navbar.component.html -m "new navbar changes and fixes"

And then:

git push origin [your branch name, usually "master"]

---------------------------------------------------------------

Or if you want to upload all your changes (several/all files):

git commit -a

And them this will appear "Please enter the commit message for your changes."

  • You'll see this message if you git commit without a message (-m)
  • You can get out of it with two steps:
  • 1.a. Type a multi-line message to move foward with the commit.
  • 1.b. Leave blank to abort the commit.
    1. Hit "esc" then type ":wq" and hit enter to save your choice. Viola!

And then:

git push

And Viola!

Bygone answered 26/9, 2018 at 17:44 Comment(1)
Same I said above: git commit -a doesn't upload al the changes, it doesn't include the new files. It will only commit the updates to existent files.Katharyn
D
4

What worked for me was to go to the root folder, where .git/ is. I was inside one the child folders and got the error there.

Demetricedemetris answered 29/9, 2020 at 10:23 Comment(0)
E
1

Follow the steps below:

1- git stash
2- git add .
3- git commit -m "your commit message"

Existentialism answered 16/3, 2018 at 5:51 Comment(5)
git stash removed all my changes, there it was 10 hours of work :(Hartal
if you used "git stash" it put those changes to a "temp place" to get them back just type "git stash pop" to get your change from the "temp place"Beisel
DO NOT USE git stash until you read @viniciussvl and Cyrus commentPugnacious
Why would I even use git stash in this case?Mf
How does a post like this gets upvotes? There should be a warning about git stash.Acroterion
L
1

You may see this error when you have added a new file to your code and you're now trying to commit the code without staging(adding) it.

To overcome this, you may first add the file by using git add (git add your_file_name.py) and then committing the changes (git commit -m "Rename Files" -m "Sample script to rename files as you like")

Liveried answered 19/8, 2019 at 10:6 Comment(0)
V
1

I made a silly mistake. I was trying to run this command in a folder where git was not initialized. Make sure you have .git folder there or run the command

git init
Vita answered 16/7, 2022 at 20:0 Comment(0)
M
0

I also received this message, so I did it like this in my python code:

text = input("Insert commit: ")
os.system("git add .")
os.system("git commit -m %s" %(text))
os.system("git push origin master")

And it worked, now it goes without problems. These commands use on a debian server with python3.

Melodramatic answered 17/5, 2021 at 21:22 Comment(0)
H
-10

Remove dir/.../.git

works for me.

Hothead answered 14/8, 2018 at 2:50 Comment(3)
your answer needs background information or insight into why or how it worked for you.Disciplinant
As is, the answer does not support the question. Please consider to change or add further information.Maryrose
this is also super dangerous if you don't know what you're doingGossett

© 2022 - 2024 — McMap. All rights reserved.