Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'git commit' not save my changes?

I did a git commit -m "message" like this:

> git commit -m "save arezzo files"
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   arezzo.txt
#       modified:   arezzo.jsp
#
no changes added to commit (use "git add" and/or "git commit -a")

But afterwards, when I do git status it shows the same modified files:

> git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   arezzo.txt
#       modified:   arezzo.jsp
#
no changes added to commit (use "git add" and/or "git commit -a")

What am I doing wrong?

like image 741
arezzo Avatar asked Oct 09 '11 15:10

arezzo


People also ask

Does git commit Save changes?

Remember, git commit is saving changes in Git. You can also use the git reset command to undo a commit or staged snapshot when/if needed.

How do I save changes to a commit?

The primary way to save your changes is to add them to Git's staging area using the git add command and then commit using git commit. This saves your revision information in Git's repository which makes it a part of your commit history.

How do I fix my commit?

You can modify the most recent commit in the same branch by running git commit --amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit --amend to modify the most recent commit.


3 Answers

As the message says:

no changes added to commit (use "git add" and/or "git commit -a")

Git has a "staging area" where files need to be added before being committed, you can read an explanation of it here.


For your specific example, you can use:

git commit -am "save arezzo files"

(note the extra a in the flags, can also be written as git commit -a -m "message" - both do the same thing)

Alternatively, if you want to be more selective about what you add to the commit, you use the git add command to add the appropriate files to the staging area, and git status to preview what is about to be added (remembering to pay attention to the wording used).

You can also find general documentation and tutorials for how to use git on the git documentation page which will give more detail about the concept of staging/adding files.


One other thing worth knowing about is interactive staging - this allows you to add parts of a file to the staging area, so if you've made three distinct code changes (for related but different functionality), you can use interactive mode to split the changes and add/commit each part in turn. Having smaller specific commits like this can be helpful.

like image 91
Peter Boughton Avatar answered Oct 18 '22 07:10

Peter Boughton


You didn't add the changes. Either specifically add them via

git add filename1 filename2

or add all changes (from root path of the project)

git add .

or use the shorthand -a while commiting:

git commit -a -m "message".
like image 40
Femaref Avatar answered Oct 18 '22 07:10

Femaref


You should do:

git commit . -m "save arezzo files"
like image 30
Baptiste Pernet Avatar answered Oct 18 '22 07:10

Baptiste Pernet