Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Git Basics Getting Started With Git Committing Changes

brandonlind2
brandonlind2
7,823 Points

how does git know what change you're commiting?

I noticed with git add he put the file name he was adding, however with git commit, he just put well git commit, does git automatically assume the newest change is what is being commited, if so how you would you selectively commit something that isnt the newest thing added?

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi Brandon Lind

you can run

git status

this comment shows you what files have been changed since the last commit, and what files will be committed if you run git commit.

Remember, git makes absolutely no assumption on what to commit, it only commit whatever it was told to commit. And to do so you need to manually add the changed files to the tracking stage by using git add filename before running the git commit; in another word, those changed files that haven't been added to the tracking stage won't get committed.

Well I guess that you can also make use of the -a switch with the commit command to automatically "add" changes from all known files.

"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."

William Li
William Li
Courses Plus Student 26,868 Points

correct, the -a flag for git commit is indeed a shorthand to stage changed files and commit in one go, but that's only when you would wanna commit every changes. I believe the questioner's asking about "how you would you selectively commit something that isnt the newest thing added?", no? It sounds to me that he wants to commit only some changed files but not all of 'em. Hope I didn't misinterpret his question.

Hi William,

It was just for completeness, as I didn't want to leave Brandon thinking that he has to use git add filename to manually add the changed files to the tracking stage all the time.

Can you explain how the git commit filename command works? I wasn't sure if it would use what had been added via a git add filename command or would just grab the file direct?

William Li
William Li
Courses Plus Student 26,868 Points

hi, bothxp, git commit filename? I am a little confused, where's that come from? Could you elaborate?

I was thinking about the "if so how you would you selectively commit something that isnt the newest thing added?" part of the question.

I was looking at the git commit documentation

"The content to be added can be specified in several ways:

  1. by listing files as arguments to the commit command, in which case the commit will ignore changes staged in the index, and instead record the current content of the listed files (which must already be known to Git);"

So I'd assumed that the command would take the form of git commit filename but I've never used that type of commit myself so I could be very wrong on that.

Reading the docs more: "After staging changes to many files, you can alter the order the changes are recorded in, by giving pathnames to git commit. When pathnames are given, the command makes a commit that only records the changes made to the named paths:

$ edit hello.c hello.h
$ git add hello.c hello.h
$ edit Makefile
$ git commit Makefile

This makes a commit that records the modification to Makefile. The changes staged for hello.c and hello.h are not included in the resulting commit. However, their changes are not lost — they are still staged and merely held back. After the above sequence, if you do:

$ git commit

this second commit would record the changes to hello.c and hello.h as expected."

William Li
William Li
Courses Plus Student 26,868 Points

Hi, bothxp, This, indeed, is very interesting (and complex too). To be honestly, I've used many git commit flags in the past, but never commit without one. So I don't have the answer myself, but tomorrow let me ask my friend who is working at Github, perhaps he knows. Will keep you posted :)

Lovely thanks.

I've got a couple of books on git at home that I really need to get around to reading. My current level of git usage is more like: http://xkcd.com/1597/

William Li
William Li
Courses Plus Student 26,868 Points

Hi, bothxp , Sorry for the late followup.

Yeah, my friend replied my message. He said he's knows a bit git commit filename, however, neither he or any Githubber is using this command. Because the git tradition of staging a file via git add before committing selected files has its reason, as when working on a large codebase, it's very common to see many files with very similar names, going directly to git commit filename without the git add step may easily committing a wrong file, it's true that git gives you many different ways to fix an unintended commit, but it's much simpler to unstage a file than to redo the commit altogether.

Furthermore, he explains that

$ edit hello.c hello.h
$ git add hello.c hello.h
$ edit Makefile
$ git commit Makefile

In this example and the explanation from the git documentation making it sounds more complex than it needs to be, it's actually pretty simple, git commit file1 file2 ... allows you to directly grab whatever changed files and commit them, without needing the extra step of git add (file1, file2 must be already tracked by git).

Hope this info is helpful. By the way, what git books you're currently reading? I finished this book git in practice couple months ago, it's a wonderful mid~expert level git book. Cheers.

Hi William,

Thanks for the explanation.

At the moment I've got "Version Control with Git, 2nd edition" & "Introducing GitHub". I just need to find some time to read them.

Hi Brandon,

You can add whichever files you like to the repository, like you say. Using git add *filename* achieves that. From then you make an initial commit; the first benchmark. After that, any deviation from that benchmark, or subsequent ones, is committed when you ask it to.

You can commit as often as you like, indeed that is encouraged, so you are able to roll back incrementatlly, if you wish, should your project stop working after the addition of some new code. I don't think you can commit part of a change. Git watches the added files for all changes; I don't think you can select one change over another within the same file. And, I'm not sure why you'd want to - if you've committed at suitable points through the development process, you should have enough points saved to rollback to the last working model.

I hope that helps - I'm not sure it did!

Steve.

You can commit partial changes, or control large changes in multiple commits.

Hi Brandon,

I'd recommend that you go and have a quick read of the git add and the git commit commands documentation. As documentation goes it's quite approachable.

Basically you use the 'git add' commands to make sure that git is aware of the changes that you have been making to files. Then you use the 'git commit' commands to well ... commit those changed files to your repository.

"git add can be performed multiple times before a commit. It only adds the content of the specified file(s) at the time the add command is run; if you want subsequent changes included in the next commit, then you must run git add again to add the new content". So I believe that if you make a change to a file, use git add to stage that file ready for your commit but then make further changes to the file then you will need to run a git add against the file again otherwise it will only commit the changes from the last add. Maybe

Running 'git commit' will commit all of the file snapshots that are currently staged using the 'git add' commands. But you can selectively commit files by using the file name as an argument to the commit command ... but I'm not sure if that will use the latest version of that file that you staged or if it will just take the file directly including any updates that you had not staged using an add.

Hmm ... I think the best think to do is to set yourself up a git repository and have a play. Have a look at the docs and try a few things out.

So as Steve has already said "I hope that helps - I'm not sure it did!"

:-)

"how does git know what change you're commiting?" - The direct answer would be that if you are not specifying a filename as part of the commit command, then it will commit all of the changes that you have previously staged using the git add commands since your last full commit. You can use the git status command to get the listing of the files that are currently staged and will be committed.