git commit -m vs. git commit -am

GitGithub

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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.

Solution 6 - Git

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 .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionslindsey3000View Question on Stackoverflow
Solution 1 - GitDavin TryonView Answer on Stackoverflow
Solution 2 - GitJuyal JeeView Answer on Stackoverflow
Solution 3 - GitNail Khaled BeneloualidView Answer on Stackoverflow
Solution 4 - GitAbideen MuhammedView Answer on Stackoverflow
Solution 5 - GitBrandon WieView Answer on Stackoverflow
Solution 6 - GitChandu SinghView Answer on Stackoverflow