text
stringlengths 16
1.76k
| label
int64 0
5
|
---|---|
To prevent the display of merge commits
cluttering up your log history, simply add the log option --no-merges. Undoing Things At any stage, you may want to undo something. Here, we’ll review a few basic tools for undoing
changes that you’ve made. Be careful, because you can’t always undo some of these undos. This is
one of the few areas in Git where you may lose some work if you do it wrong. One of the common undos takes place when you commit too early and possibly forget to add some
files, or you mess up your commit message. If you want to redo that commit, make the additional
changes you forgot, stage them, and commit again using the --amend option:
$ git commit --amend This command takes your staging area and uses it for the commit. If you’ve made no changes since
your last commit (for instance, you run this command immediately after your previous commit),
then your snapshot will look exactly the same, and all you’ll change is your commit message. | 2 |
This command takes your staging area and uses it for the commit. If you’ve made no changes since
your last commit (for instance, you run this command immediately after your previous commit),
then your snapshot will look exactly the same, and all you’ll change is your commit message. The same commit-message editor fires up, but it already contains the message of your previous
commit. You can edit the message the same as always, but it overwrites your previous commit. As an example, if you commit and then realize you forgot to stage the changes in a file you wanted
to add to this commit, you can do something like this:
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend
46 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 new commit in its place. | 2 |
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 new commit in its place. Effectively, it’s as if the previous commit never happened, and it won’t show up in
your repository history. The obvious value to amending commits is to make minor improvements to your
last commit, without cluttering your repository history with commit messages of
the form, “Oops, forgot to add a file” or “Darn, fixing a typo in last commit”. Only amend commits that are still local and have not been pushed somewhere. Amending previously pushed commits and force pushing the branch will cause
problems for your collaborators. For more on what happens when you do this and
how to recover if you’re on the receiving end read The Perils of Rebasing. Unstaging a Staged File | 2 |
Only amend commits that are still local and have not been pushed somewhere. Amending previously pushed commits and force pushing the branch will cause
problems for your collaborators. For more on what happens when you do this and
how to recover if you’re on the receiving end read The Perils of Rebasing. Unstaging a Staged File The next two sections demonstrate how to work with your staging area and working directory
changes. The nice part is that the command you use to determine the state of those two areas also
reminds you how to undo changes to them. For example, let’s say you’ve changed two files and
want to commit them as two separate changes, but you accidentally type git add * and stage them
both. How can you unstage one of the two? The git status command reminds you:
$ git add *
$ git status On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md | 2 |
How can you unstage one of the two? The git status command reminds you:
$ git add *
$ git status On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md Right below the “Changes to be committed” text, it says use git reset HEAD <file>… to unstage. So,
let’s use that advice to unstage the CONTRIBUTING.md file:
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
47
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. | 2 |
The command is a bit strange, but it works. The CONTRIBUTING.md file is modified but once again
unstaged.
It’s true that git reset can be a dangerous command, especially if you provide the
--hard flag. However, in the scenario described above, the file in your working
directory is not touched, so it’s relatively safe. For now this magic invocation is all you need to know about the git reset command. We’ll go into
much more detail about what reset does and how to master it to do really interesting things in
Reset Demystified. Unmodifying a Modified File 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 committed (or initially
cloned, or however you got it into your working directory)? Luckily, git status tells you how to do
that, too. | 2 |
How can
you easily unmodify it — revert it back to what it looked like when you last committed (or initially
cloned, or however you got it into your working directory)? Luckily, git status tells you how to do
that, too. In the last example output, the unstaged area looks like this:
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 It tells you pretty explicitly how to discard the changes you’ve made. Let’s do what it says:
$ git checkout -- CONTRIBUTING.md
$ git status On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README You can see that the changes have been reverted.
It’s important to understand that git checkout -- <file> is a dangerous command. | 2 |
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README You can see that the changes have been reverted.
It’s important to understand that git checkout -- <file> is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with
the last staged or committed version. Don’t ever use this command unless you
absolutely know that you don’t want those unsaved local changes. 48
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 almost always be recovered. Even commits that
were on branches that were deleted or commits that were overwritten with an --amend commit can
be recovered (see Data Recovery for data recovery). However, anything you lose that was never
committed is likely never to be seen again. | 2 |
Remember, anything that is committed in Git can almost always be recovered. Even commits that
were on branches that were deleted or commits that were overwritten with an --amend commit can
be recovered (see Data Recovery for data recovery). However, anything you lose that was never
committed is likely never to be seen again. Undoing things with git restore Git version 2.23.0 introduced a new command: git restore. It’s basically an alternative to git reset
which we just covered. From Git version 2.23.0 onwards, Git will use git restore instead of git
reset for many undo operations. Let’s retrace our steps, and undo things with git restore instead of git reset. Unstaging a Staged File with git restore The next two sections demonstrate how to work with your staging area and working directory
changes with git restore. The nice part is that the command you use to determine the state of those
two areas also reminds you how to undo changes to them. | 2 |
Unstaging a Staged File with git restore The next two sections demonstrate how to work with your staging area and working directory
changes with git restore. The nice part is that the command you use to determine the state of those
two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed
two files and want to commit them as two separate changes, but you accidentally type git add *
and stage them both. How can you unstage one of the two? The git status command reminds you:
$ git add *
$ git status On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: CONTRIBUTING.md
renamed: README.md -> README Right below the “Changes to be committed” text, it says use git restore --staged <file>… to
unstage. So, let’s use that advice to unstage the CONTRIBUTING.md file:
$ git restore --staged CONTRIBUTING.md
$ git status | 2 |
Right below the “Changes to be committed” text, it says use git restore --staged <file>… to
unstage. So, let’s use that advice to unstage the CONTRIBUTING.md file:
$ git restore --staged CONTRIBUTING.md
$ git status On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README 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: CONTRIBUTING.md
49
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 committed (or initially
cloned, or however you got it into your working directory)? | 2 |
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 committed (or initially
cloned, or however you got it into your working directory)? Luckily, git status tells you how to do
that, too. In the last example output, the unstaged area looks like this:
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: CONTRIBUTING.md It tells you pretty explicitly how to discard the changes you’ve made. Let’s do what it says:
$ git restore CONTRIBUTING.md
$ git status On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
It’s important to understand that git restore <file> is a dangerous command. | 2 |
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: README.md -> README
It’s important to understand that git restore <file> is a dangerous command. Any
local changes you made to that file are gone — Git just replaced that file with the
last staged or committed version. Don’t ever use this command unless you
absolutely know that you don’t want those unsaved local changes. Working with Remotes To be able to collaborate on any Git project, you need to know how to manage your remote
repositories. Remote repositories are versions of your project that are hosted on the Internet or
network somewhere. You can have several of them, each of which generally is either read-only or
read/write for you. Collaborating with others involves managing these remote repositories and
pushing and pulling data to and from them when you need to share work. | 2 |
Remote repositories are versions of your project that are hosted on the Internet or
network somewhere. You can have several of them, each of which generally is either read-only or
read/write for you. Collaborating with others involves managing these remote repositories and
pushing and pulling data to and from them when you need to share work. Managing remote
repositories includes knowing how to add remote repositories, remove remotes that are no longer
valid, manage various remote branches and define them as being tracked or not, and more. In this
section, we’ll cover some of these remote-management skills. Remote repositories can be on your local machine.
It is entirely possible that you can be working with a “remote” repository that is, in
fact, on the same host you are. The word “remote” does not necessarily imply that
the repository is somewhere else on the network or Internet, only that it is
elsewhere. Working with such a remote repository would still involve all the
standard pushing, pulling and fetching operations as with any other remote. 50
Showing Your Remotes | 2 |
The word “remote” does not necessarily imply that
the repository is somewhere else on the network or Internet, only that it is
elsewhere. Working with such a remote repository would still involve all the
standard pushing, pulling and fetching operations as with any other remote. 50
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 specified. If you’ve cloned your repository, you should at
least see origin — that is the default name Git gives to the server you cloned from:
$ git clone https://github.com/schacon/ticgit Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done. remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done. Resolving deltas: 100% (772/772), done. Checking connectivity... done. | 2 |
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done. Resolving deltas: 100% (772/772), done. Checking connectivity... done. $ cd ticgit
$ git remote
origin You can also specify -v, which shows you the URLs that Git has stored for the shortname to be used
when reading and writing to that remote:
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push) If you have more than one remote, the command lists them all. For example, a repository with
multiple remotes for working with several collaborators might look something like this. | 2 |
If you have more than one remote, the command lists them all. For example, a repository with
multiple remotes for working with several collaborators might look something like this. $ cd grit
$ git remote -v
bakkdoor https://github.com/bakkdoor/grit (fetch)
bakkdoor https://github.com/bakkdoor/grit (push)
cho45 https://github.com/cho45/grit (fetch)
cho45 https://github.com/cho45/grit (push)
defunkt https://github.com/defunkt/grit (fetch)
defunkt https://github.com/defunkt/grit (push)
koke git://github.com/koke/grit.git (fetch)
koke git://github.com/koke/grit.git (push)
origin [email protected]:mojombo/grit.git (fetch)
origin [email protected]:mojombo/grit.git (push) | 2 |
This means we can pull contributions from any of these users pretty easily. We may additionally
have permission to push to one or more of these, though we can’t tell that here. Notice that these remotes use a variety of protocols; we’ll cover more about this in Getting Git on a
Server. 51
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 remote
origin
$ git remote add pb https://github.com/paulboone/ticgit | 2 |
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 remote
origin
$ git remote add pb https://github.com/paulboone/ticgit $ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push) Now you can use the string pb on the command line in lieu of the whole URL. For example, if you
want to fetch all the information that Paul has but that you don’t yet have in your repository, you
can run git fetch pb:
$ git fetch pb
remote: Counting objects: 43, done. | 2 |
Now you can use the string pb on the command line in lieu of the whole URL. For example, if you
want to fetch all the information that Paul has but that you don’t yet have in your repository, you
can run git fetch pb:
$ git fetch pb
remote: Counting objects: 43, done. remote: Compressing objects: 100% (36/36), done. remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done. From https://github.com/paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit Paul’s master branch is now accessible locally as pb/master — you can merge it into one of your
branches, or you can check out a local branch at that point if you want to inspect it. We’ll go over
what branches are and how to use them in much more detail in Git Branching. Fetching and Pulling from Your Remotes | 2 |
We’ll go over
what branches are and how to use them in much more detail in Git Branching. Fetching and Pulling from Your Remotes As you just saw, to get data from your remote projects, you can run:
$ git fetch <remote> The command goes out to that remote project and pulls down all the data from that remote project
that you don’t have yet. After you do this, you should have references to all the branches from that
remote, which you can merge in or inspect at any time. If you clone a repository, the command automatically adds that remote repository under the name
“origin”. So, git fetch origin fetches any new work that has been pushed to that server since you
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
52
you’re ready. | 2 |
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
52
you’re ready. If your current branch is set up to track a remote branch (see the next section and Git Branching for
more information), you can use the git pull command to automatically fetch and then merge that
remote branch into your current branch. This may be an easier or more comfortable workflow for
you; and by default, the git clone command automatically sets up your local master branch to track
the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and
automatically tries to merge it into the code you’re currently working on. From git version 2.27 onward, git pull will give a warning if the pull. rebase
variable is not set. Git will keep warning you until you set the variable.
| 2 |
From git version 2.27 onward, git pull will give a warning if the pull. rebase
variable is not set. Git will keep warning you until you set the variable.
If you want the default behavior of git (fast-forward if possible, else create a merge
commit): git config --global pull. rebase "false" If you want to rebase when pulling: git config --global pull. rebase "true"
Pushing to Your Remotes When you have your project at a point that you want to share, you have to push it upstream. The
command for this is simple: git push <remote> <branch>. If you want to push your master branch to
your origin server (again, cloning generally sets up both of those names for you automatically),
then you can run this to push any commits you’ve done back up to the server:
$ git push origin master This command works only if you cloned from a server to which you have write access and if
nobody has pushed in the meantime. | 2 |
This command works only if you cloned from a server to which you have write access and if
nobody has pushed in the meantime. If you and someone else clone at the same time and they push
upstream and then you push upstream, your push will rightly be rejected. You’ll have to fetch their
work first and incorporate it into yours before you’ll be allowed to push. See Git Branching for
more detailed information on how to push to remote servers. Inspecting a Remote If you want to see more information about a particular remote, you can use the git remote show
<remote> command. If you run this command with a particular shortname, such as origin, you get
something like this:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit Push URL: https://github.com/schacon/ticgit | 2 |
If you run this command with a particular shortname, such as origin, you get
something like this:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit Push URL: https://github.com/schacon/ticgit HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
Local branch configured for 'git pull':
53
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 tells you that if you’re on the master branch and you run git pull, it will automatically
merge the remote’s master branch into the local one after it has been fetched. It also lists all the
remote references it has pulled down. That is a simple example you’re likely to encounter. | 2 |
The command
helpfully tells you that if you’re on the master branch and you run git pull, it will automatically
merge the remote’s master branch into the local one after it has been fetched. It also lists all the
remote references it has pulled down. That is a simple example you’re likely to encounter. When you’re using Git more heavily, however,
you may see much more information from git remote show:
$ git remote show origin
* remote origin
URL: https://github.com/my-org/complex-project Fetch URL: https://github.com/my-org/complex-project Push URL: https://github.com/my-org/complex-project | 2 |
Fetch URL: https://github.com/my-org/complex-project Push URL: https://github.com/my-org/complex-project HEAD branch: master
Remote branches:
master tracked
dev-branch tracked
markdown-strip tracked
issue-43 new (next fetch will store in remotes/origin)
issue-45 new (next fetch will store in remotes/origin)
refs/remotes/origin/issue-11 stale (use 'git remote prune' to remove)
Local branches configured for 'git pull':
dev-branch merges with remote dev-branch
master merges with remote master
Local refs configured for 'git push':
dev-branch pushes to dev-branch (up to
date)
markdown-strip pushes to markdown-strip (up to
date)
master pushes to master (up to
date) This command shows which branch is automatically pushed to when you run git push while on
certain branches. | 2 |
This command shows which branch is automatically pushed to when you run git push while on
certain branches. It also shows you which remote branches on the server you don’t yet have, which
remote branches you have that have been removed from the server, and multiple local branches
that are able to merge automatically with their remote-tracking branch when you run git pull. Renaming and Removing Remotes You can run git remote rename to change a remote’s shortname. For instance, if you want to rename
pb to paul, you can do so with git remote rename:
$ git remote rename pb paul
$ git remote
54
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 perhaps a contributor isn’t contributing anymore — you can either use git
remote remove or git remote rm:
$ git remote remove paul
$ git remote
origin | 2 |
If you want to remove a remote for some reason — you’ve moved the server or are no longer using
a particular mirror, or perhaps a contributor isn’t contributing anymore — you can either use git
remote remove or git remote rm:
$ git remote remove paul
$ git remote
origin Once you delete the reference to a remote this way, all remote-tracking branches and configuration
settings associated with that remote are also deleted. Tagging
Like most VCSs, Git has the ability to tag specific points in a repository’s history as being important. Typically, people use this functionality to mark release points (v1.0, v2.0 and so on). In this section,
you’ll learn how to list existing tags, how to create and delete tags, and what the different types of
tags are. Listing Your Tags
Listing the existing tags in Git is straightforward. Just type git tag (with optional -l or --list):
$ git tag
v1.0
v2.0 This command lists the tags in alphabetical order; the order in which they are displayed has no real
importance. | 2 |
Listing Your Tags
Listing the existing tags in Git is straightforward. Just type git tag (with optional -l or --list):
$ git tag
v1.0
v2.0 This command lists the tags in alphabetical order; the order in which they are displayed has no real
importance. You can also search for tags that match a particular pattern. The Git source repo, for instance,
contains more than 500 tags. If you’re interested only in looking at the 1.8.5 series, you can run this:
$ git tag -l "v1.8.5*"
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
v1.8.5-rc2
v1.8.5-rc3
v1.8.5.1
v1.8.5.2
55
v1.8.5.3
v1.8.5.4
v1.8.5.5 Listing tag wildcards requires -l or --list option | 2 |
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 wildcard pattern to match tag names, the use of -l
or --list is mandatory. Creating Tags Git supports two types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific
commit. Annotated tags, however, are stored as full objects in the Git database. They’re checksummed;
contain the tagger name, email, and date; have a tagging message; and can be signed and verified
with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you
can have all this information; but if you want a temporary tag or for some reason don’t want to
keep the other information, lightweight tags are available too. | 2 |
It’s generally recommended that you create annotated tags so you
can have all this information; but if you want a temporary tag or for some reason don’t want to
keep the other information, lightweight tags are available too. Annotated Tags
Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the tag
command:
$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4 The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for
an annotated tag, Git launches your editor so you can type it in. You can see the tag data along with the commit that was tagged by using the git show command:
$ git show v1.4
tag v1.4
Tagger: Ben Straub <[email protected]> | 2 |
You can see the tag data along with the commit that was tagged by using the git show command:
$ git show v1.4
tag v1.4
Tagger: Ben Straub <[email protected]> Date: Sat May 3 20:19:12 2014 -0700
my version 1.4
56
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]> 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
Another way to tag commits is with a lightweight tag. This is basically the commit checksum stored
in a file — no other information is kept. | 2 |
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
Another way to tag commits is with a lightweight tag. This is basically the commit checksum stored
in a file — no other information is kept. To create a lightweight tag, don’t supply any of the -a, -s, or
-m options, just provide a tag name:
$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5 This time, if you run git show on the tag, you don’t see the extra tag information. The command just
shows the commit:
$ git show v1.4-lw
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]> Date: Mon Mar 17 21:52:11 2008 -0700 Change version number
Tagging Later | 2 |
Date: Mon Mar 17 21:52:11 2008 -0700 Change version number
Tagging Later You can also tag commits after you’ve moved past them. Suppose your commit history looks like
this:
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 Create write support
0d52aaab4479697da7686c15f77a3d64d9165190 One more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment' 0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc | 2 |
One more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment' 0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc Add commit function
4682c3261057305bdd616e23b64b0857d832627b Add todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a Create write support
57
9fceb02d0ae598e95dc970b74767f19372d61af8 Update rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc Commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a Update readme | 2 |
Update readme Now, suppose you forgot to tag the project at v1.2, which was at the “Update rakefile” commit. You
can add it after the fact. To tag that commit, you specify the commit checksum (or part of it) at the
end of the command:
$ git tag -a v1.2 9fceb02 You can see that you’ve tagged the commit:
$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5
$ git show v1.2
tag v1.2
Tagger: Scott Chacon <[email protected]> Date: Mon Feb 9 15:32:16 2009 -0800
version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <[email protected]> Date: Sun Apr 27 20:43:35 2008 -0700 Update rakefile
...
Sharing Tags | 2 |
Date: Sun Apr 27 20:43:35 2008 -0700 Update rakefile
...
Sharing Tags By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly
push tags to a shared server after you have created them. This process is just like sharing remote
branches — you can run git push origin <tagname>. $ git push origin v1.5
Counting objects: 14, done. Delta compression using up to 8 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done. Total 14 (delta 3), reused 0 (delta 0)
To [email protected]:schacon/simplegit.git
58
* [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. | 2 |
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, done. Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done. Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:schacon/simplegit.git
* [new tag] v1.4 -> v1.4
* [new tag] v1.4-lw -> v1.4-lw Now, when someone else clones or pulls from your repository, they will get all your tags as well. git push pushes both types of tags
git push <remote> --tags will push both lightweight and annotated tags. There is
currently no option to push only lightweight tags, but if you use git push <remote>
--follow-tags only annotated tags will be pushed to the remote. | 2 |
git push pushes both types of tags
git push <remote> --tags will push both lightweight and annotated tags. There is
currently no option to push only lightweight tags, but if you use git push <remote>
--follow-tags only annotated tags will be pushed to the remote. Deleting Tags To delete a tag on your local repository, you can use git tag -d <tagname>. For example, we could
remove our lightweight tag above as follows:
$ git tag -d v1.4-lw
Deleted tag 'v1.4-lw' (was e7d5add) Note that this does not remove the tag from any remote servers. There are two common variations
for deleting a tag from a remote server. The first variation is git push <remote> :refs/tags/<tagname>:
$ git push origin :refs/tags/v1.4-lw
To /[email protected]:schacon/simplegit.git
- [deleted] v1.4-lw | 2 |
The first variation is git push <remote> :refs/tags/<tagname>:
$ git push origin :refs/tags/v1.4-lw
To /[email protected]:schacon/simplegit.git
- [deleted] v1.4-lw The way to interpret the above is to read it as the null value before the colon is being pushed to the
remote tag name, effectively deleting it. The second (and more intuitive) way to delete a remote tag is with:
$ git push origin --delete <tagname> 59
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 'detached HEAD' state. | 2 |
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name> Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice. detachedHead to false
HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final
$ git checkout v2.0-beta-0.1 Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-
final
HEAD is now at df3f601... Add atlas.json and cover image | 2 |
Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-
final
HEAD is now at df3f601... Add atlas.json and cover image In “detached HEAD” state, if you make changes and then create a commit, the tag will stay the same,
but your new commit won’t belong to any branch and will be unreachable, except by the exact
commit hash. Thus, if you need to make changes — say you’re fixing a bug on an older version, for
instance — you will generally want to create a branch:
$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2' If you do this and make a commit, your version2 branch will be slightly different than your v2.0.0
tag since it will move forward with your new changes, so do be careful. Git Aliases Before we move on to the next chapter, we want to introduce a feature that can make your Git
experience simpler, easier, and more familiar: aliases. | 2 |
Git Aliases Before we move on to the next chapter, we want to introduce a feature that can make your Git
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. 60
Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type
the entire text of each of the Git commands, you can easily set up an alias for each command using
git config. Here are a couple of examples you may want to set up:
$ git config --global alias. co checkout
$ git config --global alias. br branch
$ git config --global alias.ci commit
$ git config --global alias.st status This means that, for example, instead of typing git commit, you just need to type git ci. | 2 |
co checkout
$ git config --global alias. br branch
$ git config --global alias.ci commit
$ git config --global alias.st status This means that, for example, instead of typing git commit, you just need to type git ci. As you go
on using Git, you’ll probably use other commands frequently as well; don’t hesitate to create new
aliases. This technique can also be very useful in creating commands that you think should exist. For
example, to correct the usability problem you encountered with unstaging a file, you can add your
own unstage alias to Git:
$ git config --global alias.unstage 'reset HEAD --' This makes the following two commands equivalent:
$ git unstage fileA
$ git reset HEAD -- fileA This seems a bit clearer. It’s also common to add a last command, like this:
$ git config --global alias. last 'log -1 HEAD' | 2 |
This makes the following two commands equivalent:
$ git unstage fileA
$ git reset HEAD -- fileA This seems a bit clearer. It’s also common to add a last command, like this:
$ git config --global alias. last 'log -1 HEAD' This way, you can see the last commit easily:
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel <[email protected]> Date: Tue Aug 26 19:48:51 2008 +0800 Test for current head
Signed-off-by: Scott Chacon <[email protected]> As you can tell, Git simply replaces the new command with whatever you alias it for. However,
maybe you want to run an external command, rather than a Git subcommand. In that case, you
start the command with a ! character. This is useful if you write your own tools that work with a
Git repository. | 2 |
However,
maybe you want to run an external command, rather than a Git subcommand. In that case, you
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:
61
$ git config --global alias.visual '!gitk'
Summary At this point, you can do all the basic local Git operations — creating or cloning a repository, making
changes, staging and committing those changes, and viewing the history of all the changes the
repository has been through. Next, we’ll cover Git’s killer feature: its branching model. 62
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, which can take a long time for large projects. | 2 |
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, which can take a long time for large projects. Some people refer to Git’s branching model as its “killer feature,” and it certainly sets Git apart in
the VCS community. Why is it so special? The way Git branches is incredibly lightweight, making
branching operations nearly instantaneous, and switching back and forth between branches
generally just as fast. Unlike many other VCSs, Git encourages workflows that branch and merge
often, even multiple times in a day. Understanding and mastering this feature gives you a powerful
and unique tool and can entirely change the way that you develop. Branches in a Nutshell To really understand the way Git does branching, we need to take a step back and examine how Git
stores its data. | 2 |
Understanding and mastering this feature gives you a powerful
and unique tool and can entirely change the way that you develop. Branches in a Nutshell To really understand the way Git does branching, we need to take a step back and examine how Git
stores its data. As you may remember from What is Git?, Git doesn’t store data as a series of changesets or
differences, but instead as a series of snapshots. When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the
content you staged. This object also contains the author’s name and email address, the message that
you typed, and pointers to the commit or commits that directly came before this commit (its parent
or parents): zero parents for the initial commit, one parent for a normal commit, and multiple
parents for a commit that results from a merge of two or more branches. To visualize this, let’s assume that you have a directory containing three files, and you stage them
all and commit. | 2 |
To visualize this, let’s assume that you have a directory containing three files, and you stage them
all and commit. Staging the files computes a checksum for each one (the SHA-1 hash we mentioned
in What is Git?), stores that version of the file in the Git repository (Git refers to them as blobs), and
adds that checksum to the staging area:
$ git add README test.rb LICENSE
$ git commit -m 'Initial commit' When you create the commit by running git commit, Git checksums each subdirectory (in this case,
just the root project directory) and stores them as a tree object in the Git repository. Git then creates
a commit object that has the metadata and a pointer to the root project tree so it can re-create that
snapshot when needed. Your Git repository now contains five objects: three blobs (each representing the contents of one of
the three files), one tree that lists the contents of the directory and specifies which file names are
stored as which blobs, and one commit with the pointer to that root tree and all the commit
metadata. | 2 |
Your Git repository now contains five objects: three blobs (each representing the contents of one of
the three files), one tree that lists the contents of the directory and specifies which file names are
stored as which blobs, and one commit with the pointer to that root tree and all the commit
metadata. 63
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 you start making commits, you’re given a master branch that points to the
last commit you made. Every time you commit, the master branch pointer moves forward
automatically. The “master” branch in Git is not a special branch. It is exactly like any other
branch. The only reason nearly every repository has one is that the git init
command creates it by default and most people don’t bother to change it. 64
Figure 11. | 2 |
The “master” branch in Git is not a special branch. It is exactly like any other
branch. The only reason nearly every repository has one is that the git init
command creates it by default and most people don’t bother to change it. 64
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 the same commit you’re currently on. Figure 12. Two branches pointing into the same series of commits
How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. Note
that this is a lot different than the concept of HEAD in other VCSs you may be used to, such as
Subversion or CVS. In Git, this is a pointer to the local branch you’re currently on. | 2 |
It keeps a special pointer called HEAD. Note
that this is a lot different than the concept of HEAD in other VCSs you may be used to, such as
Subversion or CVS. In Git, this is a pointer to the local branch you’re currently on. In this case,
you’re still on master. The git branch command only created a new branch — it didn’t switch to that
65
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
34ac2 Fix bug #1328 - stack overflow under certain conditions
98ca9 Initial commit You can see the master and testing branches that are right there next to the f30ab commit. Switching Branches To switch to an existing branch, you run the git checkout command. | 2 |
You can see the master and testing branches that are right there next to the f30ab commit. Switching Branches To switch to an existing branch, you run the git checkout command. Let’s switch to the new testing
branch:
$ git checkout testing
This moves HEAD to point to the testing branch. 66
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 'made 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
still points to the commit you were on when you ran git checkout to switch branches. Let’s switch
back to the master branch:
$ git checkout master
git log doesn’t show all the branches all the time
67
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; | 2 |
Let’s switch
back to the master branch:
$ git checkout master
git log doesn’t show all the branches all the time
67
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 default, git log will only show commit history below the branch you’ve
checked out. To show commit history for the desired branch you have to explicitly specify it: git
log testing. To show all of the branches, add --all to your git log command. Figure 16. HEAD moves when you checkout
That command did two things. It moved the HEAD pointer back to point to the master branch, and it
reverted the files in your working directory back to the snapshot that master points to. This also
means the changes you make from this point forward will diverge from an older version of the
project. | 2 |
HEAD moves when you checkout
That command did two things. It moved the HEAD pointer back to point to the master branch, and it
reverted the files in your working directory back to the snapshot that master points to. This also
means the changes you make from this point forward will diverge from an older version of the
project. It essentially rewinds the work you’ve done in your testing branch so you can go in a
different direction. Switching branches changes files in your working directory
It’s important to note that when you switch branches in Git, files in your working
directory will change. If you switch to an older branch, your working directory
will be reverted to look like it did the last time you committed on that branch. If Git
cannot do it cleanly, it will not let you switch at all. Let’s make a few changes and commit again:
$ vim test.rb
$ git commit -a -m 'made other changes' Now your project history has diverged (see Divergent history). | 2 |
If Git
cannot do it cleanly, it will not let you switch at all. Let’s make a few changes and commit again:
$ vim test.rb
$ git commit -a -m 'made other changes' Now your project history has diverged (see Divergent history). You created and switched to a
branch, did some work on it, and then switched back to your main branch and did other work. Both
of those changes are isolated in separate branches: you can switch back and forth between the
branches and merge them together when you’re ready. And you did all that with simple branch,
68
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 --graph --all
* c2b9e (HEAD, master) | 2 |
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 --graph --all
* c2b9e (HEAD, master) Made other changes
| * 87ab2 (testing) Made a change
|/
* f30ab Add feature #32 - ability to add new formats to the central interface
* 34ac2 Fix bug #1328 - stack overflow under certain conditions
* 98ca9 initial commit of my project
Because a branch in Git is actually a simple file that contains the 40 character SHA-1 checksum of
the commit it points to, branches are cheap to create and destroy. Creating a new branch is as quick
and simple as writing 41 bytes to a file (40 characters and a newline). This is in sharp contrast to the way most older VCS tools branch, which involves copying all of the
project’s files into a second directory. | 2 |
Creating a new branch is as quick
and simple as writing 41 bytes to a file (40 characters and a newline). This is in sharp contrast to the way most older VCS tools branch, which involves copying all of the
project’s files into a second directory. This can take several seconds or even minutes, depending on
the size of the project, whereas in Git the process is always instantaneous. Also, because we’re
recording the parents when we commit, finding a proper merge base for merging is automatically
done for us and is generally very easy to do. These features help encourage developers to create
and use branches often. Let’s see why you should do so. 69
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
in one operation with git checkout -b
same time — this can be done
<newbranchname>. From Git version 2.23 onwards you can use git switch instead of git checkout to:
• Switch to an existing branch: git switch testing-branch. | 2 |
From Git version 2.23 onwards you can use git switch instead of git checkout to:
• Switch to an existing branch: git switch testing-branch. • Create a new branch and switch to it: git switch -c new-branch. The -c flag
stands for create, you can also use the full flag: --create. • Return to your previously checked out branch: git switch -.
Basic Branching and Merging
Let’s go through a simple example of branching and merging with a workflow that you might use in
the real world. You’ll follow these steps:
1. Do some work on a website. 2. Create a branch for a new user story you’re working on. 3. Do some work in that branch. At this stage, you’ll receive a call that another issue is critical and you need a hotfix. You’ll do the
following:
1. Switch to your production branch. 2. Create a branch to add the hotfix. 3. After it’s tested, merge the hotfix branch, and push to production. | 2 |
You’ll do the
following:
1. Switch to your production branch. 2. Create a branch to add the hotfix. 3. After it’s tested, merge the hotfix branch, and push to production. 4. Switch back to your original user story and continue working. Basic Branching First, let’s say you’re working on your project and have a couple of commits already on the master
branch. Figure 18. A simple commit history
70
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
$ git checkout iss53
Figure 19. Creating a new branch pointer You work on your website and do some commits. | 2 |
This is shorthand for:
$ git branch iss53
$ git checkout iss53
Figure 19. Creating a new branch pointer You work on your website and do some commits. Doing so moves the iss53 branch forward,
because you have it checked out (that is, your HEAD is pointing to it):
$ vim index.html
$ git commit -a -m 'Create new footer [issue 53]'
71
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 before you can work on applying your fix to
what is in production. All you have to do is switch back to your master branch. However, before you do that, note that if your working directory or staging area has uncommitted
changes that conflict with the branch you’re checking out, Git won’t let you switch branches. | 2 |
All you have to do is switch back to your master branch. However, before you do that, note that if your working directory or staging area has uncommitted
changes that conflict with the branch you’re checking out, Git won’t let you switch branches. It’s
best to have a clean working state when you switch branches. There are ways to get around this
(namely, stashing and commit amending) that we’ll cover later on, in Stashing and Cleaning. For
now, let’s assume you’ve committed all your changes, so you can switch back to your master branch:
$ git checkout master
Switched to branch 'master' At this point, your project working directory is exactly the way it was before you started working
on issue #53, and you can concentrate on your hotfix. This is an important point to remember:
when you switch branches, Git resets your working directory to look like it did the last time you
committed on that branch. It adds, removes, and modifies files automatically to make sure your
working copy is what the branch looked like on your last commit to it. | 2 |
This is an important point to remember:
when you switch branches, Git resets your working directory to look like it did the last time you
committed on that branch. It adds, removes, and modifies files automatically to make sure your
working copy is what the branch looked like on your last commit to it. Next, you have a hotfix to make. Let’s create a hotfix branch on which to work until it’s completed:
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'Fix broken email address'
[hotfix 1fb7853] Fix broken email address
1 file changed, 2 insertions(+) 72
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. | 2 |
Fix broken email address
1 file changed, 2 insertions(+) 72
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.html | 2 ++
1 file changed, 2 insertions(+) You’ll notice the phrase “fast-forward” in that merge. Because the commit C4 pointed to by the
branch hotfix you merged in was directly ahead of the commit C2 you’re on, Git simply moves the
pointer forward. To phrase that another way, when you try to merge one commit with a commit
that can be reached by following the first commit’s history, Git simplifies things by moving the
pointer forward because there is no divergent work to merge together — this is called a “fast-
forward.” | 2 |
To phrase that another way, when you try to merge one commit with a commit
that can be reached by following the first commit’s history, Git simplifies things by moving the
pointer forward because there is no divergent work to merge together — this is called a “fast-
forward.” Your change is now in the snapshot of the commit pointed to by the master branch, and you can
deploy the fix. 73
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 the -d option to git
branch:
$ git branch -d hotfix
Deleted branch hotfix (3a0874c). Now you can switch back to your work-in-progress branch on issue #53 and continue working on it. | 2 |
You can delete it with the -d option to git
branch:
$ git branch -d hotfix
Deleted branch hotfix (3a0874c). Now you can switch back to your work-in-progress branch on issue #53 and continue working on it. $ git checkout iss53
Switched to branch "iss53"
$ vim index.html
$ git commit -a -m 'Finish the new footer [issue 53]'
[iss53 ad82d7a] Finish the new footer [issue 53]
1 file changed, 1 insertion(+) 74
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 decide to
pull the iss53 branch back into master later. Basic Merging | 2 |
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 decide to
pull the iss53 branch back into master later. Basic Merging Suppose you’ve decided that your issue #53 work is complete and ready to be merged into your
master branch. In order to do that, you’ll merge your iss53 branch into master, much like you
merged your hotfix branch earlier. All you have to do is check out the branch you wish to merge
into and then run the git merge command:
$ git checkout master
Switched to branch 'master'
$ git merge iss53
Merge made by the 'recursive' strategy. index.html | 1 +
1 file changed, 1 insertion(+) This looks a bit different than the hotfix merge you did earlier. In this case, your development
history has diverged from some older point. Because the commit on the branch you’re on isn’t a
direct ancestor of the branch you’re merging in, Git has to do some work. | 2 |
This looks a bit different than the hotfix merge you did earlier. In this case, your development
history has diverged from some older point. Because the commit on the branch you’re on isn’t a
direct ancestor of the branch you’re merging in, Git has to do some work. In this case, Git does a
simple three-way merge, using the two snapshots pointed to by the branch tips and the common
ancestor of the two. 75
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. Figure 25. A merge commit
Now that your work is merged in, you have no further need for the iss53 branch. You can close the
issue in your issue-tracking system, and delete the branch:
$ git branch -d iss53
Basic Merge Conflicts Occasionally, this process doesn’t go smoothly. | 2 |
Figure 25. A merge commit
Now that your work is merged in, you have no further need for the iss53 branch. You can close the
issue in your issue-tracking system, and delete the branch:
$ git branch -d iss53
Basic Merge Conflicts Occasionally, this process doesn’t go smoothly. If you changed the same part of the same file
differently in the two branches you’re merging, Git won’t be able to merge them cleanly. If your fix
for issue #53 modified the same part of a file as the hotfix branch, you’ll get a merge conflict that
looks something like this:
76
$ 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 point after a merge conflict, you can
run git status:
$ git status On branch master You have unmerged paths. | 2 |
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 point after a merge conflict, you can
run git status:
$ git status On branch master You have unmerged paths. (fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a") Anything that has merge conflicts and hasn’t been resolved is listed as unmerged. Git adds standard
conflict-resolution markers to the files that have conflicts, so you can open them manually and
resolve those conflicts. Your file contains a section that looks something like this: | 2 |
Anything that has merge conflicts and hasn’t been resolved is listed as unmerged. Git adds standard
conflict-resolution markers to the files that have conflicts, so you can open them manually and
resolve those conflicts. Your file contains a section that looks something like this: <<<<<<< HEAD:index.html
<div id="footer">contact : [email protected]</div>
=======
<div id="footer">
please contact us at [email protected]
</div>
>>>>>>> iss53:index.html This means the version in HEAD (your master branch, because that was what you had checked out
when you ran your merge command) is the top part of that block (everything above the =======),
while the version in your iss53 branch looks like everything in the bottom part. In order to resolve
the conflict, you have to either choose one side or the other or merge the contents yourself. | 2 |
In order to resolve
the conflict, you have to either choose one side or the other or merge the contents yourself. For
instance, you might resolve this conflict by replacing the entire block with this:
<div id="footer">
please contact us at [email protected]
</div> This resolution has a little of each section, and the <<<<<<<, =======, and >>>>>>> lines have been
completely removed. After you’ve resolved each of these sections in each conflicted file, run git add
77
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' is not configured. See 'git mergetool --tool-help' or 'git help config' for more details. | 2 |
See 'git mergetool --tool-help' or 'git help config' for more details. 'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge
p4merge araxis bc3 codecompare vimdiff emerge
Merging:
index.html
Normal merge conflict for 'index.html':
{local}: modified file
{remote}: modified file Hit return to start merge resolution tool (opendiff): If you want to use a merge tool other than the default (Git chose opendiff in this case because the
command was run on a Mac), you can see all the supported tools listed at the top after “one of the
following tools.” Just type the name of the tool you’d rather use.
If you need more advanced tools for resolving tricky merge conflicts, we cover
more on merging in Advanced Merging. After you exit the merge tool, Git asks you if the merge was successful. | 2 |
Just type the name of the tool you’d rather use.
If you need more advanced tools for resolving tricky merge conflicts, we cover
more on merging in Advanced Merging. After you exit the merge tool, Git asks you if the merge was successful. If you tell the script that it
was, it stages the file to mark it as resolved for you. You can run git status again to verify that all
conflicts have been resolved:
$ git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed:
modified: index.html If you’re happy with that, and you verify that everything that had conflicts has been staged, you can
type git commit to finalize the merge commit. The commit message by default looks something like
this:
Merge branch 'iss53' Conflicts:
78
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.
# | 2 |
The commit message by default looks something like
this:
Merge branch 'iss53' Conflicts:
78
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 fixed but you are still merging. #
# Changes to be committed:
# modified: index.html
# If you think it would be helpful to others looking at this merge in the future, you can modify this
commit message with details about how you resolved the merge and explain why you did the
changes you made if these are not obvious. Branch Management Now that you’ve created, merged, and deleted some branches, let’s look at some branch-
management tools that will come in handy when you begin using branches all the time. The git branch command does more than just create and delete branches. | 2 |
Branch Management Now that you’ve created, merged, and deleted some branches, let’s look at some branch-
management tools that will come in handy when you begin using branches all the time. The git branch command does more than just create and delete branches. If you run it with no
arguments, you get a simple listing of your current branches:
$ git branch
iss53
* master
testing
Notice the * character that prefixes the master branch: it indicates the branch that you currently
have checked out (i.e., the branch that HEAD points to). This means that if you commit at this point,
the master branch will be moved forward with your new work. To see the last commit on each
branch, you can run git branch -v:
$ git branch -v
iss53 93b412c Fix javascript issue
* master 7a98805 Merge branch 'iss53'
testing 782fd34 Add scott to the author list in the readme | 2 |
To see the last commit on each
branch, you can run git branch -v:
$ git branch -v
iss53 93b412c Fix javascript issue
* master 7a98805 Merge branch 'iss53'
testing 782fd34 Add scott to the author list in the readme The useful --merged and --no-merged options can filter this list to branches that you have or have not
yet merged into the branch you’re currently on. To see which branches are already merged into the
branch you’re on, you can run git branch --merged:
79
$ 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 the branches that contain work you haven’t yet merged in, you can run git branch --no
-merged:
$ git branch --no-merged
testing
This shows your other branch. | 2 |
To see all the branches that contain work you haven’t yet merged in, you can run git branch --no
-merged:
$ git branch --no-merged
testing
This shows your other branch. Because it contains work that isn’t merged in yet, trying to delete it
with git branch -d will fail:
$ git branch -d testing
error: The branch 'testing' is not fully merged. If you are sure you want to delete it, run 'git branch -D testing'. If you really do want to delete the branch and lose that work, you can force it with -D, as the helpful
message points out. The options described above, --merged and --no-merged will, if not given a commit
or branch name as an argument, show you what is, respectively, merged or not
merged into your current branch. You can always provide an additional argument to ask about the merge state with
respect to some other branch without checking that other branch out first, as in,
what is not merged into the master branch? | 2 |
You can always provide an additional argument to ask about the merge state with
respect to some other branch without checking that other branch out first, as in,
what is not merged into the master branch?
$ git checkout testing
$ git branch --no-merged master
topicA
featureB
Changing a branch name
Do not rename branches that are still in use by other collaborators. Do not rename
a branch like master/main/mainline without having read the section "Changing the
master branch name". Suppose you have a branch that is called bad-branch-name and you want to change it to corrected-
80
branch-name, while keeping all history. You also want to change the branch name on the remote
(GitHub, GitLab, 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-branch-name, but this change is only local for
now. | 2 |
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-branch-name, but this change is only local for
now. To let others see the corrected branch on the remote, push it:
$ git push --set-upstream origin corrected-branch-name
Now we’ll take a brief look at where we are now:
$ git branch --all
* corrected-branch-name
main
remotes/origin/bad-branch-name
remotes/origin/corrected-branch-name
remotes/origin/main
Notice that you’re on the branch corrected-branch-name and it’s available on the remote. However,
the branch with the bad name is also still present there but you can delete it by executing the
following command:
$ git push origin --delete bad-branch-name
Now the bad branch name is fully replaced with the corrected branch name. | 2 |
However,
the branch with the bad name is also still present there but you can delete it by executing the
following command:
$ git push origin --delete bad-branch-name
Now the bad branch name is fully replaced with the corrected branch name. Changing the master branch name
Changing the name of a branch like master/main/mainline/default will break the
integrations, services, helper utilities and build/release scripts that your repository
uses. Before you do this, make sure you consult with your collaborators. Also,
make sure you do a thorough search through your repo and update any references
to the old branch name in your code and scripts. Rename your local master branch into main with the following command:
$ git branch --move master main There’s no local master branch anymore, because it’s renamed to the main branch. To let others see the new main branch, you need to push it to the remote. This makes the renamed
81
branch available on the remote. $ git push --set-upstream origin main | 2 |
There’s no local master branch anymore, because it’s renamed to the main branch. To let others see the new main branch, you need to push it to the remote. This makes the renamed
81
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
the remote. However, the old master branch is still present on the remote. Other collaborators will
continue to use the master branch as the base of their work, until you make some further changes. Now you have a few more tasks in front of you to complete the transition:
• Any projects that depend on this one will need to update their code and/or configuration. • Update any test-runner configuration files. • Adjust build and release scripts. • | 2 |
Now you have a few more tasks in front of you to complete the transition:
• Any projects that depend on this one will need to update their code and/or configuration. • Update any test-runner configuration files. • Adjust build and release scripts. • Redirect settings on your repo host for things like the repo’s default branch, merge rules, and
other things that match branch names. • Update references to the old branch in documentation. • Close or merge any pull requests that target the old branch. After you’ve done all these tasks, and are certain the main branch performs just as the master
branch, you can delete the master branch:
$ git push origin --delete master
Branching Workflows Now that you have the basics of branching and merging down, what can or should you do with
them? In this section, we’ll cover some common workflows that this lightweight branching makes
possible, so you can decide if you would like to incorporate them into your own development cycle. Long-Running Branches
Because | 2 |
Now that you have the basics of branching and merging down, what can or should you do with
them? In this section, we’ll cover some common workflows that this lightweight branching makes
possible, so you can decide if you would like to incorporate them into your own development cycle. Long-Running Branches
Because Git uses a simple three-way merge, merging from one branch into another multiple times
over a long period is generally easy to do. This means you can have several branches that are
always open and that you use for different stages of your development cycle; you can merge
regularly from some of them into others. 82
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 always stable, but whenever it gets to a stable state, it can be merged into master. | 2 |
They
have another parallel branch named develop or next that they work from or use to test stability — it
isn’t necessarily always stable, but whenever it gets to a stable state, it can be merged into master. It’s used to pull in topic branches (short-lived branches, like your earlier iss53 branch) when
they’re ready, to make sure they pass all the tests and don’t introduce bugs. In reality, we’re talking about pointers moving up the line of commits you’re making. The stable
branches are farther down the line in your commit history, and the bleeding-edge branches are
farther up the history. Figure 26. A linear view of progressive-stability branching It’s generally easier to think about them as work silos, where sets of commits graduate to a more
stable silo when they’re fully tested. Figure 27. A “silo” view of progressive-stability branching You can keep doing this for several levels of stability. Some larger projects also have a proposed or pu
(proposed updates) branch that has integrated branches that may not be ready to go into the next or
master branch. | 2 |
Figure 27. A “silo” view of progressive-stability branching You can keep doing this for several levels of stability. Some larger projects also have a proposed or pu
(proposed updates) branch that has integrated branches that may not be ready to go into the next or
master branch. The idea is that your branches are at various levels of stability; when they reach a
more stable level, they’re merged into the branch above them. Again, having multiple long-running
branches isn’t necessary, but it’s often helpful, especially when you’re dealing with very large or
complex projects. Topic Branches
Topic branches, however, are useful in projects of any size. A topic branch is a short-lived branch
that you create and use for a single particular feature or related work. This is something you’ve
likely never done with a VCS before because it’s generally too expensive to create and merge
83
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. | 2 |
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-switch quickly and completely — because your work is separated
into silos where all the changes in that branch have to do with that topic, it’s easier to see what has
happened during code review and such. You can keep the changes there for minutes, days, or
months, and merge them in when they’re ready, regardless of the order in which they were created
or worked on. Consider an example of doing some work (on master), branching off for an issue (iss91), working on
it for a bit, branching off the second branch to try another way of handling the same thing (
iss91v2), going back to your master branch and working there for a while, and then branching off
there to do some work that you’re not sure is a good idea (dumbidea branch). | 2 |
Your commit history
will look something like this:
Figure 28. Multiple topic branches Now, let’s say you decide you like the second solution to your issue best (iss91v2); and you showed
the dumbidea branch to your coworkers, and it turns out to be genius. You can throw away the
original iss91 branch (losing commits C5 and C6) and merge in the other two. Your history then
looks like this:
84
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 these branches are completely local. When you’re branching and merging, everything is being done only in your Git repository — there
is no communication with the server. Remote Branches
Remote references are references (pointers) in your remote repositories, including branches, tags,
and so on. | 2 |
When you’re branching and merging, everything is being done only in your Git repository — there
is no communication with the server. Remote Branches
Remote references are references (pointers) in your remote repositories, including branches, tags,
and so on. You can get a full list of remote references explicitly with git ls-remote <remote>, or git
remote show <remote> for remote branches as well as more information. Nevertheless, a more
common way is to take advantage of remote-tracking branches. 85
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 in your remote repositories were the last time you
connected to them. Remote-tracking branch names take the form <remote>/<branch>. | 2 |
Think of them as
bookmarks, to remind you where the branches in your remote repositories were the last time you
connected to them. Remote-tracking branch names take the form <remote>/<branch>. For instance, if you wanted to see
what the master branch on your origin remote looked like as of the last time you communicated
with it, you would check the origin/master branch. If you were working on an issue with a partner
and they pushed up an iss53 branch, you might have your own local iss53 branch, but the branch
on the server would be represented by the remote-tracking branch origin/iss53. This may be a bit confusing, so let’s look at an example. Let’s say you have a Git server on your
network at git.ourcompany.com. If you clone from this, Git’s clone command automatically names it
origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it
origin/master locally. | 2 |
Let’s say you have a Git server on your
network at git.ourcompany.com. If you clone from this, Git’s clone command automatically names it
origin for you, pulls down all its data, creates a pointer to where its master branch is, and names it
origin/master locally. Git also gives you your own local master branch starting at the same place as
origin’s master branch, so you have something to work from. “origin” is not special
Just like the branch name “master” does not have any special meaning in Git,
neither does “origin”. While “master” is the default name for a starting branch
when you run git init which is the only reason it’s widely used, “origin” is the
default name for a remote when you run git clone. If you run git clone -o booyah
instead, then you will have booyah/master as your default remote branch. 86
Figure 30. Server and local repositories after cloning | 2 |
If you run git clone -o booyah
instead, then you will have booyah/master as your default remote branch. 86
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 origin/master pointer doesn’t
move. 87
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 local
database, moving your origin/master pointer to its new, more up-to-date position. 88
Figure 32. git fetch updates your remote-tracking branches | 2 |
88
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.com. You can add it
as a new remote reference to the project you’re currently working on by running the git remote
add command as we covered in Git Basics. Name this remote teamone, which will be your shortname
for that whole URL. 89
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 to the commit that
teamone has as its master branch. 90
Figure 34. Remote-tracking branch for teamone/master
Pushing | 2 |
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 to the commit that
teamone has as its master branch. 90
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. That way, you can use private
branches for work you don’t want to share, and push up only the topic branches you want to
collaborate on. If you have a branch named serverfix that you want to work on with others, you can push it up the
same way you pushed your first branch. Run git push <remote> <branch>:
$ git push origin serverfix
Counting objects: 24, done. Delta compression using up to 8 threads. Compressing objects: 100% (15/15), done. | 2 |
Run git push <remote> <branch>:
$ git push origin serverfix
Counting objects: 24, done. Delta compression using up to 8 threads. Compressing objects: 100% (15/15), done. Writing objects: 100% (24/24), 1.91 KiB | 0 bytes/s, done. Total 24 (delta 2), reused 0 (delta 0)
To https://github.com/schacon/simplegit
* [new branch] serverfix -> serverfix This is a bit of a shortcut. Git automatically expands the serverfix branchname out to
refs/heads/serverfix:refs/heads/serverfix, which means, “Take my serverfix local branch and
push it to update the remote’s serverfix branch.” We’ll go over the refs/heads/ part in detail in Git
91
Internals, but you can generally leave it off. | 2 |
We’ll go over the refs/heads/ part in detail in Git
91
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 called serverfix on the remote, you could instead run git push origin
serverfix:awesomebranch to push your local serverfix branch to the awesomebranch branch on the
remote project. Don’t type your password every time If you’re using an HTTPS URL to push over, the Git server will ask you for your
username and password for authentication. By default it will prompt you on the
terminal for this information so the server can tell if you’re allowed to push.
If you don’t want to type it every single time you push, you can set up a “credential
cache”. | 2 |
By default it will prompt you on the
terminal for this information so the server can tell if you’re allowed to push.
If you don’t want to type it every single time you push, you can set up a “credential
cache”. The simplest is just to keep it in memory for a few minutes, which you can
easily set up by running git config --global credential. helper cache. For more information on the various credential caching options available, see
Credential Storage. The next time one of your collaborators fetches from the server, they will get a reference to where
the server’s version of serverfix is under the remote branch origin/serverfix:
$ git fetch origin
remote: Counting objects: 7, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done. | 2 |
remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done. From https://github.com/schacon/simplegit
* [new branch] serverfix -> origin/serverfix It’s important to note that when you do a fetch that brings down new remote-tracking branches,
you don’t automatically have local, editable copies of them. In other words, in this case, you don’t
have a new serverfix branch — you have only an origin/serverfix pointer that you can’t modify. To merge this work into your current working branch, you can run git merge origin/serverfix. If
you want your own serverfix branch that you can work on, you can base it off your remote-
tracking branch:
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin. Switched to a new branch 'serverfix' | 2 |
If
you want your own serverfix branch that you can work on, you can base it off your remote-
tracking branch:
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin. Switched to a new branch 'serverfix' This gives you a local branch that you can work on that starts where origin/serverfix is. Tracking Branches Checking out a local branch from a remote-tracking branch automatically creates what is called a
92
“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 repository, it generally automatically creates a master branch that tracks
origin/master. However, you can set up other tracking branches if you wish — ones that track
branches on other remotes, or don’t track the master branch. | 2 |