Github repo master branch

My computer

C1

C2

C1

C2

I check out a new branch to work on: a copy of my local master branch

Jared's computer

C1

C2

C3

C3

C3

C1

C2

C3

Let's introduce branches!

In it, I make some changes to the file datamerge.do (and save as normal)

master branch

git checkout -b "ExampleBranch"

Github repo master branch

My computer

C1

C2

C1

C2

Jared's computer

C1

C2

C3

C3

C3

C1

C2

C3

git checkout -b "ExampleBranch"

Let's introduce branches!

master branch

ExampleBranch

I check out a new branch to work on: a copy of my local master branch

In it, I make some changes to the file datamerge.do (and save as normal)

Github repo master branch

My computer

C1

C2

C1

C2

In the meantime, Jared has added code to the same file

He stages and commits these changes

Jared's computer

C1

C2

git add datamerge.do
git commit -m "Fix error in datamerge.do"

C3

C3

C3

C4

C1

C2

C3

Branches

Github repo master branch

My computer

C1

C2

C1

C2

...and pushes to the remote on GitHub

In Dropbox, two people working on the same file is asking for a "conflicted copy"

Jared's computer

C1

C2

git add datamerge.do
git commit -m "Fix error in datamerge.do"
git push origin master

C3

C3

C3

C4

C1

C2

C3

C4

Branches

Github repo master branch

My computer

C1

C2

C1

C2

My local master is now out of sync with the one on GitHub

But I merrily stage and commit my work

 

 

Jared's computer

C1

C2

git add datamerge.do
git commit -m "Fix issue in datamerge.do"

C3

C3

C3

C4

C1

C2

C3

C4

C5

Github repo master branch

My computer

C1

C2

C1

C2

I pull the latest changes from GitHub

 

 

Jared's changes were on the master branch, so when I pull them they go to my master branch

Jared's computer

C1

C2

git add datamerge.do
git commit -m "Fix issue in datamerge.do"
git checkout master
git pull origin

C3

C3

C3

C4

C1

C2

C3

C4

C5

C4

Github repo master branch

My computer

C1

C2

C1

C2

The last commit on my branch is now different from the master

This means I have to merge my changes to my master branch

Jared's computer

C1

C2

C3

C3

C3

C4

C1

C2

C3

C4

C5

C4

Github repo master branch

My computer

C1

C2

C1

C2

The last commit on my branch is now different from the master

This means I have to merge my changes to my master branch

Any conflicts between the two files get resolved in a merge commit, C6

Jared's computer

C1

C2

C3

C3

C3

C4

C4

C5

C4

C6

Github repo master branch

My computer

C1

C2

C1

C2

The good news is that git is really good at resolving conflicts

If Jared and I edited different lines in the file, it will merge automatically

If we both changed the same lines, git will ask which of those I want to keep

Jared's computer

C1

C2

C3

C3

C3

C4

C4

C5

C4

C6

Github repo master branch

My computer

C1

C2

C1

C2

To get a sense of how branches work, imagine we've both cloned the repo

 

Note: this is a workflow that is more sensible than working on directly on the master branch

Jared's computer

C1

C2

Github repo master branch

My computer

C1

C2

C1

C2

To get a sense of how branches work, imagine we've both cloned the repo

- We both check out new branches

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git checkout -b "NewDataMerge"

Github repo master branch

My computer

C1

C2

C1

C2

To get a sense of how branches work, imagine we've both cloned the repo

- We both check out new branches

- We both make changes, stage, and commit them

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"

C4

C3

Github repo master branch

My computer

C1

C2

C1

C2

Now there are two options for merging in the new changes:

1. Merge locally
 

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"

C4

C3

Github repo master branch

My computer

C1

C2

C1

C2

Now there are two options for merging in the new changes:

1. Merge locally

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout master
git merge Datacleaning
git branch -d Datacleaning
git push origin
git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"

C4

C3

Check out the master branch

Merge in the changes on the branch

Delete the branch (optional)

Github repo master branch

My computer

C1

C2

C1

C2

Now there are two options for merging in the new changes:

1. Merge locally

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout master
git merge Datacleaning
git branch -d Datacleaning
git push origin

C4

C3

C1

C2

C1

C2

C3

Check out the master branch

Merge in the changes on the branch

Delete the branch (optional)

Push your master branch to the remote

git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"

Github repo master branch

My computer

C1

C2

C1

C2

Now there are two options for merging in the new changes:

1. Merge locally
2. Remotely with a pull request on GitHub

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout master
git merge Datacleaning
git branch -d Datacleaning
git push origin

C4

C3

C1

C2

C1

C2

C3

git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"

Github repo master branch

My computer

C1

C2

C1

C2

Now there are two options for merging in the new changes:

1. Merge locally
2. Remotely with a pull request on GitHub

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout master
git merge Datacleaning
git branch -d Datacleaning
git push origin
git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"
git push origin NewDataMerge

C4

C1

C2

C1

C2

C3

C1

C2

C4

Push your branch to GitHub

Then go to your GitHub repo

Github repo master branch

My computer

C1

C2

C1

C2

You should now see a button like this, noting that your branch has recent changes

 

 

Clicking this button takes you to the page for creating pull requests

 

Jared's computer

C1

C2

C1

C2

C1

C2

git checkout -b "Datacleaning"
git add dofile1.do
git commit -m "Add dofile1.do"
git checkout master
git merge Datacleaning
git branch -d Datacleaning
git push origin

C4

C1

C2

C1

C2

C3

C1

C2

C4

git checkout -b "NewDataMerge"
git add newfile.do
git commit -m "Add newfile.do"
git push origin NewDataMerge

Github repo master branch

My computer

C1

C2

C1

C2

The nice thing about pull requests is that your collaborators can review the code before you
merge it in to the master
 

You can add them as reviewers of the pull request:

C1

C2

C1

C2

C4

C1

C2

C1

C2

C3