GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing git-scm.com — Git - gitcore-tutorial Documentation
English ▾
Topics ▾
Setup and ConfigGetting and Creating ProjectsBasic SnapshottingBranching and MergingSharing and Updating ProjectsInspection and ComparisonPatchingDebuggingExternal SystemsServer AdminGuides
AdministrationPlumbing Commands
DESCRIPTIONThis tutorial explains how to use the "core" Git commands to set up and work with a Git repository. If you just need to use Git as a revision control system you may prefer to start with "A Tutorial Introduction to Git" (gittutorial[7]) or the Git User Manual. However, an understanding of these low-level tools can be helpful if you want to understand Git’s internals. The core Git is often called "plumbing", with the prettier user interfaces on top of it called "porcelain". You may not want to use the plumbing directly very often, but it can be good to know what the plumbing does when the porcelain isn’t flushing. Back when this document was originally written, many porcelain commands were shell scripts. For simplicity, it still uses them as examples to illustrate how plumbing is fit together to form the porcelain commands. The source tree includes some of these scripts in contrib/examples/ for reference. Although these are not implemented as shell scripts anymore, the description of what the plumbing layer commands do is still valid.
Creating a Git repositoryCreating a new Git repository couldn’t be easier: all Git repositories start out empty, and the only thing you need to do is find yourself a subdirectory that you want to use as a working tree - either an empty one for a totally new project, or an existing working tree that you want to import into Git. For our first example, we’re going to start a totally new repository from scratch, with no pre-existing files, and we’ll call it git-tutorial. To start up, create a subdirectory for it, change into that subdirectory, and initialize the Git infrastructure with git init: $ mkdir git-tutorial $ cd git-tutorial $ git init to which Git will reply Initialized empty Git repository in .git/ which is just Git’s way of saying that you haven’t been doing anything
strange, and that it will have created a local
In particular, the One note: the special However, this is only a convention, and you can name your branches
anything you want, and don’t have to ever even have a
You have now created your first Git repository. Of course, since it’s empty, that’s not very useful, so let’s start populating it with data. Populating a Git repositoryWe’ll keep this simple and stupid, so we’ll start off with populating a few trivial files just to get a feel for it. Start off with just creating any random files that you want to maintain in your Git repository. We’ll start off with a few bad examples, just to get a feel for how this works: $ echo "Hello World" >hello $ echo "Silly example" >example you have now created two files in your working tree (aka working directory), but to actually check in your hard work, you will have to go through two steps:
The first step is trivial: when you want to tell Git about any changes
to your working tree, you use the git update-index program. That
program normally just takes a list of filenames you want to update, but
to avoid trivial mistakes, it refuses to add new entries to the index
(or remove existing ones) unless you explicitly tell it that you’re
adding a new entry with the So to populate the index with the two files you just created, you can do $ git update-index --add hello example and you have now told Git to track those two files. In fact, as you did that, if you now look into your object directory, you’ll notice that Git will have added two new objects to the object database. If you did exactly the steps above, you should now be able to do $ ls .git/objects/??/* and see two files: .git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962 which correspond with the objects with names of If you want to, you can use git cat-file to look at those objects, but you’ll have to use the object name, not the filename of the object: $ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238 where the $ git cat-file blob 557db03 which will print out "Hello World". The object
Anyway, as we mentioned previously, you normally never actually take a look at the objects themselves, and typing long 40-character hex names is not something you’d normally want to do. The above digression was just to show that git update-index did something magical, and actually saved away the contents of your files into the Git object database. Updating the index did something else too: it created a However, since Git knows about them, you can now start using some of the most basic Git commands to manipulate the files or look at their status. In particular, let’s not even check in the two files into Git yet, we’ll
start off by adding another line to $ echo "It's a new day for git" >>hello and you can now, since you told Git about the previous state of $ git diff-files Oops. That wasn’t very readable. It just spit out its own internal version of a diff, but that internal version really just tells you that it has noticed that "hello" has been modified, and that the old object contents it had have been replaced with something else. To make it readable, we can tell git diff-files to output the
differences as a patch, using the $ git diff-files -p diff --git a/hello b/hello index 557db03..263414f 100644 --- a/hello +++ b/hello @@ -1 +1,2 @@ Hello World +It's a new day for git i.e. the diff of the change we caused by adding another line to In other words, git diff-files always shows us the difference between what is recorded in the index, and what is currently in the working tree. That’s very useful. A common shorthand for $ git diff diff --git a/hello b/hello index 557db03..263414f 100644 --- a/hello +++ b/hello @@ -1 +1,2 @@ Hello World +It's a new day for git Committing Git stateNow, we want to go to the next stage in Git, which is to take the files that Git knows about in the index, and commit them as a real tree. We do that in two phases: creating a tree object, and committing that tree object as a commit object together with an explanation of what the tree was all about, along with information of how we came to that state. Creating a tree object is trivial, and is done with git write-tree.
There are no options or other input: $ git write-tree and this will just output the name of the resulting tree, in this case (if you have done exactly as I’ve described) it should be 8988da15d077d4829fc51d8544c097def6644dbb which is another incomprehensible object name. Again, if you want to,
you can use However — normally you’d never use git write-tree on its own, because normally you always commit a tree into a commit object using the git commit-tree command. In fact, it’s easier to not actually use git write-tree on its own at all, but to just pass its result in as an argument to git commit-tree. git commit-tree normally takes several arguments — it wants to know what the parent of a commit was, but since this is the first commit ever in this new repository, and it has no parents, we only need to pass in the object name of the tree. However, git commit-tree also wants to get a commit message on its standard input, and it will write out the resulting object name for the commit to its standard output. And this is where we create the $ tree=$(git write-tree) $ commit=$(echo 'Initial commit' | git commit-tree $tree) $ git update-ref HEAD $commit In this case this creates a totally new commit that is not related to anything else. Normally you do this only once for a project ever, and all later commits will be parented on top of an earlier commit. Again, normally you’d never actually do this by hand. There is a
helpful script called Making a changeRemember how we did the git update-index on file Further, remember how I said that git write-tree writes the contents
of the index file to the tree, and thus what we just committed was in
fact the original contents of the file As before, if we do Unlike git diff-files, which showed the difference between the index file and the working tree, git diff-index shows the differences between a committed tree and either the index file or the working tree. In other words, git diff-index wants a tree to be diffed against, and before we did the commit, we couldn’t do that, because we didn’t have anything to diff against. But now we can do $ git diff-index -p HEAD (where Again, because this is a common operation, you can also just shorthand it with $ git diff HEAD which ends up doing the above for you. In other words, git diff-index normally compares a tree against the
working tree, but when given the
However, our next step is to commit the change we did, and again, to understand what’s going on, keep in mind the difference between "working tree contents", "index file" and "committed tree". We have changes in the working tree that we want to commit, and we always have to work through the index file, so the first thing we need to do is to update the index cache: $ git update-index hello (note how we didn’t need the Note what happens to the different git diff-* versions here.
After we’ve updated Now, since we’ve updated $ git commit which starts an editor for you to write the commit message and tells you a bit about what you have done. Write whatever message you want, and all the lines that start with #
will be pruned out, and the rest will be used as the commit message for
the change. If you decide you don’t want to commit anything after all at
this point (you can continue to edit things and update the index), you
can just leave an empty message. Otherwise You’ve now made your first real Git commit. And if you’re interested in
looking at what Inspecting ChangesWhile creating changes is useful, it’s even more useful if you can tell later what changed. The most useful command for this is another of the diff family, namely git diff-tree. git diff-tree can be given two arbitrary trees, and it will tell you the differences between them. Perhaps even more commonly, though, you can give it just a single commit object, and it will figure out the parent of that commit itself, and show the difference directly. Thus, to get the same diff that we’ve already seen several times, we can now do $ git diff-tree -p HEAD (again,
More interestingly, you can also give git diff-tree the In fact, together with the git rev-list program (which generates a
list of revisions), git diff-tree ends up being a veritable fount of
changes. You can emulate Tagging a versionIn Git, there are two kinds of tags, a "light" one, and an "annotated tag". A "light" tag is technically nothing more than a branch, except we put
it in the $ git tag my-first-tag which just writes the current $ git diff my-first-tag to diff your current state against that tag which at this point will obviously be an empty diff, but if you continue to develop and commit stuff, you can use your tag as an "anchor-point" to see what has changed since you tagged it. An "annotated tag" is actually a real Git object, and contains not only a
pointer to the state you want to tag, but also a small tag name and
message, along with optionally a PGP signature that says that yes,
you really did
that tag. You create these annotated tags with either the $ git tag -s <tagname> which will sign the current You normally only do signed tags for major releases or things like that, while the light-weight tags are useful for any marking you want to do — any time you decide that you want to remember a certain point, just create a private tag for it, and you have a nice symbolic name for the state at that point. Copying repositoriesGit repositories are normally totally self-sufficient and relocatable.
Unlike CVS, for example, there is no separate notion of
"repository" and "working tree". A Git repository normally is the
working tree, with the local Git information hidden in the
This has two implications:
Note that the second point is true even across machines. You can duplicate a remote Git repository with any regular copy mechanism, be it scp, rsync or wget. When copying a remote repository, you’ll want to at a minimum update the index cache when you do this, and especially with other peoples' repositories you often want to make sure that the index cache is in some known state (you don’t know what they’ve done and not yet checked in), so usually you’ll precede the git update-index with a $ git read-tree --reset HEAD $ git update-index --refresh which will force a total index re-build from the tree pointed to by The above can also be written as simply $ git reset and in fact a lot of the common Git command combinations can be scripted
with the Many (most?) public remote repositories will not contain any of
the checked out files or even an index file, and will only contain the
actual core Git files. Such a repository usually doesn’t even have the
To create your own local live copy of such a "raw" Git repository, you’d
first create your own subdirectory for the project, and then copy the
raw repository contents into the $ mkdir my-git $ cd my-git $ rsync -rL rsync://rsync.kernel.org/pub/scm/git/git.git/ .git followed by $ git read-tree HEAD to populate the index. However, now you have populated the index, and you have all the Git internal files, but you will notice that you don’t actually have any of the working tree files to work on. To get those, you’d check them out with $ git checkout-index -u -a where the Again, this can all be simplified with $ git clone git://git.kernel.org/pub/scm/git/git.git/ my-git $ cd my-git $ git checkout which will end up doing all of the above for you. You have now successfully copied somebody else’s (mine) remote repository, and checked it out. Creating a new branchBranches in Git are really nothing more than pointers into the Git
object database from within the You can at any time create a new branch by just picking an arbitrary
point in the project history, and just writing the SHA-1 name of that
object into a file under To show that as an example, let’s go back to the git-tutorial repository we used earlier, and create a branch in it. You do that by simply just saying that you want to check out a new branch: $ git switch -c mybranch will create a new branch based at the current
You can always just jump back to your original $ git switch master (or any other branch-name, for that matter) and if you forget which branch you happen to be on, a simple $ cat .git/HEAD will tell you where it’s pointing. To get the list of branches you have, you can say $ git branch which used to be nothing more than a simple script around Sometimes you may wish to create a new branch without actually checking it out and switching to it. If so, just use the command $ git branch <branchname> [startingpoint] which will simply create the branch, but will not do anything further. You can then later — once you decide that you want to actually develop on that branch — switch to that branch with a regular git switch with the branchname as the argument. Merging two branchesOne of the ideas of having a branch is that you do some (possibly
experimental) work in it, and eventually merge it back to the main
branch. So assuming you created the above $ git switch mybranch $ echo "Work, work, work" >>hello $ git commit -m "Some work." -i hello Here, we just added another line to Now, to make it a bit more interesting, let’s assume that somebody else does some work in the original branch, and simulate that by going back to the master branch, and editing the same file differently there: $ git switch master Here, take a moment to look at the contents of $ echo "Play, play, play" >>hello $ echo "Lots of fun" >>example $ git commit -m "Some fun." -i hello example since the master branch is obviously in a much better mood. Now, you’ve got two branches, and you decide that you want to merge the work done. Before we do that, let’s introduce a cool graphical tool that helps you view what’s going on: $ gitk --all will show you graphically both of your branches (that’s what the Anyway, let’s exit gitk ( $ git merge -m "Merge work in mybranch" mybranch where the first argument is going to be used as the commit message if the merge can be resolved automatically. Now, in this case we’ve intentionally created a situation where the
merge will need to be fixed up by hand, though, so Git will do as much
of it as it can automatically (which in this case is just merge the Auto-merging hello CONFLICT (content): Merge conflict in hello Automatic merge failed; fix conflicts and then commit the result. It tells you that it did an "Automatic merge", which
failed due to conflicts in Not to worry. It left the (trivial) conflict in Hello World It's a new day for git Play, play, play Work, work, work and once you’re happy with your manual merge, just do a $ git commit -i hello which will very loudly warn you that you’re now committing a merge (which is correct, so never mind), and you can write a small merge message about your adventures in git merge-land. After you’re done, start up Another useful tool, especially if you do not always work in X-Window
environment, is $ git show-branch --topo-order --more=1 master mybranch * [master] Merge work in mybranch ! [mybranch] Some work. -- - [master] Merge work in mybranch *+ [mybranch] Some work. * [master^] Some fun. The first two lines indicate that it is showing the two branches
with the titles of their top-of-the-tree commits, you are currently on
Now, let’s pretend you are the one who did all the work in
$ git switch mybranch $ git merge -m "Merge upstream changes." master This outputs something like this (the actual commit object names would be different) Updating from ae3a2da... to a80b4aa.... Fast-forward (no commit created; -m option ignored) example | 1 + hello | 1 + 2 files changed, 2 insertions(+) Because your branch did not contain anything more than what had
already been merged into the You can run $ git show-branch master mybranch ! [master] Merge work in mybranch * [mybranch] Merge work in mybranch -- -- [master] Merge work in mybranch Merging external workIt’s usually much more common that you merge with somebody else than merging with your own branches, so it’s worth pointing out that Git makes that very easy too, and in fact, it’s not that different from doing a git merge. In fact, a remote merge ends up being nothing more than "fetch the work from a remote repository into a temporary tag" followed by a git merge. Fetching from a remote repository is done by, unsurprisingly, git fetch: $ git fetch <remote-repository> One of the following transports can be used to name the repository to download from:
Once you fetch from the remote repository, you However — it’s such a common thing to $ git pull <remote-repository> and optionally give a branch-name for the remote end as a second argument.
It is likely that you will be pulling from the same remote repository from time to time. As a short hand, you can store the remote repository URL in the local repository’s config file like this: $ git config remote.linus.url https://git.kernel.org/pub/scm/git/git.git/ and use the "linus" keyword with git pull instead of the full URL. Examples.
the above are equivalent to:
How does the merge work?We said this tutorial shows what plumbing does to help you cope with the porcelain that isn’t flushing, but we so far did not talk about how the merge really works. If you are following this tutorial the first time, I’d suggest to skip to "Publishing your work" section and come back here later. OK, still with me? To give us an example to look at, let’s go back to the earlier repository with "hello" and "example" file, and bring ourselves back to the pre-merge state: $ git show-branch --more=2 master mybranch ! [master] Merge work in mybranch * [mybranch] Merge work in mybranch -- -- [master] Merge work in mybranch +* [master^2] Some work. +* [master^] Some fun. Remember, before running git merge, our $ git switch -C mybranch master^2 $ git switch master $ git reset --hard master^ After rewinding, the commit structure should look like this: $ git show-branch * [master] Some fun. ! [mybranch] Some work. -- * [master] Some fun. + [mybranch] Some work. *+ [master^] Initial commit Now we are ready to experiment with the merge by hand.
$ mb=$(git merge-base HEAD mybranch) The command writes the commit object name of the common ancestor to the standard output, so we captured its output to a variable, because we will be using it in the next step. By the way, the common ancestor commit is the "Initial commit" commit in this case. You can tell it by: $ git name-rev --name-only --tags $mb my-first-tag After finding out a common ancestor commit, the second step is this: $ git read-tree -m -u $mb HEAD mybranch This is the same git read-tree command we have already seen, but it takes three trees, unlike previous examples. This reads the contents of each tree into different stage in the index file (the first tree goes to stage 1, the second to stage 2, etc.). After reading three trees into three stages, the paths that are the same in all three stages are collapsed into stage 0. Also paths that are the same in two of three stages are collapsed into stage 0, taking the SHA-1 from either stage 2 or stage 3, whichever is different from stage 1 (i.e. only one side changed from the common ancestor). After collapsing operation, paths that are different in three trees are left in non-zero stages. At this point, you can inspect the index file with this command: $ git ls-files --stage 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello In our example of only two files, we did not have unchanged files so only example resulted in collapsing. But in real-life large projects, when only a small number of files change in one commit, this collapsing tends to trivially merge most of the paths fairly quickly, leaving only a handful of real changes in non-zero stages. To look at only non-zero stages, use $ git ls-files --unmerged 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello The next step of merging is to merge these three versions of the file, using 3-way merge. This is done by giving git merge-one-file command as one of the arguments to git merge-index command: $ git merge-index git-merge-one-file hello Auto-merging hello ERROR: Merge conflict in hello fatal: merge program failed git merge-one-file script is called with parameters to
describe those three versions, and is responsible to leave the
merge results in the working tree.
It is a fairly straightforward shell script, and
eventually calls merge program from RCS suite to perform a
file-level 3-way merge. In this case, merge detects
conflicts, and the merge result with conflict marks is left in
the working tree.. This can be seen if you run $ git ls-files --stage 100644 7f8b141b65fdcee47321e399a2598a235a032422 0 example 100644 557db03de997c86a4a028e1ebd3a1ceb225be238 1 hello 100644 ba42a2a96e3027f3333e13ede4ccf4498c3ae942 2 hello 100644 cc44c73eb783565da5831b4d820c962954019b69 3 hello This is the state of the index file and the working file after
git merge returns control back to you, leaving the conflicting
merge for you to resolve. Notice that the path Publishing your workSo, we can use somebody else’s work from a remote repository, but how can you prepare a repository to let other people pull from it? You do your real work in your working tree that has your
primary repository hanging under it as its
Publishing the changes from your local (private) repository to your remote (public) repository requires a write privilege on the remote machine. You need to have an SSH account there to run a single command, git-receive-pack. First, you need to create an empty repository on the remote machine that will house your public repository. This empty repository will be populated and be kept up to date by pushing into it later. Obviously, this repository creation needs to be done only once.
Your private repository’s Git directory is usually $ mkdir my-git.git Then, make that directory into a Git repository by running
git init, but this time, since its name is not the usual
$ GIT_DIR=my-git.git git init Make sure this directory is available for others you want your
changes to be pulled via the transport of your choice. Also
you need to make sure that you have the git-receive-pack
program on the
Your "public repository" is now ready to accept your changes. Come back to the machine you have your private repository. From there, run this command: $ git push <public-host>:/path/to/my-git.git master This synchronizes your public repository to match the named
branch head (i.e. As a real example, this is how I update my public Git repository. Kernel.org mirror network takes care of the propagation to other publicly visible machines: $ git push master.kernel.org:/pub/scm/git/git.git/ Packing your repositoryEarlier, we saw that one file under .git/objects/??/ directory is stored for each Git object you create. This representation is efficient to create atomically and safely, but not so convenient to transport over the network. Since Git objects are immutable once they are created, there is a way to optimize the storage by "packing them together". The command $ git repack will do it for you. If you followed the tutorial examples, you
would have accumulated about 17 objects in .git/objects/??/
directories by now. git repack tells you how many objects it
packed, and stores the packed file in the
If you are paranoid, running git verify-pack command would detect if you have a corrupt pack, but do not worry too much. Our programs are always perfect ;-). Once you have packed objects, you do not need to leave the unpacked objects that are contained in the pack file anymore. $ git prune-packed would remove them for you. You can try running
If you run When a repository is synchronized via Working with OthersAlthough Git is a truly distributed system, it is often convenient to organize your project with an informal hierarchy of developers. Linux kernel development is run this way. There is a nice illustration (page 17, "Merges to Mainline") in Randy Dunlap’s presentation. It should be stressed that this hierarchy is purely informal. There is nothing fundamental in Git that enforces the "chain of patch flow" this hierarchy implies. You do not have to pull from only one remote repository. A recommended workflow for a "project lead" goes like this:
A recommended work cycle for a "subsystem maintainer" who works on that project and has an own "public repository" goes like this:
A recommended work cycle for an "individual developer" who does not have a "public" repository is somewhat different. It goes like this:
Working with Others, Shared Repository StyleIf you are coming from a CVS background, the style of cooperation suggested in the previous section may be new to you. You do not have to worry. Git supports the "shared public repository" style of cooperation you are probably more familiar with as well. See gitcvs-migration[7] for the details. Bundling your work togetherIt is likely that you will be working on more than one thing at a time. It is easy to manage those more-or-less independent tasks using branches with Git. We have already seen how branches work previously, with "fun and work" example using two branches. The idea is the same if there are more than two branches. Let’s say you started out from "master" head, and have some new code in the "master" branch, and two independent fixes in the "commit-fix" and "diff-fix" branches: $ git show-branch ! [commit-fix] Fix commit message normalization. ! [diff-fix] Fix rename detection. * [master] Release candidate #1 --- + [diff-fix] Fix rename detection. + [diff-fix~1] Better common substring algorithm. + [commit-fix] Fix commit message normalization. * [master] Release candidate #1 ++* [diff-fix~2] Pretty-print messages. Both fixes are tested well, and at this point, you want to merge in both of them. You could merge in diff-fix first and then commit-fix next, like this: $ git merge -m "Merge fix in diff-fix" diff-fix $ git merge -m "Merge fix in commit-fix" commit-fix Which would result in: $ git show-branch ! [commit-fix] Fix commit message normalization. ! [diff-fix] Fix rename detection. * [master] Merge fix in commit-fix --- - [master] Merge fix in commit-fix + * [commit-fix] Fix commit message normalization. - [master~1] Merge fix in diff-fix +* [diff-fix] Fix rename detection. +* [diff-fix~1] Better common substring algorithm. * [master~2] Release candidate #1 ++* [master~3] Pretty-print messages. However, there is no particular reason to merge in one branch first and the other next, when what you have are a set of truly independent changes (if the order mattered, then they are not independent by definition). You could instead merge those two branches into the current branch at once. First let’s undo what we just did and start over. We would want to get the master branch before these two merges by resetting it to master~2: $ git reset --hard master~2 You can make sure $ git merge commit-fix diff-fix $ git show-branch ! [commit-fix] Fix commit message normalization. ! [diff-fix] Fix rename detection. * [master] Octopus merge of branches 'diff-fix' and 'commit-fix' --- - [master] Octopus merge of branches 'diff-fix' and 'commit-fix' + * [commit-fix] Fix commit message normalization. +* [diff-fix] Fix rename detection. +* [diff-fix~1] Better common substring algorithm. * [master~1] Release candidate #1 ++* [master~2] Pretty-print messages. Note that you should not do Octopus just because you can. An octopus is a valid thing to do and often makes it easier to view the commit history if you are merging more than two independent changes at the same time. However, if you have merge conflicts with any of the branches you are merging in and need to hand resolve, that is an indication that the development happened in those branches were not independent after all, and you should merge two at a time, documenting how you resolved the conflicts, and the reason why you preferred changes made in one side over the other. Otherwise it would make the project history harder to follow, not easier. GITPart of the git[1] suite gitcore-tutorial | ||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |