125

Seems easy but I just don't get it. I am in the root of my application.

Here is my workflow.

git add .
git commit -m "added a new feature some files changed"
git push heroku master

This usually works. All my changes are pushed.

But sometimes I have a file that I change but when I push to Heroku the changes are not there for THAT ONE FILE... but for most of the files the changes are there...

But if I do

git add .
git commit -am "added a new feature some files changed"
git push heroku master

Everything (all changes) are pushed to Heroku

slindsey3000
  • 4,053
  • 5
  • 36
  • 56
  • 1
    Can you give examples as to which file isn't caught? Is it a file you've deleted? Sometimes you need to do `git add . --update` to catch those. – helion3 Nov 09 '13 at 15:53
  • @BotskoNet - The file is a CSS file in my Rails application. For some reason when I edit that file the changes are not pushed to Heroku. This has happened before and I just don't understand why. – slindsey3000 Nov 11 '13 at 14:05
  • Maybe this will help you. [Different between those][1] [1]: http://stackoverflow.com/a/15419846/3962576 – Reven Feb 06 '15 at 07:40

8 Answers8

135

From the docs:

git commit -a automatically stage all tracked, modified files before the commit If you think the git add stage of the workflow is too cumbersome, Git allows you to skip that part with the -a option. This basically tells Git to run git add on any file that is "tracked" - that is, any file that was in your last commit and has been modified. This allows you to do a more Subversion style workflow if you want, simply editing files and then running git commit -a when you want to snapshot everything that has been changed. You still need to run git add to start tracking new files, though, just like Subversion.

Using the option -am allows you to add and create a message for the commit in one command.

JBallin
  • 8,481
  • 4
  • 46
  • 51
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • 3
    So why does `git commit -am "x"` work ... but .... `git add .` `git commit -m "x"` does not work? – slindsey3000 Nov 11 '13 at 14:05
  • 20
    Because some of your files are not staged. This occurs with deleted/added files that are not currently being tracked. Usually, you have to do a `git add -u` in order to stage "untracked" files. `-am` will do this for you as well. – Davin Tryon Nov 11 '13 at 14:18
  • 4
    @slindsey3000 you might be looking for git add -A which is the same as git add --all, which is different than git add . – Alexander Mills May 30 '15 at 01:03
  • From 2022. @DavinTryon. I believe it is not entirely correct, `git add -u` only stages modified files, not untracked, it is clear in the [documentation](https://git-scm.com/docs/git-add#Documentation/git-add.txt---update:~:text=but%20adds%20no%20new%20files) (adds no new files) – Diogo Jul 23 '22 at 03:44
  • The same happens to `git commit -am`, the `-a` option [doesn't stage new files](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--a:~:text=new%20files%20you%20have%20not%20told%20Git%20about%20are%20not%20affected) – Diogo Jul 23 '22 at 03:46
  • @slindsey3000, I believe the reason why `git commit -am "x"` works and `git add .` doesn't is because `git add .` only stages files in the current folder. Please see [this answer](https://stackoverflow.com/a/26039014/9761768) – Diogo Jul 23 '22 at 03:53
38

I would suggest, if you only changed one file then you might do something like this:

git add "Your_file.txt"
git commit -m "added a new feature in a file"
git push heroku master

Or if you changed multiple files then you could do something like this:

git add .
git commit -m "some files changed"
git push heroku master

Similarly, you could add and commit all the files on one line with this command:

git commit -am "added a new feature some files changed"
git push heroku master
hasan.alkhatib
  • 1,509
  • 3
  • 14
  • 28
Juyal Jee
  • 517
  • 5
  • 10
  • 5
    `git commit -am` does not push, so you can't replace the three mentioned lines. – WilomGfx Mar 13 '19 at 16:44
  • 7
    @WilomGfx I think no. What he is saying is: _you could add and commit_ not push; so he is right when says: _on one line with this command_ – robe007 Jul 24 '19 at 17:47
5

The basic difference between them is that:

 git commit -m = send log message (don't work without git add )
 git commit -am = git add -a + git commit -m

and git add -a = stages Everything

4

git commit -am is a convenient command to stage and commit at once for TRACKED(=STAGED) files.

As Nail answered,

git commit -am = git commit -a + git commit -m 

git commit -m: commit with message (you probably know this part)

git commit -a | git commit --all:

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected. -from git documentation

new files you have not told Git about are not affected

The bold part is what's important.

the -am flag works only for TRACKED(staged) files that are modified or deleted(b/c "delete" means removing what existed = tracked before)

Therefore, if you add new file(s), and commit with -am flag, those newly create file(s) won't included in the commit because it is not tracked(staged).

If you create new files and want to use -am flag to avoid using

git add .
git commit -m "some commit" // reducing one line makes a big difference, I agree

, stage those new ones first with git add . and you can use git commit -am instead of the commands with two lines above.

brandonwie
  • 551
  • 5
  • 12
2

The difference between git commit -m "first commit" and git commit -am "your first commit" is that in the former, you will need to first of us do "git add ." while you don't need that in the latter. The 'a' in the "-am" flag tell git to first of all add all changes

1

Imagine there are fileA fileB fileC, and you modify them. Then commit.

git add .
git commit -m "update"

All files are commited and tracked by git.

Imagine fileD is created by you or some toolchain, and you modify fileD and fileA. Then commit.

git add .
git commit -m "update"

fileD gets tracked by git (git add .), and fileA and fileD are tracked by git.


With regarding to "So why does git commit -am "x" work ... but .... git add . git commit -m "x" does not work?"

Ans:

IT IS BECAUSE you did something like this:

Imagine fileE is created by you or some toolchain (let's say it is fileE version1), and you staged it

git add .

and then you modify fileE (let's say it becomes fileE version2). ^ Then commit.

git commit -m "update"

As a result, only version1 of fileE is committed. Although it confuses the user, it is the expected behaviour of git: git doesn't know about fileE version2. ---- for the uncommited fileE, you staged it too early as you forget to let git know about fileE version2 with doing a git add fileE (at ^ mark of moment) after a newer version of fileE, before committing.

So just remember to git add everything of its newest version before commit. (add new version, commit new version)

-

LET'S ROLL BACK the whole story before fileE is created, and do it again so that you don't miss the fileE version2.

Imagine fileE is created by you or some toolchain (let's say it is fileE version1), and you modify fileE (let's say it becomes fileE version2).

Then commit.

git add .
git commit -m "update"

As a result, version2 of fileE is committed.

If you want the version1 fileE to be in a commit in your commit history, simply just commit twice: once for its old version, once for its new version (aka. once before its being modified to version2, once for its version2)


Tip:

Always follow this sequence: (add new version, commit new version)

(file modification) + git add . + git commit -m "update",

(file modification again) + git add . + git commit -m "update",

and you won't run into this problem of missing any changes.

And you don't even need the "convenience" provide by -am. Because using -am is equivalent to asking git to be smart and be lenient about your forgetting git add . With a clear mind to follow the intuitive sequence, you don't need lenience at this level. Sometimes, unnecessary lenience bring more brain load than benefits.

tinystone
  • 369
  • 3
  • 6
0

In layman's words :

if you use only -m, then you need to add any file one by one, if you can't use .(dot) due to avoid adding many unwanted unstaged / untracked files

so you use like below

git add test1.txt
git commit -m "adding test1.txt file"

Now if you know you want to add all files which are modified (not new file or untracked file), you can simply use -am with commit command and all modified files will add and stage together in one command

so if text1.txt is not a new file / untracked file and its a modified file / tracked file then simply do below without using add command

git commit -am "adding test1.txt file"

Source:

https://www.gitkraken.com/learn/git/commit

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
-1

it is doing 2 jobs together 1)staging the modified files 2)adding comment in the same line

but it will not add the files from unstage to stage i.e it is not working the job of $git add .