text stringlengths 0 3.84k |
|---|
Pro Git Scott Chacon, Ben Straub Version 2. 1. 430, 2024-06-04 |
Table of Contents License..................................................................................... 1 Preface by Scott Chacon..................................................................... 2 Preface by Ben Straub....................................................................... 3 Dedications...... |
Smart HTTP............................................................................ 117 Git Web................................................................................ 119 Git Lab................................................................................. 121 Third Party Hosted Options.................. |
Migrating to Git........................................................................ 399 Summary.............................................................................. 413 Git Internals.............................................................................. 414 Plumbing and Porcelain................... |
Index.................................................................................... 493 |
License This work is licensed under the Creative Commons Attribution-Non Commercial-Share Alike 3. 0 Unported License. To view a copy of this license, visit https://creativecommons. org/licenses/by-nc-sa/3. 0 or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. 1 |
Preface by Scott Chacon Welcome to the second edition of Pro Git. The first edition was published over four years ago now. Since then a lot has changed and yet many important things have not. While most of the core commands and concepts are still valid today as the Git core team is pretty fantastic at keeping things ba... |
Preface by Ben Straub The first edition of this book is what got me hooked on Git. This was my introduction to a style of making software that felt more natural than anything I had seen before. I had been a developer for several years by then, but this was the right turn that sent me down a much more interesting path t... |
Dedications To my wife, Becky, without whom this adventure never would have begun. — Ben This edition is dedicated to my girls. To my wife Jessica who has supported me for all of these years and to my daughter Josephine, who will support me when I'm too old to know what's going on. — Scott 4 |
Contributors Since this is an Open Source book, we have gotten several errata and content changes donated over the years. Here are all the people who have contributed to the English version of Pro Git as an open source project. Thank you everyone for helping make this a better book for everyone. Contributors as of 1ca2... |
Chaitanya Gurrapu Mathieu Dubreuilh Volker-Weissmann Changwoo Park Matt Cooper Wesley Gonçalves Christian Decker Matt Trzcinski William Gathoye Christoph Bachhuber Matthew Miner William Turrell Christoph Prokop Matthieu Moy Wlodek Bzyl Christopher Wilson Mavaddat Javid Xavier Bonaventura Coding Spider Fox Max Coplan Y.... |
Helmut K. C. Tessarek Rob Blanco root Hemant Kumar Meena Robert P. Goldman sanders@oreilly. com Hidde de Vries Robert P. J. Day sharpiro Honking Goose Robert Theis slavos1 Howard Rohan D'Souza spacewander Ignacy Roman Kosenko td2014 Igor Ronald Wampler twekberg Ilker Cat Rory uerdogan Jan Groenewald Rüdiger Herrmann ug... |
Introduction You're about to spend several hours of your life reading about Git. Let's take a minute to explain what we have in store for you. Here is a quick summary of the ten chapters and three appendices of this book. In Chapter 1, we're going to cover Version Control Systems (VCSs) and Git basics — no technical st... |
several different systems in case you do convince everyone to make the plunge. Chapter 10 delves into the murky yet beautiful depths of Git internals. Now that you know all about Git and can wield it with power and grace, you can move on to discuss how Git stores its objects, what the object model is, details of packfi... |
Getting Started This chapter will be about getting started with Git. We will begin by explaining some background on version control tools, then move on to how to get Git running on your system and finally how to get it set up to start working with. At the end of this chapter you should understand why Git is around, why... |
Figure 1. Local version control diagram One of the most popular VCS tools was a system called RCS, which is still distributed with many computers today. RCS works by keeping patch sets (that is, the differences between files) in a special format on disk; it can then re-create what any file looked like at any point in t... |
Figure 2. Centralized version control diagram This setup offers many advantages, especially over local VCSs. For example, everyone knows to a certain degree what everyone else on the project is doing. Administrators have fine-grained control over who can do what, and it's far easier to administer a CVCS than it is to d... |
Figure 3. Distributed version control diagram Furthermore, many of these systems deal pretty well with having several remote repositories they can work with, so you can collaborate with different groups of people in different ways simultaneously within the same project. This allows you to set up several types of workfl... |
A Short History of Git As with many great things in life, Git began with a bit of creative destruction and fiery controversy. The Linux kernel is an open source software project of fairly large scope. During the early years of the Linux kernel maintenance (1991-2002), changes to the software were passed around as patch... |
Figure 4. Storing data as changes to a base version of each file Git doesn't think of or store its data this way. Instead, Git thinks of its data more like a series of snapshots of a miniature filesystem. With Git, every time you commit, or save the state of your project, Git basically takes a picture of what all your ... |
For example, to browse the history of the project, Git doesn't need to go out to the server to get the history and display it for you — it simply reads it directly from your local database. This means you see the project history almost instantly. If you want to see the changes introduced between the current version of ... |
Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot. Committed means that the data is safely stored in your local database. This leads us to the three main sections of ... |
modified and was added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified. In Git Basics, you'll learn more about these states and how you can either take advantage of them or skip the staged part entirely. The Command Line There are a lot of differ... |
Installing on mac OS There are several ways to install Git on mac OS. The easiest is probably to install the Xcode Command Line Tools. On Mavericks (10. 9) or above you can do this simply by trying to run git from the Terminal the very first time. $ git--version If you don't have it installed already, it will prompt yo... |
Installing from Source Some people may instead find it useful to install Git from source, because you'll get the most recent version. The binary installers tend to be a bit behind, though as Git has matured in recent years, this has made less of a difference. If you do want to install Git from source, you need to have ... |
generally a little clearer what the latest version is on the Git Hub page, but the kernel. org page also has release signatures if you want to verify your download. Then, compile and install: $ tar-zxf git-2. 8. 0. tar. gz $ cd git-2. 8. 0 $ make configure $. /configure--prefix=/usr $ make all doc info $ sudo make inst... |
C:\Program Data\Git\config on Windows Vista and newer. This config file can only be changed by git config-f <file> as an admin. You can view all of your settings and where they are coming from using: $ git config--list--show-origin Your Identity The first thing you should do when you install Git is to set your user nam... |
another editor, or a 32-bit version, please find specific instructions for how to set up your favorite editor with Git in git config core. editor commands. You may find, if you don't setup your editor like this, you get into a really confusing state when Git attempts to launch it. An example on a Windows system may in... |
$ git config--show-origin rerere. auto Update file:/home/johndoe/. gitconfig false Getting Help If you ever need help while using Git, there are three equivalent ways to get the comprehensive manual page (manpage) help for any of the Git commands: $ git help <verb> $ git <verb>--help $ man git-<verb> For example, you c... |
--ignore-missing check if-even missing-files are ignored in dry run --sparse allow updating entries outside of the sparse-checkout cone --chmod (+|-)x override the executable bit of the listed files --pathspec-from-file <file> read pathspec from file --pathspec-file-nul with--pathspec-from-file, pathspec elem... |
Git Basics If you can read only one chapter to get going with Git, this is it. This chapter covers every basic command you need to do the vast majority of the things you'll eventually spend your time doing with Git. By the end of the chapter, you should be able to configure and initialize a repository, begin and stop t... |
more information about exactly what files are contained in the. git directory you just created. If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that sp... |
Recording Changes to the Repository At this point, you should have a bona fide Git repository on your local machine, and a checkout or working copy of all of its files in front of you. Typically, you'll want to start making changes and committing snapshots of those changes into your repository each time the project rea... |
branch on the server. For now, that branch is always master, which is the default; you won't worry about it here. Git Branching will go over branches and references in detail. Git Hub changed the default branch name from master to main in mid-2020, and other Git hosts followed suit. So you may find that the default br... |
(use "git restore--staged <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 subsequent historical snapshot. You may recall that when ... |
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: $ v... |
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 the status of the staging area and th... |
Here is another example. gitignore file: # ignore all. a files *. a # but do track lib. a, even though you're ignoring. a files above !lib. a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes. txt, but not doc/server/arc... |
Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>... " to unstage) modified: 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: C... |
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 Your ... |
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 Git Diff in an External Tool We will continue to use the git diff command in various ways throughout the rest of the book. There is another way to look at these diffs i... |
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... |
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. That's because the-a flag includes all changed files. This is convenient, but be careful; sometimes this flag will cause you to include unwanted changes. Removing Files ... |
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 o... |
$ 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 git mv is one command instead of three — it's a convenience function. More importantly, you can use any tool you like to rename ... |
One of the more helpful options is-p or--patch, which shows the difference (the patch output) introduced in each commit. You can also limit the number of log entries displayed, such as using-2 to show only the last two entries. $ git log-p-2 commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@... |
$ git log--stat commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail. com> Date: Mon Mar 17 21:52:11 2008-0700 Change version number Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Author: Scott Chacon <schacon@gee-mail. ... |
$ git log--pretty=format:"%h-%an, %ar : %s" ca82a6d-Scott Chacon, 6 years ago : Change version number 085bb3b-Scott Chacon, 6 years ago : Remove unnecessary test a11bef0-Scott Chacon, 6 years ago : Initial commit Useful specifiers for git log--pretty=format lists some of the more useful specifiers that format takes. Ta... |
* | 5a09431 Add timeout protection to grit * | e1193f8 Support for heads with slashes in them |/ * d6016bc Require time for xmlschema * 11d191e Merge branch 'defunkt' into local This type of output will become more interesting as we go through branching and merging in the next chapter. Those are only some simple output... |
You can also filter the list to commits that match some search criteria. The--author option allows you to filter on a specific author, and the--grep option lets you search for keywords in the commit messages. You can specify more than one instance of both the--author and--grep search criteria, which will limit the com... |
can run something like this: $ git log--pretty="%h-%s"--author='Junio C Hamano'--since="2008-10-01" \ --before="2008-11-01"--no-merges--t/ 5610e3b-Fix testcase failure when extended attributes are in use acd3b9e-Enhance hold_lock_file_for_{update,append}() API f563754-demonstrate breakage of detached checkout with sy... |
$ git commit--amend You end up with a single commit — the second commit replaces the results of the first. It's important to understand that when you're amending your last commit, you're not so much fixing it as replacing it entirely with a new, improved commit that pushes the old commit out of the way and puts the ne... |
renamed: README. md-> 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 command is a bit strange, but it works. The CONTRIBUTING. md file is modified but once a... |
absolutely know that you don't want those unsaved local changes. If you would like to keep the changes you've made to that file but still need to get it out of the way for now, we'll go over stashing and branching in Git Branching ; these are generally better ways to go. Remember, anything that is committed in Git can ... |
modified: CONTRIBUTING. md The CONTRIBUTING. md file is modified but once again unstaged. Unmodifying a Modified File with git restore What if you realize that you don't want to keep your changes to the CONTRIBUTING. md file? How can you easily unmodify it — revert it back to what it looked like when you last committ... |
elsewhere. Working with such a remote repository would still involve all the standard pushing, pulling and fetching operations as with any other remote. Showing Your Remotes To see which remote servers you have configured, you can run the git remote command. It lists the shortnames of each remote handle you've specifie... |
Server. Adding Remote Repositories We've mentioned and given some demonstrations of how the git clone command implicitly adds the origin remote for you. Here's how to add a new remote explicitly. To add a new remote Git repository as a shortname you can reference easily, run git remote add <shortname> <url> : $ git rem... |
cloned (or last fetched from) it. It's important to note that the git fetch command only downloads the data to your local repository — it doesn't automatically merge it with any of your work or modify what you're currently working on. You have to merge it manually into your work when you're ready. If your current branc... |
Remote branches: master tracked dev-branch tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date) It lists the URL for the remote repository as well as the tracking branch information. The command helpfully... |
$ git remote rename pb paul $ git remote origin paul It's worth mentioning that this changes all your remote-tracking branch names, too. What used to be referenced at pb/master is now at paul/master. If you want to remove a remote for some reason — you've moved the server or are no longer using a particular mirror, or ... |
v1. 8. 5. 1 v1. 8. 5. 2 v1. 8. 5. 3 v1. 8. 5. 4 v1. 8. 5. 5 Listing tag wildcards requires-l or--list option If you want just the entire list of tags, running the command git tag implicitly assumes you want a listing and provides one; the use of-l or--list in this case is optional. If, however, you're supplying a wild... |
my version 1. 4 commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail. com> Date: Mon Mar 17 21:52:11 2008-0700 Change version number That shows the tagger information, the date the commit was tagged, and the annotation message before showing the commit information. Lightweight Tags A... |
4682c3261057305bdd616e23b64b0857d832627b Add todo file 166ae0c4d3f420721acbb115cc33848dfcc2121a Create write support 9fceb02d0ae598e95dc970b74767f19372d61af8 Update rakefile 964f16d36dfccde844893cac5b347e7b3d44abbc Commit the todo 8a5cbc430f1a9c3d00faaeffd07798508422908a Update readme Now, suppose you forgot to tag the... |
To git@github. com:schacon/simplegit. git * [new tag] v1. 5-> v1. 5 If you have a lot of tags that you want to push up at once, you can also use the--tags option to the git push command. This will transfer all of your tags to the remote server that are not already there. $ git push origin--tags Counting objects: 1, do... |
$ git push origin--delete <tagname> Checking out Tags If you want to view the versions of files a tag is pointing to, you can do a git checkout of that tag, although this puts your repository in “detached HEAD” state, which has some ill side effects: $ git checkout v2. 0. 0 Note: switching to 'v2. 0. 0'. You are in 'de... |
experience simpler, easier, and more familiar: aliases. For clarity's sake, we won't be using them anywhere else in this book, but if you go on to use Git with any regularity, aliases are something you should know about. Git doesn't automatically infer your command if you type it in partially. If you don't want to type... |
start the command with a ! character. This is useful if you write your own tools that work with a Git repository. We can demonstrate by aliasing git visual to run gitk: $ git config--global alias. visual '!gitk' Summary At this point, you can do all the basic local Git operations — creating or cloning a repository, mak... |
Git Branching Nearly every VCS has some form of branching support. Branching means you diverge from the main line of development and continue to do work without messing with that main line. In many VCS tools, this is a somewhat expensive process, often requiring you to create a new copy of your source code directory, w... |
Figure 9. A commit and its tree If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it. Figure 10. Commits and their parents A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As yo... |
Figure 11. A branch and its commit history Creating a New Branch What happens when you create a new branch? Well, doing so creates a new pointer for you to move around. Let's say you want to create a new branch called testing. You do this with the git branch command: $ git branch testing This creates a new pointer to t... |
branch. Figure 13. HEAD pointing to a branch You can easily see this by running a simple git log command that shows you where the branch pointers are pointing. This option is called--decorate. $ git log--oneline--decorate f30ab (HEAD-> master, testing) Add feature #32-ability to add new formats to the central interface... |
Figure 14. HEAD points to the current branch What is the significance of that? Well, let's do another commit: $ vim test. rb $ git commit-a-m 'Make a change' Figure 15. The HEAD branch moves forward when a commit is made This is interesting, because now your testing branch has moved forward, but your master branch stil... |
If you were to run git log right now, you might wonder where the "testing" branch you just created went, as it would not appear in the output. The branch hasn't disappeared; Git just doesn't know that you're interested in that branch and it is trying to show you what it thinks you're interested in. In other words, by d... |
checkout, and commit commands. Figure 17. Divergent history You can also see this easily with the git log command. If you run git log--oneline--decorate--graph--all it will print out the history of your commits, showing where your branch pointers are and how your history has diverged. $ git log--oneline--decorate--grap... |
Creating a new branch and switching to it at the same time It's typical to create a new branch and want to switch to that new branch at the same time — this can be done in one operation with git checkout-b <newbranchname>. From Git version 2. 23 onwards you can use git switch instead of git checkout to: Switch to an ... |
You've decided that you're going to work on issue #53 in whatever issue-tracking system your company uses. To create a new branch and switch to it at the same time, you can run the git checkout command with the-b switch: $ git checkout-b iss53 Switched to a new branch "iss53" This is shorthand for: $ git branch iss53 $... |
Figure 20. The iss53 branch has moved forward with your work Now you get the call that there is an issue with the website, and you need to fix it immediately. With Git, you don't have to deploy your fix along with the iss53 changes you've made, and you don't have to put a lot of effort into reverting those changes befo... |
Figure 21. Hotfix branch based on master You can run your tests, make sure the hotfix is what you want, and finally merge the hotfix branch back into your master branch to deploy to production. You do this with the git merge command: $ git checkout master $ git merge hotfix Updating f42c576..3a0874c Fast-forward index... |
Figure 22. master is fast-forwarded to hotfix After your super-important fix is deployed, you're ready to switch back to the work you were doing before you were interrupted. However, first you'll delete the hotfix branch, because you no longer need it — the master branch points at the same place. You can delete it with... |
Figure 23. Work continues on iss53 It's worth noting here that the work you did in your hotfix branch is not contained in the files in your iss53 branch. If you need to pull it in, you can merge your master branch into your iss53 branch by running git merge master, or you can wait to integrate those changes until you d... |
Figure 24. Three snapshots used in a typical merge Instead of just moving the branch pointer forward, Git creates a new snapshot that results from this three-way merge and automatically creates a new commit that points to it. This is referred to as a merge commit, and is special in that it has more than one parent. Fig... |
$ git merge iss53 Auto-merging index. html CONFLICT (content): Merge conflict in index. html Automatic merge failed; fix conflicts and then commit the result. Git hasn't automatically created a new merge commit. It has paused the process while you resolve the conflict. If you want to see which files are unmerged at any... |
on each file to mark it as resolved. Staging the file marks it as resolved in Git. If you want to use a graphical tool to resolve these issues, you can run git mergetool, which fires up an appropriate visual merge tool and walks you through the conflicts: $ git mergetool This message is displayed because 'merge. tool' ... |
index. html # # It looks like you may be committing a merge. # If this is not correct, please remove the file # . git/MERGE_HEAD # and try again. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # All conflicts fi... |
$ git branch--merged iss53 * master Because you already merged in iss53 earlier, you see it in your list. Branches on this list without the * in front of them are generally fine to delete with git branch-d ; you've already incorporated their work into another branch, so you're not going to lose anything. To see all t... |
branch-name, while keeping all history. You also want to change the branch name on the remote (Git Hub, Git Lab, other server). How do you do this? Rename the branch locally with the git branch--move command: $ git branch--move bad-branch-name corrected-branch-name This replaces your bad-branch-name with corrected-bran... |
branch available on the remote. $ git push--set-upstream origin main Now we end up with the following state: $ git branch--all * main remotes/origin/HEAD-> origin/master remotes/origin/main remotes/origin/master Your local master branch is gone, as it's replaced with the main branch. The main branch is present on... |
Many Git developers have a workflow that embraces this approach, such as having only code that is entirely stable in their master branch — possibly only code that has been or will be released. They have another parallel branch named develop or next that they work from or use to test stability — it isn't necessarily alw... |
branches. But in Git it's common to create, work on, merge, and delete branches several times a day. You saw this in the last section with the iss53 and hotfix branches you created. You did a few commits on them and deleted them directly after merging them into your main branch. This technique allows you to context-swi... |
Figure 29. History after merging dumbidea and iss91v2 We will go into more detail about the various possible workflows for your Git project in Distributed Git, so before you decide which branching scheme your next project will use, be sure to read that chapter. It's important to remember when you're doing all this that... |
Remote-tracking branches are references to the state of remote branches. They're local references that you can't move; Git moves them for you whenever you do any network communication, to make sure they accurately represent the state of the remote repository. Think of them as bookmarks, to remind you where the branches... |
Figure 30. Server and local repositories after cloning If you do some work on your local master branch, and, in the meantime, someone else pushes to git. ourcompany. com and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your or... |
Figure 31. Local and remote work can diverge To synchronize your work with a given remote, you run a git fetch <remote> command (in our case, git fetch origin ). This command looks up which server “origin” is (in this case, it's git. ourcompany. com ), fetches any data from it that you don't yet have, and updates your ... |
Figure 32. git fetch updates your remote-tracking branches To demonstrate having multiple remote servers and what remote branches for those remote projects look like, let's assume you have another internal Git server that is used only for development by one of your sprint teams. This server is at git. team1. ourcompany... |
Figure 33. Adding another server as a remote Now, you can run git fetch teamone to fetch everything the remote teamone server has that you don't have yet. Because that server has a subset of the data your origin server has right now, Git fetches no data but sets a remote-tracking branch called teamone/master to point t... |
Figure 34. Remote-tracking branch for teamone/master Pushing When you want to share a branch with the world, you need to push it up to a remote to which you have write access. Your local branches aren't automatically synchronized to the remotes you write to — you have to explicitly push the branches you want to share. ... |
Internals, but you can generally leave it off. You can also do git push origin serverfix:serverfix, which does the same thing — it says, “Take my serverfix and make it the remote's serverfix. ” You can use this format to push a local branch into a remote branch that is named differently. If you didn't want it to be cal... |
“tracking branch” (and the branch it tracks is called an “upstream branch”). Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git pull, Git automatically knows which server to fetch from and which branch to merge in. When you clone a reposi... |
If you want to see what tracking branches you have set up, you can use the-vv option to git branch. This will list out your local branches with more information including what each branch is tracking and if your local branch is ahead, behind or both. $ git branch-vv iss53 7e424c3 [origin/iss53: ahead 2] Add forgotten... |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 5