-
1. Per Iniziare
- 1.1 Il Controllo di Versione
- 1.2 Una Breve Storia di Git
- 1.3 CosâĂ© Git?
- 1.4 La riga di comando
- 1.5 Installing Git
- 1.6 First-Time Git Setup
- 1.7 Chiedere aiuto
- 1.8 Sommario
-
2. Git Basics
- 2.1 Getting a Git Repository
- 2.2 Recording Changes to the Repository
- 2.3 Viewing the Commit History
- 2.4 Undoing Things
- 2.5 Working with Remotes
- 2.6 Tagging
- 2.7 Git Aliases
- 2.8 Sommario
-
3. Git Branching
- 3.1 Branches in a Nutshell
- 3.2 Basic Branching and Merging
- 3.3 Branch Management
- 3.4 Branching Workflows
- 3.5 Remote Branches
- 3.6 Rebasing
- 3.7 Summary
-
4. Git on the Server
- 4.1 The Protocols
- 4.2 Getting Git on a Server
- 4.3 Generating Your SSH Public Key
- 4.4 Setting Up the Server
- 4.5 Git Daemon
- 4.6 Smart HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 Third Party Hosted Options
- 4.10 Summary
-
5. Distributed Git
- 5.1 Distributed Workflows
- 5.2 Contributing to a Project
- 5.3 Maintaining a Project
- 5.4 Summary
-
6. GitHub
-
7. Git Tools
- 7.1 Revision Selection
- 7.2 Interactive Staging
- 7.3 Stashing and Cleaning
- 7.4 Signing Your Work
- 7.5 Searching
- 7.6 Rewriting History
- 7.7 Reset Demystified
- 7.8 Advanced Merging
- 7.9 Rerere
- 7.10 Debugging with Git
- 7.11 Submodules
- 7.12 Bundling
- 7.13 Replace
- 7.14 Credential Storage
- 7.15 Summary
-
8. Customizing Git
- 8.1 Git Configuration
- 8.2 Git Attributes
- 8.3 Git Hooks
- 8.4 An Example Git-Enforced Policy
- 8.5 Summary
-
9. Git and Other Systems
- 9.1 Git as a Client
- 9.2 Migrating to Git
- 9.3 Summary
-
10. Git Internals
- 10.1 Plumbing and Porcelain
- 10.2 Git Objects
- 10.3 Git References
- 10.4 Packfiles
- 10.5 The Refspec
- 10.6 Transfer Protocols
- 10.7 Maintenance and Data Recovery
- 10.8 Environment Variables
- 10.9 Summary
-
A1. Appendice A: Git in altri contesti
- A1.1 Graphical Interfaces
- A1.2 Git in Visual Studio
- A1.3 Git in Eclipse
- A1.4 Git in Bash
- A1.5 Git in Zsh
- A1.6 Git in Powershell
- A1.7 Riassunto
-
A2. Appendice B: Embedding Git in your Applications
- A2.1 Command-line Git
- A2.2 Libgit2
- A2.3 JGit
-
A3. Appendice C: Git Commands
- A3.1 Setup and Config
- A3.2 Getting and Creating Projects
- A3.3 Basic Snapshotting
- A3.4 Branching and Merging
- A3.5 Sharing and Updating Projects
- A3.6 Inspection and Comparison
- A3.7 Debugging
- A3.8 Patching
- A3.9 Email
- A3.10 External Systems
- A3.11 Administration
- A3.12 Plumbing Commands
2.2 Git Basics - Recording Changes to the Repository
Recording Changes to the Repository
You have a bona fide Git repository and a checkout or working copy of the files for that project. You need to make some changes and commit snapshots of those changes into your repository each time the project reaches a state you want to record.
Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else â any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and havenât edited anything.
As you edit files, Git sees them as modified, because youâve changed them since your last commit. You stage these modified files and then commit all your staged changes, and the cycle repeats.
Checking the Status of Your Files
The main tool you use to determine which files are in which state is the git status
command.
If you run this command directly after a clone, you should see something like this:
$ git status
On branch master
nothing to commit, working directory clean
This means you have a clean working directory â in other words, there are no tracked and modified files. Git also doesnât see any untracked files, or they would be listed here. Finally, the command tells you which branch youâre on and informs you that it has not diverged from the same branch on the server. For now, that branch is always âmasterâ, which is the default; you wonât worry about it here. [ch03-git-branching] will go over branches and references in detail.
Letâs say you add a new file to your project, a simple README file.
If the file didnât exist before, and you run git status
, you see your untracked file like so:
$ echo 'My Project' > README
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
nothing added to commit but untracked files present (use "git add" to track)
You can see that your new README file is untracked, because itâs under the âUntracked filesâ heading in your status output. Untracked basically means that Git sees a file you didnât have in the previous snapshot (commit); Git wonât start including it in your commit snapshots until you explicitly tell it to do so. It does this so you donât accidentally begin including generated binary files or other files that you did not mean to include. You do want to start including README, so letâs start tracking the file.
Tracking New Files
In order to begin tracking a new file, you use the command git add
.
To begin tracking the README file, you can run this:
$ git add README
If you run your status command again, you can see that your README file is now tracked and staged to be committed:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
You can tell that itâs staged because itâs under the âChanges to be committedâ heading.
If you commit at this point, the version of the file at the time you ran git add
is what will be in the historical snapshot.
You may recall that when you ran git init
earlier, you then ran git add (files)
â that was to begin tracking files in your directory.
The git add
command takes a path name for either a file or a directory; if itâs a directory, the command adds all the files in that directory recursively.
Staging Modified Files
Letâs change a file that was already tracked.
If you change a previously tracked file called âCONTRIBUTING.mdâ and then run your git status
command again, you get something that looks like this:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
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: CONTRIBUTING.md
The âCONTRIBUTING.mdâ file appears under a section named âChanged but not staged for commitâ â which means that a file that is tracked has been modified in the working directory but not yet staged.
To stage it, you run the git add
command. git add
is a multipurpose command â you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved. It may be helpful to think of it more as âadd this content to the next commitâ rather than âadd this file to the projectâ.
Letâs run git add
now to stage the âCONTRIBUTING.mdâ file, and then run git status
again:
$ git add CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
modified: CONTRIBUTING.md
Both files are staged and will go into your next commit.
At this point, suppose you remember one little change that you want to make in CONTRIBUTING.md
before you commit it.
You open it again and make that change, and youâre ready to commit.
However, letâs run git status
one more time:
$ vim CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
modified: CONTRIBUTING.md
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: CONTRIBUTING.md
What the heck?
Now CONTRIBUTING.md
is listed as both staged and unstaged.
How is that possible?
It turns out that Git stages a file exactly as it is when you run the git add
command.
If you commit now, the version of CONTRIBUTING.md
as it was when you last ran the git add
command is how it will go into the commit, not the version of the file as it looks in your working directory when you run git commit
.
If you modify a file after you run git add
, you have to run git add
again to stage the latest version of the file:
$ git add CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
modified: CONTRIBUTING.md
Short Status
While the git status
output is pretty comprehensive, itâs also quite wordy. Git also has a short status flag so you can see your changes in a more compact way. If you run git status -s
or git status --short
you get a far more simplified output from the command.
$ git status -s
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
New files that arenât tracked have a ??
next to them, new files that have been added to the staging area have an A
, modified files have an M
and so on. There are two columns to the output - the left hand column indicates that the file is staged and the right hand column indicates that itâs modified. So for example in that output, the README
file is modified in the working directory but not yet staged, while the lib/simplegit.rb
file is modified and staged. The Rakefile
was modified, staged and then modified again, so there are changes to it that are both staged and unstaged.
Ignoring Files
Often, youâll have a class of files that you donât want Git to automatically add or even show you as being untracked.
These are generally automatically generated files such as log files or files produced by your build system.
In such cases, you can create a file listing patterns to match them named .gitignore
.
Here is an example .gitignore
file:
$ cat .gitignore
*.[oa]
*~
The first line tells Git to ignore any files ending in â.oâ or â.aâ â object and archive files that may be the product of building your code.
The second line tells Git to ignore all files that end with a tilde (~
), which is used by many text editors such as Emacs to mark temporary files.
You may also include a log, tmp, or pid directory; automatically generated documentation; and so on.
Setting up a .gitignore
file before you get going is generally a good idea so you donât accidentally commit files that you really donât want in your Git repository.
The rules for the patterns you can put in the .gitignore
file are as follows:
-
Blank lines or lines starting with
#
are ignored. -
Standard glob patterns work.
-
You can end patterns with a forward slash (
/
) to specify a directory. -
You can negate a pattern by starting it with an exclamation point (
!
).
Glob patterns are like simplified regular expressions that shells use.
An asterisk (*
) matches zero or more characters; [abc]
matches any character inside the brackets (in this case a, b, or c); a question mark (?
) matches a single character; and brackets enclosing characters separated by a hyphen([0-9]
) matches any character between them (in this case 0 through 9).
You can also use two asterisks to match nested directories; a/**/z
would match a/z
, a/b/z
, a/b/c/z
, and so on.
Here is another example .gitignore file:
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
Suggerimento
|
GitHub maintains a fairly comprehensive list of good |
Viewing Your Staged and Unstaged Changes
If the git status
command is too vague for you â you want to know exactly what you changed, not just which files were changed â you can use the git diff
command.
Weâll cover git diff
in more detail later, but youâll probably use it most often to answer these two questions: What have you changed but not yet staged?
And what have you staged that you are about to commit?
Although git status
answers those questions very generally by listing the file names, git diff
shows you the exact lines added and removed â the patch, as it were.
Letâs say you edit and stage the README
file again and then edit the CONTRIBUTING.md
file without staging it.
If you run your git status
command, you once again see something like this:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
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: CONTRIBUTING.md
To see what youâve changed but not yet staged, type git diff
with no other arguments:
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you're contributing
in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if you patch is
+longer than a dozen lines.
If you are starting to work on a particular area, feel free to submit a PR
that highlights your work in progress (and note in the PR title that it's
That command compares what is in your working directory with what is in your staging area. The result tells you the changes youâve made that you havenât yet staged.
If you want to see what youâve staged that will go into your next commit, you can use git diff --staged
.
This command compares your staged changes to your last commit:
$ git diff --staged
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project
Itâs important to note that git diff
by itself doesnât show all changes made since your last commit â only changes that are still unstaged.
This can be confusing, because if youâve staged all of your changes, git diff
will give you no output.
For another example, if you stage the CONTRIBUTING.md
file and then edit it, you can use git diff
to see the changes in the file that are staged and the changes that are unstaged. If our environment looks like this:
$ git add CONTRIBUTING.md
$ echo 'test line' >> CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: CONTRIBUTING.md
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: CONTRIBUTING.md
Now you can use git diff
to see what is still unstaged
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 643e24f..87f08c8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,3 +119,4 @@ at the
## Starter Projects
See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).
+# test line
and git diff --cached
to see what youâve staged so far (--staged and --cached are synonyms):
$ git diff --cached
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you're contributing
in the first place, you're less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if you patch is
+longer than a dozen lines.
If you are starting to work on a particular area, feel free to submit a PR
that highlights your work in progress (and note in the PR title that it's
Nota
|
Git Diff in an External Tool
We will continue to use the |
Committing Your Changes
Now that your staging area is set up the way you want it, you can commit your changes.
Remember that anything that is still unstaged â any files you have created or modified that you havenât run git add
on since you edited them â wonât go into this commit.
They will stay as modified files on your disk.
In this case, letâs say that the last time you ran git status
, you saw that everything was staged, so youâre ready to commit your changes.
The simplest way to commit is to type git commit
:
$ git commit
Doing so launches your editor of choice.
(This is set by your shellâs $EDITOR
environment variable â usually vim or
emacs, although you can configure it with whatever you want using the git config
--global core.editor
command as you saw in
Per Iniziare).
The editor displays the following text (this example is a Vim screen):
# 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:
# new file: README
# modified: CONTRIBUTING.md
#
~
~
~
".git/COMMIT_EDITMSG" 9L, 283C
You can see that the default commit message contains the latest output of the git status
command commented out and one empty line on top.
You can remove these comments and type your commit message, or you can leave them there to help you remember what youâre committing.
(For an even more explicit reminder of what youâve modified, you can pass the -v
option to git commit
.
Doing so also puts the diff of your change in the editor so you can see exactly what changes youâre committing.)
When you exit the editor, Git creates your commit with that commit message (with the comments and diff stripped out).
Alternatively, you can type your commit message inline with the commit
command by specifying it after a -m flag, like this:
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
2 files changed, 2 insertions(+)
create mode 100644 README
Now youâve created your first commit!
You can see that the commit has given you some output about itself: which branch you committed to (master
), what SHA-1 checksum the commit has (463dc4f
), how many files were changed, and statistics about lines added and removed in the commit.
Remember that the commit records the snapshot you set up in your staging area. Anything you didnât stage is still sitting there modified; you can do another commit to add it to your history. Every time you perform a commit, youâre recording a snapshot of your project that you can revert to or compare to later.
Skipping the Staging Area
Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow.
If you want to skip the staging area, Git provides a simple shortcut.
Adding the -a
option to the git commit
command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add
part:
$ git status
On branch master
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: CONTRIBUTING.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
1 file changed, 5 insertions(+), 0 deletions(-)
Notice how you donât have to run git add
on the âCONTRIBUTING.mdâ file in this case before you commit.
Removing Files
To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit.
The git rm
command does that, and also removes the file from your working directory so you donât see it as an untracked file the next time around.
If you simply remove the file from your working directory, it shows up under the âChanged but not updatedâ (that is, unstaged) area of your git status
output:
$ rm PROJECTS.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: PROJECTS.md
no changes added to commit (use "git add" and/or "git commit -a")
Then, if you run git rm
, it stages the fileâs removal:
$ git rm PROJECTS.md
rm 'PROJECTS.md'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: PROJECTS.md
The next time you commit, the file will be gone and no longer tracked.
If you modified the file and added it to the index already, you must force the removal with the -f
option.
This is a safety feature to prevent accidental removal of data that hasnât yet been recorded in a snapshot and that canât be recovered from Git.
Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area.
In other words, you may want to keep the file on your hard drive but not have Git track it anymore.
This is particularly useful if you forgot to add something to your .gitignore
file and accidentally staged it, like a large log file or a bunch of .a
compiled files.
To do this, use the --cached
option:
$ git rm --cached README
You can pass files, directories, and file-glob patterns to the git rm
command.
That means you can do things such as
$ git rm log/\*.log
Note the backslash (\
) in front of the *
.
This is necessary because Git does its own filename expansion in addition to your shellâs filename expansion.
This command removes all files that have the .log
extension in the log/
directory.
Or, you can do something like this:
$ git rm \*~
This command removes all files that end with ~
.
Moving Files
Unlike many other VCS systems, Git doesnât explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact â weâll deal with detecting file movement a bit later.
Thus itâs a bit confusing that Git has a mv
command.
If you want to rename a file in Git, you can run something like
$ git mv file_from file_to
and it works fine. In fact, if you run something like this and look at the status, youâll see that Git considers it a renamed file:
$ git mv README.md README
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
However, this is equivalent to running something like this:
$ mv README.md README
$ git rm README.md
$ git add README
Git figures out that itâs a rename implicitly, so it doesnât matter if you rename a file that way or with the mv
command.
The only real difference is that mv
is one command instead of three â itâs a convenience function.
More important, you can use any tool you like to rename a file, and address the add/rm later, before you commit.