doc: add tutorial on branches for beginners
Subsystems (2)
PATCH 1/1 doc: add tutorial on branches for beginners
From: Julia Evans <[email protected]>
Because Git's branches are so flexible and the meaning of a branch is only what people assign to it, it's important to talk about some of the social context about branches. Do this in two ways:
1. Introduce some imaginary characters who are learning branches for the first time and examples of workflows they might use. Test readers said this was helpful because it helped them see different ways they might use branches.
2. Explain the importance of a "main" or "master" branch in many projects, and give a few examples of how projects might use it.
On git log:
- Use the output of git log --graph to visualize branches instead of a more abstract ASCII diagram, since it serves a double purpose of teaching people how to read git log's output.
But also mention that folks may prefer to use a GUI to visualize branches.
- Include two sections to explain the output of git log, since several test readers found it very hard to understand. In that explanation, only explain HEAD fairly briefly and instead keep the focus on the idea of a "current branch", since that's the term Git's UI normally uses.
On the conceptual introduction at the beginning:
- One of the biggest issues with explaining branches is that there are 3 ways in Git to think of branches and it's not straightforward to explain how to reconcile them:
1. just the commits that "branch off"
2. the latest commit on the branch
3. the full history since the beginning of time
Handle this by starting with the "tree" metaphor where branches "branch off" a central trunk (way 1), since this makes sense with how people actually want to use branches. Point out some issues with the tree metaphor and talk about how it relates to the way Git represents branches (way 2).
- Avoid using computer science jargon, because it's a major barrier to folks without a CS background. It's not useful or realistic to teach most beginners the term "directed acyclic graph" just so they can learn branches. Mention the term "directed acyclic graph" briefly in a note for folks who are already familiar with that term and link folks to the data model if they'd like more information.
- Include some basic facts about commits. Folks seemed to find this helpful, one person said they didn't realize it would be so hard to change their email address later.
To keep it readable:
- Don't assume that the reader has read gittutorial, and instead refer back to it explicitly if it's relevant.
- Always include the output for every command. Test readers said this was helpful.
Other choices about material that is/isn't included:
- git-prompt.sh (yes): Mention how important it is to be aware what branch you're on, and suggest using git-prompt.sh. Link to git-prompt.sh on GitHub since several test readers couldn't find it on their machine. The test readers liked this.
- fast-forward merges (yes): Explain the difference between a fast-forward merge and a merge commit by giving examples of what each one looks like.
- upstreams/rebase (no): Don't explain what an upstream is or introduce rebase, since we're already introducing a lot of concepts. Instead include a "Where to learn more" section with some pointers to where to continue learning.
- GUIs (yes): Some readers wanted a suggestion for what GUI to use. It's very hard to suggest an appropriate GUI because there are so many and so many of them are not open source, but give a couple of suggestions of open source tools that seem to be popular with the community. Also mention that many non-open-source IDEs have built-in Git integrations for folks who are already using those tools who might not be aware, since they're a popular way to use Git.
---
doc: add tutorial on branches for beginners
Some self-review comments:
* The section on GUIs is tough to write because a lot of the popular GUIs are proprietary to some extent. Might remove it if that seems like the best way.
* When the new gittutorial.adoc lands I think it would be good to add a little table of contents to the top of it to match this one's TOC.
Thanks to Mario (bavarianbidi), Johnny, Ken Wells, Yatin Lala, Valentin Bachem, Ozgur Dincer, Ken Wells, matsch, and all the other test readers for giving feedback on this. We got 70 comments from 16 people.
* Here's the overall feedback from the test readers so far (from "Any other feedback?")
* As git with all the different concepts, and everything builds on each other is often for new learners hard to follow, I really liked the scope of the tutorial. Just focusing on branches and everything else might be relevant later.
* This is so good!
* Thanks for doing this, it's greatly appreciated.
* It went very fluent. I was surprised at how quickly I was progressing in the text. How the important thing is that I'm going to use what I've learned in practice. A tutorial cannot help directly with that; It can only motivate. Maybe it would be nice to ass a sentence or two on that.
* Thank you for what you do! 🫶
* This is the best tutorial for git branches. It was easy to follow and I learned so much. I really appreciated the way that the steps flowed into each other and built off of previous steps, and all the example output matched what I saw on my machine. Up until now I've just been blindly following other people's instructions on how to manage branches, but I finally feel like I get it now. Especially with the different examples at the end; I can see myself in almost all of them and it has given me some ideas on how to manage my small-time projects. Like for modifying other people's repositories, I don't have to keep making new directories, I can just make a new branch and push that branch to my repo! It was such a huge confidence boost to be able to look at the different use cases and be like "I can do that one here, and that one there!" I don't know what could be done to make it better. I might end up printing this out and putting it on the wall for when I'm working on stuff later.
* I am German. Most of the tutorial was understandable and clear.
* Not for this one, but I'd love a tutorial on worktrees.
* Very concise, I learned something new.
* Really enjoying and appreciating these tutorials, thanks very much
* The whole tutorial is quite readable and helpful. :)
* I'm confused of using git switch. I always did what described in the tutorial using git checkout...
* Thank you for this and the previous tutorial! I've long been interested in using git but found the complete documentation a bit overwhelming. You're breaking it down into easily digestible chunks and I really appreciate it!
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2307%2Fjvns%2Ftutorial-branches-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2307/jvns/tutorial-branches-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2307
Documentation/Makefile | 1 + Documentation/gittutorial-branches.adoc | 940 ++++++++++++++++++++++++ Documentation/meson.build | 1 + command-list.txt | 1 + 4 files changed, 943 insertions(+) create mode 100644 Documentation/gittutorial-branches.adoc
Documentation/Makefile +1 -0
@@ -65,6 +65,7 @@ MAN7_TXT += gitremote-helpers.adoc MAN7_TXT += gitrevisions.adoc MAN7_TXT += gitsubmodules.adoc MAN7_TXT += gittutorial-2.adoc +MAN7_TXT += gittutorial-branches.adoc MAN7_TXT += gittutorial.adoc MAN7_TXT += gitworkflows.adoc
Documentation/gittutorial-branches.adoc new file +940 -0
940 lines not on this page · raw message
Documentation/meson.build +1 -0
@@ -208,6 +208,7 @@ manpages = { 'gitrevisions.adoc' : 7, 'gitsubmodules.adoc' : 7, 'gittutorial-2.adoc' : 7, + 'gittutorial-branches.adoc' : 7, 'gittutorial.adoc' : 7, 'gitworkflows.adoc' : 7, }
command-list.txt +1 -0
@@ -245,6 +245,7 @@ gitrepository-layout userinterfaces gitrevisions userinterfaces gitsubmodules guide gittutorial guide +gittutorial-branches guide gitweb ancillaryinterrogators gitworkflows guide scalar mainporcelain
base-commit: 3c60ef1e38ee80337025a1b908327f7879386329
Signature · 1 line
gitgitgadget
Quoted 5 lines · Julia Evans via GitGitGadget
From: Julia Evans <[email protected]>
Because Git's branches are so flexible and the meaning of a branch is only what people assign to it, it's important to talk about some of the social context about branches. Do this in two ways:
A meta-comment, not limited to this patch.
We are currently reviewer-bandwidth-limited. Having many topics in-flight under review would not help these topics go any faster with greater parallelism. In fact, it slows everything down by wearing available reviewers down due to forced context switching.
I wonder what incentives, carrots and sticks we have on those who have in-flight topics that are so close to the finish line to concentrate on completing these topics before sending new ones.
Ideas?
Quoted 5 lines
I wonder what incentives, carrots and sticks we have on those who have in-flight topics that are so close to the finish line to concentrate on completing these topics before sending new ones.
Ideas?
For sure, I can avoid submitting new topics in the future until the current ones are finished. I don't have any other changes in progress in any case.
I'm very conscious that reviewer bandwidth is limited and I'm very interested in ideas for how to make the docs review process easier.
- Julia
Quoted 4 lines · Julia Evans
Quoted 4 lines
I wonder what incentives, carrots and sticks we have on those who have in-flight topics that are so close to the finish line to concentrate on completing these topics before sending new ones.
Quoted 8 lines
Quoted 1 line
Ideas?
For sure, I can avoid submitting new topics in the future until the current ones are finished. I don't have any other changes in progress in any case.
I'm very conscious that reviewer bandwidth is limited and I'm very interested in ideas for how to make the docs review process easier.
My interest in making review process better is not limited to documentation patches, though.
Thanks.
Quoted 12 lines · Junio C Hamano
Quoted 10 lines · Julia Evans
Quoted 5 lines
I wonder what incentives, carrots and sticks we have on those who have in-flight topics that are so close to the finish line to concentrate on completing these topics before sending new ones.
Ideas?
For sure, I can avoid submitting new topics in the future until the current ones are finished. I don't have any other changes in progress in any case.
Quoted 5 lines
Quoted 2 lines
I'm very conscious that reviewer bandwidth is limited and I'm very interested in ideas for how to make the docs review process easier.
My interest in making review process better is not limited to documentation patches, though.
When I applied for funding from the Sovereign Tech Agency to work on the Git project's documentation, the main risk I flagged in my application was that reviewer capacity in the project is very limited. I tried to deal with this in a several ways:
1. I emailed a few people who had reviewed my patches in the past to ask if they would be have capacity to do review on the new patches. Patrick and Ben agreed to do some reviews which I really appreciate.
2. I applied for a relatively small amount of funding, which limits how many hours Marie and I actually spent writing. Fewer hours spent writing means less work to review :)
3. We recruited dozens of test readers to participate in a kind of side review process. That doesn't serve the same function as the mailing list review of course, but it hugely improves the final quality of the document without introducing extra pressure on Git project's reviewers.
I don't know which of these strategies would apply to code changes but those are some of things I've tried so far.
This tutorial is a bit of a long read, so here are some of my own notes on the branch after a week away from it. Also let me know if I can do anything else to help review this one! My current funding ends Nov. 30, though I'll see this one through without funding if needed.
1. It was a bit tougher to get test readers for this one even though so many people said they were interested in learning about branches and collaboration, and I'm not sure why. Almost everyone who completed it said that it took less than an hour. Maybe a table of contents would help folks understand the overall structure to see what they're getting into?
2. This document is doing a pretty tricky dance where we're introducing some example Git workflows for collaboration without explaining remotes or upstreams, because the tutorial is already quite long, but ultimately the reason many people use Git is to collaborate and it feels silly to not talk about collaboration at all. I believe we gave at least a basic explanation of everything that is actually used in the example Git workflows but this will definitely leave some people with questions about how remotes work. We don't have a great place to point people to learn more about remotes. In the future it might make sense to write another guide on remotes and upstreams (maybe git help remotes?).
3. One of the test readers commented that they were confused by git switch because they're familiar with git checkout. At first I didn't get it (if someone already knows about git checkout, why do they even need to read this tutorial?), but actually some of the test readers had been using Git for quite a long time (even 10+ years) but were still interested in a basic tutorial on branches because they still weren't clear on the basics of how branches work. So it might be better to also mention git checkout.
4. Only using git log --graph for the branch diagrams is tough and a lot of people hard a hard time understanding the ASCII art diagrams. I didn't see a better option because other ASCII diagrams are not really that much better IMO (you still have to explain them, and they don't have the benefit of being a tool you can actually use to explore your repo). And we don't have prior art for using SVGs in the HTML docs.
- Julia
Quoted 7 lines · Julia Evans
1. It was a bit tougher to get test readers for this one even though so many people said they were interested in learning about branches and collaboration, and I'm not sure why. Almost everyone who completed it said that it took less than an hour. Maybe a table of contents would help folks understand the overall structure to see what they're getting into?
It certainly might! I also wondered how many of your test readers were genuinely new to the ideas here, which can make it harder to get readers.
Quoted 10 lines
2. This document is doing a pretty tricky dance where we're introducing some example Git workflows for collaboration without explaining remotes or upstreams, because the tutorial is already quite long, but ultimately the reason many people use Git is to collaborate and it feels silly to not talk about collaboration at all. I believe we gave at least a basic explanation of everything that is actually used in the example Git workflows but this will definitely leave some people with questions about how remotes work. We don't have a great place to point people to learn more about remotes. In the future it might make sense to write another guide on remotes and upstreams (maybe
git help remotes?).
Yep, agreed. I've mulled on this a bit and will keep doing so---I have some paternity leave coming up, so perhaps I'll find some time to write in earnest. Mind doing some early feedback when I get there?
Quoted 4 lines
3. One of the test readers commented that they were confused by
git switchbecause they're familiar withgit checkout.
So it might be better to also
mentiongit checkout.
I waffle on this. It's probably easy to mention "switch is the modern form, but you may see older guides teaching checkout" or something? Any more than a sentence feels like another distraction, though, and I'm also perfectly content to *not* mention checkout at all (and leave that to folks to discover if they choose to).
Quoted 2 lines
4. Only using
git log --graphfor the branch diagrams is tough and a lot of people hard a hard time understanding the ASCII art diagrams.
Yeah, this is fair. "git show-branch" is probably harder to learn, though presents similar information when it comes to "what's going on with branches." I only mention it as a way to have *more* branch diagrams :P
Anyway, I think my overall feeling is we want folks to learn how to read log output (graph or not)---it's an important skill to working with Git IMO. (What if there's no graphical environment available?) I like how your approach sort of sneaks in the vegetables, too, and future guides can build on that so folks get more exposure to and build more confidence with the output.
Signature · 1 line
D. Ben Knoble
Hi Julia, sorry for the delay! I've read the current email thread, so I'm replying with a bit of "foreknowledge" ;)
Quoted 11 lines · Julia Evans via GitGitGadget
From: Julia Evans <[email protected]>
Because Git's branches are so flexible and the meaning of a branch is only what people assign to it, it's important to talk about some of the social context about branches. Do this in two ways:
1. Introduce some imaginary characters who are learning branches for the first time and examples of workflows they might use. Test readers said this was helpful because it helped them see different ways they might use branches.
This is extremely helpful, not least because of what it does for this doc, but because it sets up future guides to refer back to well-known scenarios. In particular, I'm mulling on a triangular workflow guide, which could feasibly be one component of a guide to collaboration with remotes; these characters will have different things to say about remotes!
Quoted 12 lines
On
git log:- Use the output of
git log --graphto visualize branches instead of a more abstract ASCII diagram, since it serves a double purpose of teaching people how to readgit log's output.
But also mention that folks may prefer to use a GUI to visualize branches.
- Include two sections to explain the output ofgit log, since several test readers found it very hard to understand. In that explanation, only explainHEADfairly briefly and instead keep the focus on the idea of a "current branch", since that's the term Git's UI normally uses.
Much appreciated. Another thing I've been doing at work is showing folks how to find a "map", as it were: my recipe for getting folks unstuck with Git is to
1. Find out where you are
2. Decide where you want to go
3. Discover how to get there
and git-log is crucial for step (1).
Quoted 41 lines
On the conceptual introduction at the beginning:
- One of the biggest issues with explaining branches is that there are 3 ways in Git to think of branches and it's not straightforward to explain how to reconcile them:
1. just the commits that "branch off"
2. the latest commit on the branch
3. the full history since the beginning of time
Handle this by starting with the "tree" metaphor where branches "branch off" a central trunk (way 1), since this makes sense with how people actually want to use branches. Point out some issues with the tree metaphor and talk about how it relates to the way Git represents branches (way 2).
- Avoid using computer science jargon, because it's a major barrier to folks without a CS background. It's not useful or realistic to teach most beginners the term "directed acyclic graph" just so they can learn branches. Mention the term "directed acyclic graph" briefly in a note for folks who are already familiar with that term and link folks to the data model if they'd like more information.
- Include some basic facts about commits. Folks seemed to find this helpful, one person said they didn't realize it would be so hard to change their email address later.To keep it readable:
- Don't assume that the reader has readgittutorial, and instead refer back to it explicitly if it's relevant.
- Always include the output for every command. Test readers said this was helpful.Other choices about material that is/isn't included:
-
git-prompt.sh(yes): Mention how important it is to be aware what branch you're on, and suggest usinggit-prompt.sh. Link togit-prompt.shon GitHub since several test readers couldn't find it on their machine. The test readers liked this.
- fast-forward merges (yes): Explain the difference between a fast-forward merge and a merge commit by giving examples of what each one looks like.
- upstreams/rebase (no): Don't explain what an upstream is or introduce rebase, since we're already introducing a lot of concepts. Instead include a "Where to learn more" section with some pointers to where to
Quoted 8 lines
continue learning.
- GUIs (yes): Some readers wanted a suggestion for what GUI to use. It's very hard to suggest an appropriate GUI because there are so many and so many of them are not open source, but give a couple of suggestions of open source tools that seem to be popular with the community. Also mention that many non-open-source IDEs have built-in Git integrations for folks who are already using those tools who might not be aware, since they're a popular way to use Git.
All quite sensible to me.
Quoted 4 lines
Documentation/gittutorial-branches.adoc new file +4
-0+Step 8: Create another branch +----------------------------- + +Now create a second branch called `add-animals`. We're creating two
Documentation/gittutorial-branches.adoc line 430
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+branches to show what the process of working on multiple changes at the +same time looks like. + +First, switch back to the `main` branch. This is important because +normally when you start a new branch, you'll start it from the +current `main`. + +We normally start from `main` because if you create a branch called `two` +while on the `one` branch, merging `two` into `main` will also +merge `one`. Starting from `main` keeps all the branches separate.
This feels a bit confusing to me on first read, if only because the "two" and "one" branches are hypothetical (and because we haven't covered merges yet). I don't love to keep the reader in suspense ("we'll explain why we do this later"), but I do wonder if we can give a better demonstration of what happens when we stack a branch like this by using the example names here?
Perhaps we can start with the idea that we want to keep work independent ("Branches are a tool to organize your work", "working on multiple unrelated changes"), and then after merges are introduced we can revisit the stacked version and show what happens in that scenario? There are pros and cons to stacking, but I think covering "branch per independent idea" as the norm is a good way to handle it.
(I've used "stack" here because that's what the industry is starting to call such things, though even then such a stack is usually held together by a set of related ideas. This example is independent enough that I would probably tell the author to split up the branches if they were stacked!)
Quoted 25 lines
Documentation/gittutorial-branches.adoc new file +18
-0+Step 13: Merge the `add-fruits` branch into `main` +-------------------------------------------------- +Here you can see that it created a new commit `e3c630d`. This commit +looks different from the other commits because it has two parents: +cd4bb48 ("Add fruit list") and 1ceb7b4 ("Add animal list"). This kind +of commit with two parents is called a "merge commit". +Note: merge conflicts +--------------------- +See linkgit:gitmergeconflicts[7] (`git help mergeconflicts`) for a +guide to handling merge conflicts. +3 ways to think about branches +------------------------------ +How to use branches to collaborate: it depends +---------------------------------------------- +Using `git pull`: the basics +---------------------------- +Example: Emilia the PhD student +-------------------------------
Documentation/gittutorial-branches.adoc
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +7
-0+Tip: put your current branch in your shell prompt +------------------------------------------------- +[[guis]] +GUI tools +--------- +Where to learn more +-------------------
Here are the last handful of sections, and 2 paragraphs that come between what I see as major sections. Here alas flat text bites us a bit: it's not particularly evident that step 13 + it's merge conflict note form "the end" of the practicum, and the stuff afterwards is (somewhat) back to the introductory concept idea or examples.
I don't have a particular way in mind to alleviate this, but some ideas:
• Transition paragraphs? ("This concludes the tutorial on XYZ; next, we'll cover a few more branching concepts")
• Perhaps the Examples ought to come earlier, but I'm not sure: we want both the branching material and the collaboration aspect for some. Maybe we could move the solo project earlier, and return to it to mention the backups/pushes after the later bits about push?
All in all, I'm quite excited to see this be available to folks.
Signature · 1 line
D. Ben Knoble
Quoted 9 lines
Quoted 4 lines
1. Introduce some imaginary characters who are learning branches for the first time and examples of workflows they might use. Test readers said this was helpful because it helped them see different ways they might use branches.
This is extremely helpful, not least because of what it does for this doc, but because it sets up future guides to refer back to well-known scenarios. In particular, I'm mulling on a triangular workflow guide, which could feasibly be one component of a guide to collaboration with remotes; these characters will have different things to say about remotes!
Cool! I think probably we'd want to re-introduce the characters each time (and maybe even choose different characters depending on the situation). For a more advanced workflow the characters might be different.
Quoted 20 lines
Quoted 4 lines
Documentation/gittutorial-branches.adoc new file +4
-0+Step 8: Create another branch +----------------------------- + +Now create a second branch called `add-animals`. We're creating twoDocumentation/gittutorial-branches.adoc line 430
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+branches to show what the process of working on multiple changes at the +same time looks like. + +First, switch back to the `main` branch. This is important because +normally when you start a new branch, you'll start it from the +current `main`. + +We normally start from `main` because if you create a branch called `two` +while on the `one` branch, merging `two` into `main` will also +merge `one`. Starting from `main` keeps all the branches separate.This feels a bit confusing to me on first read, if only because the "two" and "one" branches are hypothetical (and because we haven't covered merges yet). I don't love to keep the reader in suspense ("we'll explain why we do this later"), but I do wonder if we can give a better demonstration of what happens when we stack a branch like this by using the example names here?
Quoted 6 lines
Perhaps we can start with the idea that we want to keep work independent ("Branches are a tool to organize your work", "working on multiple unrelated changes"), and then after merges are introduced we can revisit the stacked version and show what happens in that scenario? There are pros and cons to stacking, but I think covering "branch per independent idea" as the norm is a good way to handle it.
I like this, I'll try just saying "You'll usually want to start all new branches from the main branch to keep your branches independent from each other."
I agree it's weird to talk about merging since we haven't introduced it yet. I'll take that out.
I think stacking is probably too advanced for this beginner tutorial.
Quoted 6 lines
Quoted 1 line
...
Here are the last handful of sections, and 2 paragraphs that come between what I see as major sections. Here alas flat text bites us a bit: it's not particularly evident that step 13 + it's merge conflict note form "the end" of the practicum, and the stuff afterwards is (somewhat) back to the introductory concept idea or examples.
I agree the last few sections are pretty messy, thanks for finding that.
I've reorganized things for v2 like this::
- Move the "merge conflicts" section into a note
- Move the "3 ways to think about branch" inline in the step-by-step tutorial, after step 6 (next to the example it's talking about)
- Rename the section about workflows to "Examples of using branches to collaborate" to make it clear that the git pull section is a subsection of the overall workflows section
- Add a little overview at the beginning explaining the structure of the document:
This tutorial contains:
1. <<intro,An introduction to branches>>
2. <<tutorial,A step-by-step practical tutorial>>
3. <<workflows,Example collaboration workflows>>
4. <<tip,Tips, GUI tools, and where to learn more>>
"Tips, GUI tools, and where to learn more" is still a little bit of a grab bag at the end but at least it's laid out at the beginning now
Thanks for the review!
Quoted 69 lines · Julia Evans
Quoted 10 lines
Quoted 5 lines
1. Introduce some imaginary characters who are learning branches for the first time and examples of workflows they might use. Test readers said this was helpful because it helped them see different ways they might use branches.
This is extremely helpful, not least because of what it does for this doc, but because it sets up future guides to refer back to well-known scenarios. In particular, I'm mulling on a triangular workflow guide, which could feasibly be one component of a guide to collaboration with remotes; these characters will have different things to say about remotes!
Cool! I think probably we'd want to re-introduce the characters each time (and maybe even choose different characters depending on the situation). For a more advanced workflow the characters might be different.
Quoted 20 lines
Quoted 4 lines
Documentation/gittutorial-branches.adoc new file +4
-0+Step 8: Create another branch +----------------------------- + +Now create a second branch called `add-animals`. We're creating twoDocumentation/gittutorial-branches.adoc line 430
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+branches to show what the process of working on multiple changes at the +same time looks like. + +First, switch back to the `main` branch. This is important because +normally when you start a new branch, you'll start it from the +current `main`. + +We normally start from `main` because if you create a branch called `two` +while on the `one` branch, merging `two` into `main` will also +merge `one`. Starting from `main` keeps all the branches separate.This feels a bit confusing to me on first read, if only because the "two" and "one" branches are hypothetical (and because we haven't covered merges yet). I don't love to keep the reader in suspense ("we'll explain why we do this later"), but I do wonder if we can give a better demonstration of what happens when we stack a branch like this by using the example names here?
Quoted 6 lines
Perhaps we can start with the idea that we want to keep work independent ("Branches are a tool to organize your work", "working on multiple unrelated changes"), and then after merges are introduced we can revisit the stacked version and show what happens in that scenario? There are pros and cons to stacking, but I think covering "branch per independent idea" as the norm is a good way to handle it.
I like this, I'll try just saying "You'll usually want to start all new branches from the
mainbranch to keep your branches independent from each other."I agree it's weird to talk about merging since we haven't introduced it yet. I'll take that out.
I think stacking is probably too advanced for this beginner tutorial.
Quoted 6 lines
Quoted 1 line
...
Here are the last handful of sections, and 2 paragraphs that come between what I see as major sections. Here alas flat text bites us a bit: it's not particularly evident that step 13 + it's merge conflict note form "the end" of the practicum, and the stuff afterwards is (somewhat) back to the introductory concept idea or examples.
I agree the last few sections are pretty messy, thanks for finding that.
I've reorganized things for v2 like this::
- Move the "merge conflicts" section into a note
- Move the "3 ways to think about branch" inline in the step-by-step tutorial, after step 6 (next to the example it's talking about)
- Rename the section about workflows to "Examples of using branches to collaborate" to make it clear that thegit pullsection is a subsection of the overall workflows section
- Add a little overview at the beginning explaining the structure of the document:This tutorial contains:
Quoted 8 lines
1. <<intro,An introduction to branches>>
2. <<tutorial,A step-by-step practical tutorial>>
3. <<workflows,Example collaboration workflows>>
4. <<tip,Tips, GUI tools, and where to learn more>>"Tips, GUI tools, and where to learn more" is still a little bit of a grab bag at the end but at least it's laid out at the beginning now
Thanks for the review!
All sounds great to me :)
Quoted 14 lines · Julia Evans via GitGitGadget
From: Julia Evans <[email protected]>
...Documentation/gittutorial-branches.adoc new file +12
-0+Next, edit README.md and make another commit, like this: + +---- +git commit -am "Update README.md" +---- + +`git commit -am` commits the current version of all files that Git is +tracking. + +Now we're set up and we can start creating branches. + +Step 2: Find your current branch
Documentation/gittutorial-branches.adoc line 231
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+-------------------------------- + +In Git, you normally have a "current branch". You can see your current +branch by running `git status`. It's usually on the first line, like this: + +---- +$ git status + +On branch main +----
After reading through the steps, the abrupt appearance of the "$" prompt feels a bit disorienting. You have a good reason to use a prompt here, to make it possible to tell what you type and what the commands tell you apart. Wouldn't it be better to use the prompt consistently, e.g., making the last example of the previous step
----
$ git commit -a -m "Update README.md" ----
instead?
Side note. Yesterday I learned while reviewing an unrelated patch that we encourage users not to squish short options together into a single word, so I split your "-am <msg>" in the above rewrite of the example.
There are a handful of other places where you add the "$" prompt in a block that has both input and output, and many blocks that lack the "$" prompt. The mixture looks rather messy.
Quoted 11 lines
Documentation/gittutorial-branches.adoc new file +11
-0+Example: Emilia the PhD student +------------------------------- + +Emilia is writing her PhD thesis. She follows the steps from +linkgit:gittutorial[7] to set up a Git repository on the Internet so +that she can back up her work. + +To back up her work after making a commit, she runs: + +---- +git push origin main
Documentation/gittutorial-branches.adoc line 739
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+---- + +Sometimes, she creates a branch to make an experimental change she's not +sure that she's going to keep, like `rewrite-chapter-5`. + +She does this by running: + +---- +git switch -c rewrite-chapter-5 +----
Would it help to explicitly mention that after running 'switch -c' to create the branch, Emilia actually does some work on it before the "If she decides to keep/If she decides to discard" paragraphs?
Quoted 5 lines
Documentation/gittutorial-branches.adoc new file +5
-0+ +If she decides that the experimental change should be merged, she merges +it into `main` like this: + +----
Documentation/gittutorial-branches.adoc line 754
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+git switch main +git merge rewrite-chapter-5 +---- + +Or if she decides the rewrite of chapter 5 was a bad idea, she can +delete the branch: + +---- +git branch -D rewrite-chapter-5 +----
This is slightly misleading after saying "Or if she decides", which implies that the previous one did not happen. If she did not switch to 'main' and merge, she would still be on the rewrite-chapter-5 branch, and the "branch -D", even with a capital "-D", would fail.
Perhaps make the example switch back to 'main' before discarding the branch?
Documentation/gittutorial-branches.adoc line 841
Quoted 4 lines
Documentation/gittutorial-branches.adoc new file +4
-0+Example: Daniela and Carmen with the cat shelter website +-------------------------------------------------------- + +Daniela and Carmen create a Git repository for their cat shelter website.
A very minor thing, but wouldn't breaking the alphabetical order of appearance of the characters be a bit jarring to readers? Does this example need to be about Daniela and Carmen, not Carmen and Daniela?
Quoted 9 lines
Documentation/gittutorial-branches.adoc new file +9
-0+They both clone it onto their computer like this: + +---- +git clone [email protected]:cat-shelter/kitties.git +---- + +They decide to work together by just pushing all of their commits to the +`main` branch, without using any other branches. +
Documentation/gittutorial-branches.adoc line 861
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+When Daniela wants to do work, she first runs `git pull` on the main +branch, like this: + +---- +git pull +---- + +She also checks in with Carmen to make sure they won't be working on +the same files. Then she works on her changes. Then, before pushing her +changes, she runs this command to merge any changes Carmen has made.
Wouldn't it be helpful to be more explicit that before running this command (git pull), she commits all her work? We assume that "pull" somehow finishes cleanly, so ...
Quoted 4 lines
+----
Documentation/gittutorial-branches.adoc new file +3
-0+git pull --no-rebase +---- +
Documentation/gittutorial-branches.adoc line 876
Quoted 10 lines
Documentation/gittutorial-branches.adoc new file +10
-0+We don't have space for a full introduction to linkgit:git-pull[1] +here, but `git pull` fetches Carmen's changes and merges them into the +current branch. The `--no-rebase` flag tells Git to use `git merge` to +do the merge. + +Then she pushes her changes with: + +---- +git push +----
... there is no extra need for "git commit" before the "git push" that comes after the "git pull --no-rebase".
Thanks.
PATCH v2 1/1 doc: add tutorial on branches for beginners
In reply to Julia Evans via GitGitGadgetFrom: Julia Evans <[email protected]>
Because Git's branches are so flexible and the meaning of a branch is only what people assign to it, it's important to talk about some of the social context about branches. Do this in two ways:
1. Introduce some imaginary characters who are learning branches for the first time and examples of workflows they might use. Test readers said this was helpful because it helped them see different ways they might use branches.
2. Explain the importance of a "main" or "master" branch in many projects, and give a few examples of how projects might use it.
On git log:
- Use the output of git log --graph to visualize branches instead of a more abstract ASCII diagram, since it serves a double purpose of teaching people how to read git log's output.
But also mention that folks may prefer to use a GUI to visualize branches.
- Include two sections to explain the output of git log, since several test readers found it very hard to understand. In that explanation, only explain HEAD fairly briefly and instead keep the focus on the idea of a "current branch", since that's the term Git's UI normally uses.
On the conceptual introduction at the beginning:
- One of the biggest issues with explaining branches is that there are 3 ways in Git to think of branches and it's not straightforward to explain how to reconcile them:
1. just the commits that "branch off"
2. the latest commit on the branch
3. the full history since the beginning of time
Handle this by starting with the "tree" metaphor where branches "branch off" a central trunk (way 1), since this makes sense with how people actually want to use branches. Point out some issues with the tree metaphor and talk about how it relates to the way Git represents branches (way 2).
- Avoid using computer science jargon, because it's a major barrier to folks without a CS background. It's not useful or realistic to teach most beginners the term "directed acyclic graph" just so they can learn branches. Mention the term "directed acyclic graph" briefly in a note for folks who are already familiar with that term and link folks to the data model if they'd like more information.
- Include some basic facts about commits. Folks seemed to find this helpful, one person said they didn't realize it would be so hard to change their email address later.
To keep it readable:
- Don't assume that the reader has read gittutorial, and instead refer back to it explicitly if it's relevant.
- Always include the output for every command. Test readers said this was helpful.
Other choices about material that is/isn't included:
- git-prompt.sh (yes): Mention how important it is to be aware what branch you're on, and suggest using git-prompt.sh. Link to git-prompt.sh on GitHub since several test readers couldn't find it on their machine. The test readers liked this.
- fast-forward merges (yes): Explain the difference between a fast-forward merge and a merge commit by giving examples of what each one looks like.
- upstreams/rebase (no): Don't explain what an upstream is or introduce rebase, since we're already introducing a lot of concepts. Instead include a "Where to learn more" section with some pointers to where to continue learning.
- GUIs (yes): Some readers wanted a suggestion for what GUI to use. It's very hard to suggest an appropriate GUI because there are so many and so many of them are not open source, but give a couple of suggestions of open source tools that seem to be popular with the community. Also mention that many non-open-source IDEs have built-in Git integrations for folks who are already using those tools who might not be aware, since they're a popular way to use Git.
---
doc: add tutorial on branches for beginners
This was reviewed by 17 test readers who overall said it helped them understand branches. We addressed the feedback from the test readers to improve it before submitting it here.
I think it would be good to add a little table of contents to gittutorial.adoc to match this one's TOC as well. I'm not sure what the best way to do that is since my understanding is that rebasing on the latest master causes problems.
Changes in v2:
* Use a $ for the commands for consistency (thanks to Junio)
* Add more details in the example workflows to make sure they make sense logically (thanks to Junio)
* Make sure the characters are alphabetized in the section order and in sentences. Rename Emilia -> Anita, Abdul -> Bruno, Betsy -> Carmen, Carmen and Daniela -> Daniela and Emilia. Keep the genders the same so we don't have to change all the pronouns. (thanks to Junio)
* Deal with the poorly structured sections that Ben pointed at the end as follows:
* Add a summary at the beginning to explain the overall structure
* Move "3 ways to think about a branch" to the middle, right after creating the branch we're using as an example
* Move the merge conflicts section to a note at the end of Step 13
* Rename some of the headings to more clearly explain the structure (around "the step by step tutorial" and "Examples of using branches to collaborate")
* Delete the "We normally start from main because if you create a branch called two..." explanation and instead just say that we want to keep branches independent (thanks to Ben)
* One test reader said that in "3 ways to think about a branch", it was confusing to them than #1 and #3 were the same (just one commit). Add an extra commit to the add-fruits branch so that the branch has 2 commits, to make it more clear what a branch with multiple commits on it looks like.
Note: I used this script
https://gist.github.com/jvns/bcd36fdd42952b9ec9a67ec139dba13e to create the example repository used in this document and to copy and paste the example output from. Not sure if we should store the contents of that script somewhere: it's very annoying to update the examples without it. I also realized there's an issue with that script where, because it hardcodes GIT_COMMITTER_DATE, all the commits happen at the same time, which makes the order of git log a bit weird. I manually adjusted Step 12 to account for that.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2307%2Fjvns%2Ftutorial-branches-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2307/jvns/tutorial-branches-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2307
Range-diff vs v1:
1: 5eaecda843 ! 1: d6824feca4 doc: add tutorial on branches for beginners
@@ Documentation/gittutorial-branches.adoc (new)
+- Part 1 (linkgit:gittutorial[7]): Introduction to commits and git push +- Part 2 (you are here): Introduction to branches
+
++This tutorial contains:
++
++1. <<intro,An introduction to branches>>
++2. <<tutorial,A step-by-step practical tutorial>>
++3. <<workflows,Example collaboration workflows>>
++4. <<tip,Tips, GUI tools, and where to learn more>>
++
++
+Meet some example Git users
+---------------------------
+
@@ Documentation/gittutorial-branches.adoc (new)
+branches differently in different situations, so it might be useful for +you to think about which is closest to your situation.
+
-+1. Abdul is making his first contribution to Python, a large open ++1. Anita is using Git on her own to write her PhD thesis with LaTeX. ++2. Bruno is making his first contribution to Python, a large open
+ source project.
-+2. Betsy is a Go developer for a social media website which has 100 ++3. Carmen is a Go developer for a social media website which has 100
+ other software developers. All of her changes need to be reviewed by a
+ coworker before they're released.
-+3. Carmen and Daniela are building a website for their local cat ++4. Daniela and Emilia are building a website for their local cat
+ shelter. They're the only two people on the project.
-+4. Emilia is using Git on her own to write her PhD thesis with LaTeX.
+
++[[intro]]
+Why use branches?
+-----------------
+
@@ Documentation/gittutorial-branches.adoc (new)
+Another update to README.md could originally have been created on a +different branch that was deleted after merging it into main.
+
-+The practical part
-+------------------
++[[tutorial]]
++Step-by-step tutorial
++---------------------
+
+Now let's talk about how to actually use branches.
+
@@ Documentation/gittutorial-branches.adoc (new)
+First, create an empty directory, move into it, and start a new Git repository:
+
+----
-+mkdir myproject
-+cd myproject
-+git init
++$ mkdir myproject
++$ cd myproject
++$ git init
+----
+
+Then open your text editor and create a file called README.md, with
@@ Documentation/gittutorial-branches.adoc (new)
+Then add the file and commit it, by running these commands:
+
+----
-+git add README.md
-+git commit -m "Initial commit"
++$ git add README.md
++$ git commit -m "Initial commit"
+----
+
+Next, edit README.md and make another commit, like this:
+
+----
-+git commit -am "Update README.md"
++$ git commit -am "Update README.md"
+----
+
+git commit -am commits the current version of all files that Git is
@@ Documentation/gittutorial-branches.adoc (new)
+
+
+----
-+git log --oneline
++$ git log --oneline
+----
+
+The output should look something like this, though with
@@ Documentation/gittutorial-branches.adoc (new)
+We're only going to talk about 2 kinds of decorations here:
+(main) and (HEAD -> main). Here's how to read each of them:
+
-+- e9c0b17 (main) means that e9c0b17 is the latest commit on the main branch. ++- e9c0b17 (main) means that e9c0b17 is the latest commit
++ on the main branch.
+- e9c0b17 (HEAD -> main) means that e9c0b17 is the latest commit on
+ the main branch, AND that main is the current branch.
+
@@ Documentation/gittutorial-branches.adoc (new)
+Now make a third commit, by editing README.md again, and running this command:
+
+----
-+git commit -am "Another update to README.md"
++$ git commit -am "Another update to README.md"
+----
+
+Now run git log --oneline again to see how our branch looks now.
@@ Documentation/gittutorial-branches.adoc (new)
+First, create the branch and switch to it, by running this command:
+
+----
-+git switch -c add-fruits
++$ git switch -c add-fruits
+----
+
+The -c flag stands for "create". You should see the output:
@@ Documentation/gittutorial-branches.adoc (new)
+some fruits in it. Then add it and commit, like this:
+
+----
-+git add fruits.txt
-+git commit -m "Add fruit list"
++$ git add fruits.txt
++$ git commit -m "Add fruit list"
++----
++
++Now edit fruits.txt again and commit it again, so that we can see what ++it looks like when you make 2 commits on a branch.
++
++----
++$ git add fruits.txt
++$ git commit -m "Update fruit list"
+----
+
+Run git log --oneline again to get a list of all the commits on the +current branch.
+
+----
-+cd4bb48 (HEAD -> add-fruits) Add fruit list
++36d0cbf (HEAD -> add-fruits) Update fruit list
++cd4bb48 Add fruit list
+37634fd (main) Another update to README.md
+e9c0b17 Update README.md
+2e018a5 Initial commit
@@ Documentation/gittutorial-branches.adoc (new)
+See <<read_oneline,How to read git log --oneline's output>> above for +an explanation of how to interpret this output.
+
-+To summarize, this tells us that there's been 1 commit ("Add fruit list") -+since our add-fruits branch split off from the main branch.
++To summarize, this tells us that there have been 2 commits
++("Add fruit list" and "Update fruit list") since our add-fruits branch ++split off from the main branch.
++
++3 ways to think about a branch
++------------------------------
++
++In Git there are always 3 ways to think about a branch, and different ++commands will treat a branch in different ways. For example, let's look ++at the 3 ways to think about this add-fruits branch.
++
++-----
++* 36d0cbf (HEAD -> add-fruits) Update fruit list
++* cd4bb48 Add fruit list
++* 37634fd (main) Another update to README.md
++* e9c0b17 Update README.md
++* 2e018a5 Initial commit
++-----
++
++We can think of the add-fruits branch as:
++
++1. Just the 2 commits that "branched off" of the main branch
++ (cd4bb48 and 36d0cbf).
++2. The full history of the branch since the beginning of the repository,
++ including all the commits on main (5 commits in this case).
++ For example, git log add-fruits will show you the full history.
++3. Just the latest commit on that branch (36d0cbf).
++ For example, git show add-fruits will show you the latest commit.
+
+Step 6: Get a list of all branches
+----------------------------------
@@ Documentation/gittutorial-branches.adoc (new)
+You can get a list of all branches by running this command:
+
+----
-+git branch
++$ git branch
+----
+
+The output should look like this:
@@ Documentation/gittutorial-branches.adoc (new)
+Change your current branch to main by running:
+
+----
-+git switch main
++$ git switch main
+----
+
+This command:
@@ Documentation/gittutorial-branches.adoc (new)
+README.md
+-----
+
++[NOTE]
++Older Git tutorials used git checkout to switch branches.
++git switch is a newer command which is easier to learn because it ++only has one job.
++
+Step 8: Create another branch
+-----------------------------
+
@@ Documentation/gittutorial-branches.adoc (new)
+branches to show what the process of working on multiple changes at the +same time looks like.
+
-+First, switch back to the main branch. This is important because -+normally when you start a new branch, you'll start it from the -+current main.
-+
-+We normally start from main because if you create a branch called two -+while on the one branch, merging two into main will also -+merge one. Starting from main keeps all the branches separate. ++You'll usually want to start all new branches from the main branch ++to keep your branches independent from each other. Right now
++we're on the add-fruits branch, so we need to switch back to the ++main branch:
+
+-----
-+git switch main
++$ git switch main
+-----
+
+Then create a branch called add-animals.
+
+-----
-+git switch -c add-animals
++$ git switch -c add-animals
+-----
+
+Now create a file named animals.txt with some animal names in it, add +it, and commit it.
+
+-----
-+git add animals.txt
-+git commit -m "Add animal list"
++$ git add animals.txt
++$ git commit -m "Add animal list"
+-----
+
+Step 9: Look at our branches and commits
@@ Documentation/gittutorial-branches.adoc (new)
+Run this command to show all branches and commits:
+
+-----
-+git log --oneline --graph --all
++$ git log --oneline --graph --all
+-----
+
+The output should look like this:
+
+-----
+* 1ceb7b4 (HEAD -> add-animals) Add animal list
-+| * cd4bb48 (add-fruits) Add fruit list
++| * 36d0cbf (add-fruits) Update fruit list
++| * cd4bb48 Add fruit list
+|/
+* 37634fd (main) Another update to README.md
+* e9c0b17 Update README.md
@@ Documentation/gittutorial-branches.adoc (new)
+
+-----
+* 1ceb7b4 (HEAD -> add-animals) Add animal list
-+| * cd4bb48 (add-fruits) Add fruit list
++| * 36d0cbf (add-fruits) Update fruit list
++| * cd4bb48 Add fruit list
+|/
+* 37634fd (main) Another update to README.md
+* e9c0b17 Update README.md
@@ Documentation/gittutorial-branches.adoc (new)
+See <<read_oneline,How to read git log --oneline's output>> above for +how to read the decorations like (main) or (HEAD -> add-animals). +
-+Overall this shows that the add-animals and add-fruits branches have -+both added one new commit since they split off from the main branch. ++Overall this shows that the add-animals branch has added 1 commit and ++add-fruits has added 2 commits since they split off from the main ++branch.
+
+Step 10: Merge the add-animals branch into main
+---------------------------------------------------
@@ Documentation/gittutorial-branches.adoc (new)
+Merge add-animals into the main branch by running these two commands:
+
+----
-+git switch main
-+git merge add-animals
++$ git switch main
++$ git merge add-animals
+----
+
+The output should look like this:
@@ Documentation/gittutorial-branches.adoc (new)
+Step 11: Delete the add-animals branch
+----------------------------------------
+
-+Now that you've integrated add-animals branch into main, you can ++Now that you've integrated the add-animals branch into main, you can
+safely delete it. Delete the branch by running this command. -d stands +for "delete":
+
+-----
-+git branch -d add-animals
++$ git branch -d add-animals
+-----
+
+Running this is safe: it'll refuse to delete any branch that hasn't
@@ Documentation/gittutorial-branches.adoc (new)
+and commits again:
+
+-----
-+git log --oneline --graph --all
++$ git log --oneline --graph --all
+-----
+
+The output should look like this:
+
+-----
-+* cd4bb48 (add-fruits) Add fruit list
-+| * 1ceb7b4 (HEAD -> main) Add animal list
++* 1ceb7b4 (HEAD -> main) Add animal list
++| * 36d0cbf (add-fruits) Update fruit list
++| * cd4bb48 Add fruit list
+|/
+* 37634fd Another update to README.md
+* e9c0b17 Update README.md
@@ Documentation/gittutorial-branches.adoc (new)
+Now run git merge to see what happens:
+
+----
-+git merge --no-edit add-fruits
++$ git merge --no-edit add-fruits
+----
+
+The --no-edit option tells git merge to use the default commit
@@ Documentation/gittutorial-branches.adoc (new)
+Once the branch is merged, delete it by running:
+
+----
-+git branch -d add-fruits
++$ git branch -d add-fruits
+----
+
+To see what git merge did, run git log --oneline --graph again. +
+----
-+* e3c630d (HEAD -> main) Merge branch 'add-fruits'
++* 576d053 (HEAD -> main) Merge branch 'add-fruits'
+|\
++| * 36d0cbf Update fruit list
+| * cd4bb48 Add fruit list
+* | 1ceb7b4 Add animal list
+|/
@@ Documentation/gittutorial-branches.adoc (new)
+* 2e018a5 Initial commit
+----
+
-+Here you can see that it created a new commit e3c630d. This commit ++Here you can see that it created a new commit 576d053. This commit
+looks different from the other commits because it has two parents:
-+cd4bb48 ("Add fruit list") and 1ceb7b4 ("Add animal list"). This kind ++36d0cbf ("Update fruit list") and 1ceb7b4 ("Add animal list"). This kind
+of commit with two parents is called a "merge commit".
+
-+Note: merge conflicts
-+---------------------
-+
-+Git has an automatic merge algorithm to combine the code from two -+branches. In the example from Step 13, it's simple: one side added -+fruits.txt and the other side added animals.txt, so the merge adds -+both files.
-+
-+But if the two branches both edited the same lines of the same files, -+Git will ask you to decide how to handle it. This is called a "merge -+conflict".
-+
-+See linkgit:gitmergeconflicts[7] (git help mergeconflicts) for a -+guide to handling merge conflicts.
-+
-+3 ways to think about branches
-+------------------------------
-+
-+In Git there are always 3 ways to think about a branch, and different -+commands will treat a branch in different ways. For example, let's look -+at the earlier setup again with the add-fruits branch. It has a main -+branch with 3 commits on it, and an add-fruits branch with one commit. -+
-+-----
-+ * cd4bb48 (add-fruits) Add fruit list
-+ /
-+* 37634fd (main) Another update to README.md
-+* e9c0b17 Update README.md
-+* 2e018a5 Initial commit
-+-----
-+
-+We can think of the add-fruits branch as:
-+
-+1. Just the 1 commit that "branched off" of the main branch (cd4bb48). -+2. The full history of the branch since the beginning of the repository,
-+ including all the commits on main (4 commits in this case).
-+ For example, git log add-fruits will show you the full history.
-+3. Just the latest commit on that branch.
-+ For example, git show add-fruits will show you the latest commit.
++[NOTE]
++If you merge two branches which both edited the same lines of the ++same files, Git will ask you to decide how to handle it. This is ++called a "merge conflict". See linkgit:gitmergeconflicts[7]
++(git help mergeconflicts) for a guide to handling merge conflicts.
+
-+How to use branches to collaborate: it depends
-+----------------------------------------------
++[[workflows]]
++Examples of using branches to collaborate
++-----------------------------------------
+
+There are lots of ways to use branches to collaborate with other people. +We don't have space for a full guide to collaborating with Git here, but
-+after a very quick introduction to git pull, we'll give examples of ++we'll give a quick introduction to git pull and examples of
+how each of our characters from the beginning might use branches.
+
+In these examples we're going to use git push, git pull, git +clone, and git remote add. For a basic introduction to git push and +git remote add, see Step 14 of linkgit:gittutorial[7].
+
-+Using git pull: the basics
-+----------------------------
++Intro to git pull
++-------------------
+
+Here's the simplest way to use git pull.
+
@@ Documentation/gittutorial-branches.adoc (new)
+branch.
+
+-------
-+git clone https://example.com/example.git
++$ git clone https://example.com/example.git
+-------
+
+After cloning a repository, you can run git pull while on the main +branch to update it with the latest version, like this:
+
+-------
-+git switch main
-+git pull
++$ git switch main
++$ git pull
+-------
+
+This is all you need to know about git pull to understand these +examples.
+
-+Example: Emilia the PhD student
-+-------------------------------
++Example: Anita the PhD student
++------------------------------
+
-+Emilia is writing her PhD thesis. She follows the steps from
++Anita is writing her PhD thesis. She follows the steps from
+linkgit:gittutorial[7] to set up a Git repository on the Internet so +that she can back up her work.
+
+To back up her work after making a commit, she runs:
+
+----
-+git push origin main
++$ git push origin main
+----
+
+Sometimes, she creates a branch to make an experimental change she's not
@@ Documentation/gittutorial-branches.adoc (new)
+She does this by running:
+
+----
-+git switch -c rewrite-chapter-5
++$ git switch -c rewrite-chapter-5
+----
+
-+If she decides that the experimental change should be merged, she merges ++Then she makes an experimental change, like rewriting Chapter 5 and commits it. ++If she decides that the change should be merged, she merges
+it into main like this:
+
+----
-+git switch main
-+git merge rewrite-chapter-5
++$ git switch main
++$ git merge rewrite-chapter-5
+----
+
+Or if she decides the rewrite of chapter 5 was a bad idea, she can
-+delete the branch:
++switch back to main and delete the branch:
+
+----
-+git branch -D rewrite-chapter-5
++$ git switch main
++$ git branch -D rewrite-chapter-5
+----
+
-+Example: Abdul the open source contributor
++Example: Bruno the open source contributor
+------------------------------------------
+
-+Abdul is contributing to an open source project for the first time. ++Bruno is contributing to an open source project for the first time.
+
+First, he finds the official Git repository for the project and clones +it, like this:
+
+----
-+git clone https://example.com/project/project
++$ git clone https://example.com/project/project
+----
+
+He doesn't have permission to push changes directly to that +project, so he creates his own copy of the repository at
-+https://example.com/abdul/project. ++https://example.com/bruno/project.
+
+Then he adds that repository as a second remote, like this:
+
+----
-+git remote add abdul https://example.com/abdul/project
++$ git remote add bruno https://example.com/bruno/project
+----
+
+Before starting work on his branch, he makes sure that he has the +latest version of the main branch using git pull, like this:
+
+----
-+git switch main
-+git pull
++$ git switch main
++$ git pull
+----
+
+Then he creates a new branch, like this:
+
+----
-+git switch -c new-feature
++$ git switch -c new-feature
+----
+
+Once he's done with his branch, he pushes the branch to his Git +repository, like this:
+
+----
-+git push abdul new-feature
++$ git push bruno new-feature
+----
+
+Then he goes through the open source project's process to submit his +branch for review.
+
-+Example: Betsy the corporate employee
-+-------------------------------------
++Example: Carmen the corporate employee
++--------------------------------------
+
+On her first day of work, she clones the company's Git repository by +running:
+
+----
-+git clone [email protected]:company/project
++$ git clone [email protected]:company/project
+----
+
+She has access to push new branches to this repository with her work,
@@ Documentation/gittutorial-branches.adoc (new)
+latest version of the main branch using git pull, like this:
+
+----
-+git switch main
-+git pull
++$ git switch main
++$ git pull
+----
+
+Then she creates a new branch, like this:
+
+----
-+git switch -c new-feature
++$ git switch -c new-feature
+----
+
+Once she's done with her branch, she pushes the branch to the company +Git repository, like this:
+
+----
-+git push origin new-feature
++$ git push origin new-feature
+----
+
+Then she goes through her company's process for getting branches merged. +
-+Example: Daniela and Carmen with the cat shelter website
++Example: Daniela and Emilia with the cat shelter website
+--------------------------------------------------------
+
-+Daniela and Carmen create a Git repository for their cat shelter website. ++Daniela and Emilia create a Git repository for their cat shelter website.
+
+They both clone it onto their computer like this:
+
+----
-+git clone [email protected]:cat-shelter/kitties.git
++$ git clone [email protected]:cat-shelter/kitties.git
+----
+
+They decide to work together by just pushing all of their commits to the +main branch, without using any other branches.
+
-+When Daniela wants to do work, she first runs git pull on the main ++When Emilia wants to do work, she first runs git pull on the main
+branch, like this:
+
+----
-+git pull
++$ git pull
+----
+
-+She also checks in with Carmen to make sure they won't be working on -+the same files. Then she works on her changes. Then, before pushing her -+changes, she runs this command to merge any changes Carmen has made. ++She also checks in with Daniela to make sure they won't be working on ++the same files. Then she works on her changes and commits them. ++Then, before pushing her changes, she runs this command to merge any ++changes Daniela has made.
+
+----
-+git pull --no-rebase
++$ git pull --no-rebase
+----
+
+We don't have space for a full introduction to linkgit:git-pull[1]
-+here, but git pull fetches Carmen's changes and merges them into the ++here, but git pull fetches Daniela's changes and merges them into the
+current branch. The --no-rebase flag tells Git to use git merge to +do the merge.
+
+Then she pushes her changes with:
+
+----
-+git push
++$ git push
+----
+
++[[tip]]
+Tip: put your current branch in your shell prompt
+-------------------------------------------------
+
Documentation/Makefile | 1 + Documentation/gittutorial-branches.adoc | 964 ++++++++++++++++++++++++ Documentation/meson.build | 1 + command-list.txt | 1 + 4 files changed, 967 insertions(+) create mode 100644 Documentation/gittutorial-branches.adoc
Documentation/Makefile +1 -0
@@ -65,6 +65,7 @@ MAN7_TXT += gitremote-helpers.adoc MAN7_TXT += gitrevisions.adoc MAN7_TXT += gitsubmodules.adoc MAN7_TXT += gittutorial-2.adoc +MAN7_TXT += gittutorial-branches.adoc MAN7_TXT += gittutorial.adoc MAN7_TXT += gitworkflows.adoc
Documentation/gittutorial-branches.adoc new file +964 -0
964 lines not on this page · raw message
Documentation/meson.build +1 -0
@@ -208,6 +208,7 @@ manpages = { 'gitrevisions.adoc' : 7, 'gitsubmodules.adoc' : 7, 'gittutorial-2.adoc' : 7, + 'gittutorial-branches.adoc' : 7, 'gittutorial.adoc' : 7, 'gitworkflows.adoc' : 7, }
command-list.txt +1 -0
@@ -245,6 +245,7 @@ gitrepository-layout userinterfaces gitrevisions userinterfaces gitsubmodules guide gittutorial guide +gittutorial-branches guide gitweb ancillaryinterrogators gitworkflows guide scalar mainporcelain
base-commit: 3c60ef1e38ee80337025a1b908327f7879386329
Signature · 1 line
gitgitgadget