I Don’t Git It: A beginner’s guide to git Presented by Mathew Robinson @chasinglogic
Who Am I Mathew Robinson DevOps Engineer for The Kroger Co. Linux/FOSS Enthusiast for 13 years Documentation Connoisseur Developer
So you want to contribute to OSS So how many people in here have wanted to contribute to FOSS but have never gotten around to learning how?
But where do I start?
Git is the most ubiquitous tool in Open Source
What is Git? It's a Distributed Version Control System
A repository is a collection of commits, like a truck full of boxes
Create a repository with ‘git init’, like buying a new truck without any boxes * using git will not improve your photo editing skills
Get someone else’s repository using ‘git clone’, like buying a copy of their truck with the same boxes Mine Yours
Commits are the boxes in the truck
Changes or “edits” are stored in the commits
Additionally the commits have messages that describe the changes inside Update .gitignore
When working on your own repo, to make a commit first you’ll need to make some changes
Then you’ll need to use ‘git add’ to put those changes in the “staging area” $ git add MANIFEST.in
From there ‘git commit’ will take all changes in the staging area and put them into a commit $ git commit -m “update MANIFEST.in”
You now have added a commit to your repository! update MANIFEST.in
But now you have a commit that the remote repository does not Yours Mine update MANIFEST.in
And it’s likely the remote repository has commits that you do not Yours Mine update MANIFEST.in Added new test
First you must ‘git pull’ to accept a delivery from the remote of the commits you don’t have $ git pull origin master Yours Mine Added new test This is merging the commits, that’s why it’s called a pull request and not a merge request update MANIFEST.in Added new test
Now you can ‘git push’ to send a delivery of your commits to the remote repository $ git push origin master Yours Mine Added new test update MANIFEST.in update MANIFEST.in Added new test
Now what if the remote had a commit which contains changes on the same file we changed? Mine Yours
This causes a merge conflict because git does not know who’s version is right Mine Yours
To solve this problem, we can use branching Mine Yours
A branch takes a “snapshot” of the repo’s current HEAD (latest) commit $ git checkout -b update-manifest Your freshly cloned repo Branch: update-manifest If this looks like cloning it should! While branching is a similar operation there is a reason that we do the branching Someone else’s change Someone else’s change
Now you can apply your change on your branch Branch: master Branch: update-manifest update MANIFEST.in Someone else’s change Someone else’s change
Now when the remote is updated Remote Branch: origin/master Branch: master Clean up MANIFEST.in Someone else’s change Someone else’s change
We can pull those changes into our copy of the master branch $ git checkout master Remote Branch: origin/master Branch: master Clean up MANIFEST.in Someone else’s change Someone else’s change
We can pull those changes into our copy of the master branch $ git pull origin master Remote Branch: origin/master Branch: master Clean up MANIFEST.in Clean up MANIFEST.in Someone else’s change Someone else’s change
Now we can merge or rebase as necessary $ git checkout update-manifest $ git merge master # or $ git rebase -i master Branch: master Branch: update-manifest update MANIFEST.in Clean up MANIFEST.in Clean up MANIFEST.in Someone else’s change Someone else’s change
Now when we merge back to master it will fast-forward avoiding all merge conflicts $ git checkout master $ git merge update-manifest Branch: master Branch: update-manifest update MANIFEST.in update MANIFEST.in Clean up MANIFEST.in Clean up MANIFEST.in Someone else’s change Someone else’s change
The same is true when we push our master branch back up to the remote $ git push origin master Remote Branch: origin/master Branch: master update MANIFEST.in update MANIFEST.in Clean up MANIFEST.in Clean up MANIFEST.in Someone else’s change Someone else’s change
What we’ve covered git init git clone git add git commit git checkout git branch git merge && git rebase git push && git pull
$ git status Questions? You can find me on Twitter: @chasinglogic Github: @chasinglogic mrobinson@praelatus.io