Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Teach me git commit

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?

like image 324
Quintin Par Avatar asked Apr 11 '11 18:04

Quintin Par


People also ask

How do I do a git commit?

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

What is commit in git in simple words?

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.

Is git easy to learn?

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.


1 Answers

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):

enter image description here

(from http://progit.org/book/ch2-2.html)

hope this helps.

like image 136
Adam Dymitruk Avatar answered Oct 04 '22 02:10

Adam Dymitruk