Commit#

git commit is central command for git that allows to save staged memories for ever.

Add files (-a)#

This option makes git to stage file that wasn’t staged before the commit. It’s like to do commit for edited files without execution of git add before.

Basic example#

So in the following example:

  • Create a repository containing a modified file;

  • First attempt to commit without additional options - fails, log doesn’t show control commit just because there was nothing to commit;

  • Second attempt to commit -a - it works, new commit appears in git log results.

%%bash
mkdir commit_example
cd commit_example

git init &> /dev/null
echo "some text" > test_file
git add --all
git commit -m "innitial commit"&> /dev/null
echo "some other text" > test_file

echo "=====Just commit====="
echo "-----commit-----"
git commit -m "control commit"
echo "-----log-----"
git log --oneline

echo
echo "=====Commit -a====="
echo "-----commit-----"
git commit -am "control commit"
echo "-----log-----"
git log --oneline


cd ..
rm -r commit_example
=====Just commit=====
-----commit-----
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   test_file

no changes added to commit (use "git add" and/or "git commit -a")
-----log-----
b492f00 innitial commit

=====Commit -a=====
-----commit-----
[master 1b938f2] control commit
 1 file changed, 1 insertion(+), 1 deletion(-)
-----log-----
1b938f2 control commit
b492f00 innitial commit

Untracked files#

This option won’t work for untracked files. You have to add it before.

So in the following example, I’m trying to use commit -a for a repository with only one untracked file, and getting messages that I need to add it first.

%%bash
mkdir commit_example
cd commit_example

git init &> /dev/null
echo "some text" > test_file
# git add --all don't add untracked file
echo "=====commit====="
git commit -am "innitial commit"
echo "=====log====="
git log

cd ..
rm -r commit_example
=====commit=====
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test_file

nothing added to commit but untracked files present (use "git add" to track)
=====log=====
fatal: your current branch 'master' does not have any commits yet

Update tip commit (--amend)#

You can replace tip commit of the current branch with new commit. You can think about it like you added new changes to the last commit.

To perform it you have to use --amend option for git commit command.

So in the following example:

  • New repository has been initiated and first commit created;

  • Some changes have been made - this is shown in the git status output;

  • But by using git add and git commit --amend changes have been committed;

  • Because of the --amend option, there is still only one commit in the git log output.

%%bash
mkdir commit_amend
cd commit_amend

git init &> /dev/null

# initial commit
echo "first line" > new_file
git add --all
git commit -m "test commit" &> /dev/null

echo "new line" >> new_file
echo "=====file changed====="
git status

git add --all
git commit --amend -m "test commit change" &> /dev/null

echo
echo "======git status====="
git status
echo
echo "=====git log====="
git log --all --graph --oneline

cd ..
rm -r commit_amend
=====file changed=====
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   new_file

no changes added to commit (use "git add" and/or "git commit -a")

======git status=====
On branch master
nothing to commit, working tree clean

=====git log=====
* fc660b7 test commit change