Difference between revisions of "Tutorial Git Commit"

From HaFrWiki
Jump to: navigation, search
(Created page with "{{TOCright}} == See also == <span class="editsection">top</span> == Reference == <span class="editsection">top</span> <references/> [[Category:Ind...")
 
m
Line 1: Line 1:
 
{{TOCright}}
 
{{TOCright}}
 +
 +
== Introduction ==
 +
Set up the repo.
 +
<syntaxhighlight lang="bash">
 +
# Setup test repository
 +
$ mkdir /test/tutgitcommitall
 +
$ cd /test/tutgitcommitall
 +
$ git init
 +
Initialized empty Git repository in /Test/tutgitcommitall/.git/
 +
 +
$ echo something >> ready
 +
$ echo something else >> notyet
 +
$ git add ready notyet
 +
$ git commit -m "Setup"
 +
[master (root-commit) 5105eaf] Setup
 +
2 files changed, 2 insertions(+)
 +
create mode 100644 notyet
 +
create mode 100644 ready
 +
</syntaxhighlight>
 +
 +
== Making changes ==
 +
<syntaxhighlight lang="bash">
 +
# Modify file "ready" and "git add" it to the index
 +
$ nano ready
 +
...
 +
$ git add ready
 +
 +
# Modify file "notyet", leaving it unstaged.
 +
$ nano notyet
 +
...
 +
# Add a new file in a subdirectory, but don't add it.
 +
$ mkdir subdir
 +
$ echo Nope >> subdir/new
 +
 +
# Use git status to see what a regular commit (without command line options) would do:
 +
$ git status
 +
# On branch master
 +
# Changes to be committed:
 +
#  (use "git reset HEAD <file>..." to unstage)
 +
#
 +
# modified:  ready
 +
#
 +
# Changes not staged for commit:
 +
#  (use "git add <file>..." to update what will be committed)
 +
#  (use "git checkout -- <file>..." to discard changes in working directory)
 +
#
 +
# modified:  notyet
 +
#
 +
# Untracked files:
 +
#  (use "git add <file>..." to include in what will be committed)
 +
#
 +
# subdir/
 +
</syntaxhighlight>
 +
 +
Here, the '''index''' is prepared to commit just the one file named '''ready''', because it’s the only file that has been staged.
 +
 +
However, if you run {{FormFCT|9|blue|git commit --all}}, Git recursively traverses the entire repository, stages all known, modified files and commits those.
 +
<br>In this case, when your editor presents the commit message template, it should indicate that the modified and known file notyet will, in fact, be committed as well:
 +
<syntaxhighlight lang="bash">
 +
$ git commit --all
 +
....
 +
# Please enter the commit message for your changes. Lines starting
 +
# with '#' will be ignored, and an empty message aborts the commit.
 +
# On branch master
 +
# Changes to be committed:
 +
#      modified:  notyet
 +
#      modified:  ready
 +
#
 +
# Untracked files:
 +
#      subdir/
 +
#
 +
 +
$ git status
 +
On branch master
 +
Untracked files:
 +
  (use "git add <file>..." to include in what will be committed)
 +
 +
subdir/
 +
 +
nothing added to commit but untracked files present (use "git add" to track)
 +
</syntaxhighlight>
 +
 +
Finally, because the directory named subdir/ is new and no file name or path within is tracked, not even the --all option causes it to be committed.
 +
<br>While Git recursively traverses the repository looking for modified and removed files, the completely new file subdir/ directory and all of its files do not become part of the commit.
 +
  
 
== See also ==
 
== See also ==

Revision as of 15:27, 8 July 2017

Introduction

Set up the repo. <syntaxhighlight lang="bash">

  1. Setup test repository

$ mkdir /test/tutgitcommitall $ cd /test/tutgitcommitall $ git init Initialized empty Git repository in /Test/tutgitcommitall/.git/

$ echo something >> ready $ echo something else >> notyet $ git add ready notyet $ git commit -m "Setup" [master (root-commit) 5105eaf] Setup

2 files changed, 2 insertions(+)
create mode 100644 notyet
create mode 100644 ready

</syntaxhighlight>

Making changes

<syntaxhighlight lang="bash">

  1. Modify file "ready" and "git add" it to the index

$ nano ready ... $ git add ready

  1. Modify file "notyet", leaving it unstaged.

$ nano notyet ...

  1. Add a new file in a subdirectory, but don't add it.

$ mkdir subdir $ echo Nope >> subdir/new

  1. Use git status to see what a regular commit (without command line options) would do:

$ git status

  1. On branch master
  2. Changes to be committed:
  3. (use "git reset HEAD <file>..." to unstage)
  4. modified: ready
  5. Changes not staged for commit:
  6. (use "git add <file>..." to update what will be committed)
  7. (use "git checkout -- <file>..." to discard changes in working directory)
  8. modified: notyet
  9. Untracked files:
  10. (use "git add <file>..." to include in what will be committed)
  11. subdir/

</syntaxhighlight>

Here, the index is prepared to commit just the one file named ready, because it’s the only file that has been staged.

However, if you run git commit --all, Git recursively traverses the entire repository, stages all known, modified files and commits those.
In this case, when your editor presents the commit message template, it should indicate that the modified and known file notyet will, in fact, be committed as well: <syntaxhighlight lang="bash"> $ git commit --all ....

  1. Please enter the commit message for your changes. Lines starting
  2. with '#' will be ignored, and an empty message aborts the commit.
  3. On branch master
  4. Changes to be committed:
  5. modified: notyet
  6. modified: ready
  7. Untracked files:
  8. subdir/

$ git status On branch master Untracked files:

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

subdir/

nothing added to commit but untracked files present (use "git add" to track) </syntaxhighlight>

Finally, because the directory named subdir/ is new and no file name or path within is tracked, not even the --all option causes it to be committed.
While Git recursively traverses the repository looking for modified and removed files, the completely new file subdir/ directory and all of its files do not become part of the commit.


See also

top

Reference

top