105

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
  • 3,753
  • 5
  • 31
  • 55
  • 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

6 Answers6

117

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
  • 6,568
  • 1
  • 40
  • 45
Davin Tryon
  • 64,963
  • 15
  • 144
  • 131
  • 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
  • 18
    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
33

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,379
  • 3
  • 12
  • 26
Juyal Jee
  • 467
  • 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
  • 6
    @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
3

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

1

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

0

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.

Brandon Wie
  • 27
  • 2
  • 7
-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 .