Skip to content

Commit 7b43e67

Browse files
authored
Merge pull request #1200 from ben/fetch_push_suggestions
Fetch and push on different repositories
2 parents 0c0b109 + 9a48cda commit 7b43e67

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

book/06-github/sections/2-contributing.asc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Here's how it generally works:
4343
5. Open a Pull Request on GitHub.
4444
6. Discuss, and optionally continue committing.
4545
7. The project owner merges or closes the Pull Request.
46+
8. Sync the updated master back to your fork.
4647

4748
This is basically the Integration Manager workflow covered in <<ch05-distributed-git#_integration_manager>>, but instead of using email to communicate and review changes, teams use GitHub's web based tools.
4849

@@ -417,3 +418,63 @@ This isn't technically GitHub Flavored Markdown, but it is incredibly useful. In
417418
image::images/markdown-08-drag-drop.png[Drag and drop images]
418419

419420
If you look at <<_md_drag>>, you can see a small ``Parsed as Markdown'' hint above the text area. Clicking on that will give you a full cheat sheet of everything you can do with Markdown on GitHub.
421+
422+
[[_fetch_and_push_on_different_repositories]]
423+
==== Keep your GitHub public repository up-to-date
424+
425+
Once you've forked a GitHub repository, your repository (your "fork") exists independently from the original.
426+
In particular, when the original repository has new commits, GitHub informs you by a message like:
427+
428+
[source,text]
429+
----
430+
This branch is 5 commits behind progit:master.
431+
----
432+
433+
But your GitHub repository will never be automatically updated by GitHub; this is something that you must do yourself.
434+
Fortunately, this is very easy to do.
435+
436+
One possibility to do this requires no configuration.
437+
For example, if you forked from `https://github.com/progit/progit2.git`, you can keep your `master` branch up-to-date like this:
438+
439+
[source,console]
440+
----
441+
$ git checkout master <1>
442+
$ git pull https://github.com/progit/progit2.git <2>
443+
$ git push origin master <3>
444+
----
445+
446+
<1> If you were on another branch, return to `master`.
447+
<2> Fetch changes from `https://github.com/progit/progit2.git` and merge them into `master`.
448+
<3> Push your `master` branch to `origin`.
449+
450+
This works, but it is a little tedious having to spell out the fetch URL every time.
451+
You can automate this work with a bit of configuration:
452+
453+
[source,console]
454+
----
455+
$ git remote add progit https://github.com/progit/progit2.git <1>
456+
$ git branch --set-upstream-to=progit/master master <2>
457+
$ git config --local remote.pushDefault origin <3>
458+
----
459+
460+
<1> Add the source repository and give it a name.
461+
Here, I have chosen to call it `progit`.
462+
<2> Set your `master` branch to fetch from the `progit` remote.
463+
<3> Define the default push repository to `origin`.
464+
465+
Once this is done, the workflow becomes much simpler:
466+
467+
[source,console]
468+
----
469+
$ git checkout master <1>
470+
$ git pull <2>
471+
$ git push <3>
472+
----
473+
474+
<1> If you were on another branch, return to `master`.
475+
<2> Fetch changes from `progit` and merge changes into `master`.
476+
<3> Push your `master` branch to `origin`.
477+
478+
This approach can be useful, but it's not without downsides.
479+
Git will happily do this work for you silently, but it won't warn you if you make a commit to `master`, pull from `progit`, then push to `origin` -- all of those operations are valid with this setup.
480+
So you'll have to take care never to commit directly to `master`, since that branch effectively belongs to the upstream repository.

book/10-git-internals/sections/refspec.asc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ If they want Git to do that automatically each time they run `git push origin`,
120120

121121
Again, this will cause a `git push origin` to push the local `master` branch to the remote `qa/master` branch by default.
122122

123+
[NOTE]
124+
====
125+
You cannot use the refspec to fetch from one repository and push to another one.
126+
For an example to do so, refer to <<ch06-github#_fetch_and_push_on_different_repositories>>.
127+
====
128+
123129
==== Deleting References
124130

125131
You can also use the refspec to delete references from the remote server by running something like this:

0 commit comments

Comments
 (0)