I don’t understand git commit at all.
It’s been a week since I’ve been using git and all these git with –a, -m, new files, untracked etc is making me totally confused. Tortoisegit is saving me for the moment, but I would like to understand the essence and use it in a way it’s totally easy.
When I am in the git command line, it’s rather difficult to selectively commit some files and keep the rest for another commit. How do I make this easy?
To add a Git commit message to your commit, you will use the git commit command followed by the -m flag and then your message in quotes. Adding a Git commit message should look something like this: git commit -m “Add an anchor for the trial end sectionnn.”
The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.
To get the full experience you should listen while you read. Git is actually sooo hard. Not just to learn, but also to use consistently. And I say that as a person who used it for probably over ten years.
It is quite simple. You need to add the changes that you want to the index first:
git add file1 file2
then
git commit
if you removed a file, the add does that. Add means "add the change" even though it is a removal.
If you want to add all changes:
git add -A
The -a
parameter on commit says to add all changes of tracked files and commit them. So
git commit -a
will not commit a new file you created. You must explicitly add this.
The -m
parameter allows you to avoid opening the editor to edit your commit message and use what you put in following the -m
option:
git commit -m "Use this message and don't open the editor"
Sometimes this is not a good idea. If you just tried a merge and had conflicts, git caches a very nice message for you once you resolve the conflicts and commit. So there a git commit
is better.
To selectively add files, use the patch modifier on git add:
git add -p
This will now prompt you about the files. This is quite powerful as you can also specify parts of files, or alternatively edit what you want to add to the index. A git commit will only add those.
If you want some gui help that is not tortoisegit (avoid windows shell integration), use git gui
.
Here is a diagram explaining the index (or staged files):
(from http://progit.org/book/ch2-2.html)
hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With