Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Version Control All The Codes

Version Control All The Codes

Learn the basic concepts of of version control using git and Github.

Thanks to Tower (a Git GUI) I also handed out cheat sheets. Get a copy here:
http://www.git-tower.com/files/cheatsheet/Git_Cheat_Sheet_grey.pdf

Note: The message around what git-blame is and why it's awesome does not appear in this static version since it was improvised. Short version: Git-blame provides an annotated, line-by-line commit history for the file. For best results, click the "Blame" button when looking at any file on Github to unleash the awesomeness that is git-blame.

Tiffany Conroy

August 23, 2012
Tweet

More Decks by Tiffany Conroy

Other Decks in Programming

Transcript

  1. @theophani TIFFANY CONROY My name is Tiffany, and I work

    as an interaction designer and front-end developer at SoundCloud here in Berlin.
  2. USE VERSION CONTROL I'm going to convince you to use

    a formal version control system for everything you do. Proper version control is too easy and too valuable not to be using for everything. If you aren’t already using any version control and aren’t sure what you use,
  3. USE GIT use git. I’m not going to spend anytime

    trying to convince you. If you want to hear why, come talk afterwards.
  4. Have used some sort of versioning 3. Have used some

    sort of versioning. Hands up if you think I forgot to include anyone in this chart. You may think I forgot this group:
  5. Have used some sort of versioning Have never used any

    sort versioning people who have never used version control. But I don't think that anyone here has never used version control.
  6. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control.
  7. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  8. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  9. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  10. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  11. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  12. If you have ever done this: [used ⌘Z or CTRL-Z],

    then you have used version control. As we make revisions, we create a linear history of our changes.
  13. When we use UNDO, we back out of the changes

    we’ve made to get to another earlier state we preferred.
  14. When we use UNDO, we back out of the changes

    we’ve made to get to another earlier state we preferred.
  15. From that point in our past, we can move forward

    into a new, alternate future. But when we create the new future, we completely lose the edits we undid.
  16. From that point in our past, we can move forward

    into a new, alternate future. But when we create the new future, we completely lose the edits we undid.
  17. What if that forever-lost version turns out to have been

    better than the new thing you created? With very few exceptions, when using the UNDO function, we do not have access to the edit TREE. When we back out of a change this way, using UNDO, it's forever. But what makes the direct past more valuable than an alternate future? Nothing. But with UNDO, we are forced to choose before we know. So, this is a solved problem. We've all done this at some point:
  18. So, this is a solved problem. We've all done this

    at some point, which is just another form of version control. This happens when we reach a stage in our work where we want to make fundamental changes, like deleting huge sections, or re-writing sections, but we are afraid of losing our existing work. We want to make bold changes without risk.
  19. Version control gives you freedom to try new ideas because

    it liberates you from the fear of losing your work This is true if you are writing an essay, or poetry, or code or making art. EVERYTHING you do can benefit from using version control, not just code. You already rely on being able to go back in time to an earlier state. With version control, and it's far less work to get full control over what gets saved.
  20. TERMINOLOGY Having more than one competing version of the same

    thing is called having *branches*. Creating a new branch is called *branching*.
  21. A saved earlier version you feel you has enough value

    to keep is called a *tag*. A tag is a safe place you can go back to.
  22. A *commit* is a unit of change in your code.

    It's also a safe place you can go back to, but it doesn't have the same significance as a tag.
  23. WHEN SHOULD I USE VERSION CONTROL? The thing that might

    be holding you back from using version control is wondering when to use it. The problem with version control is that by the time you realize you want to take advantage of it, it's too late. Version control is like insurance: you have it all along for that moment of crisis when you need it. I'm going to show you situations that you have all experienced, and tell you how to use version control in each of them.
  24. ## This looks great! I should save a copy It

    may not occur to you to use version control until after you've started working on a project, and at some point think “This is great so far! I'd love to save a copy of this thing just like this because it's [working, beautiful, one complete part done]”.
  25. ## This looks great! I should save a copy And

    when that happens, you should create a repository, and save your first version into the newly created repository. ## Okay, now I have a new thing I want to save.
  26. ## Okay, now I have a new thing I want

    to save. If the new thing you want to save is definitely better than the previous thing you saved, then just save the new version with a description of the change. This is the best approach when you want to *replace* the previous version. Don't worry: you always *can* go back to the older version, but it might not always be easy. ## Oh, I'm not actually 100% sure this new thing is an improvement.
  27. ## Oh, I'm not actually 100% sure this new thing

    is an improvement. No problem! If you ever want to save a version that is sort of an experiment that you aren't totally committed to yet, create a BRANCH and then commit to that instead. Keep working in that manner, branching every time you aren't sure.
  28. ## I branched a while ago, but now I'm sure

    this is better. If you are working alone and in a linear order, then no problem. All the edits up to the point are all there. By convention, the "main" branch in git is the so-named "master" branch. If you prefer, you can merge your best branch back into master.
  29. ## I branched a while ago, but now I'm sure

    this is better. [If you prefer, you can merge your best branch back into master.]
  30. Let’s imagine this instead. You are working along, any think:

    ## Shit, the changes I made suck. I want to go back in time to an earlier version. This is when version control shines. THIS is why you are using version control.
  31. TO DELETE EDITS: git reset --hard If you completely don't

    want ANY of the changes that you made since the last commit, use "git reset --hard".
  32. Anything that is not committed when you do a hard

    reset will be gone. Usually, this is not what we really want to do. Usually we want to keep the version we created and try again with an older version.
  33. If you want to go back to an older version

    but KEEP all your edits, just in case,
  34. TO TRY AGAIN: Create a branch from an older commit,

    and work from there. In summary, TO TRY AGAIN: Create a branch from an older commit, and work from there.
  35. ## Which commit do I want to go back to?

    In order to be able to “go back” to an earlier commit, you need to be able to tell the commits apart from each other. Luckily, they have identifiers:
  36. b1df5f0 HEAD@{0}: commit: how to connect to the channel 643f106

    HEAD@{1}: commit: added timezone support 26cf918 HEAD@{2}: commit: links to more advice dd71a0e HEAD@{3}: commit: typo!! 641ac84 HEAD@{4}: commit: minor header style changes These descriptions are MUCH more useful. With a version control system, when you save a revision by making a so-called commit. With each commit you must describe the change in words and save that description along with the change itself. The description of the change is called the "commit message". A commit is the *change* between revisions plus the *description* of that change. Additionally, it includes the author of the change (you, in our discussion), when it was made. Each commit knows what its parent commit is. And the parent knows its parent. And so on. And so the collection of commits is a history of the project up to that point in time. Then you can read through the commit messages and know the history of the repository. In this way, you should not have to look at the changes directly in order to understand what was done, or why, or by whom.
  37. TO SEE DIFFERENCES: git diff [version1] [version2] Though you may

    need to look at the difference between two versions to know what is interesting. For that, we need the concept of a diff, short for difference, which is just that: a way of showing you the difference between two versions.
  38. ## I have some great ideas in two different branches

    that I want to combine. You can always merge one branch into another branch.
  39. new-styles new-text Example: say you have two branches, "new-styles" and

    "new-text". You want to combine these two versions. We call that “merging branches”. You always merge a branch INTO another branch.
  40. new-styles new-text So if you want to combine new-text and

    new styles, you can merge "new-styles" into "new- text"
  41. new-styles new-text You can also do the reverse: you can

    merge "new-text" into "new-styles".
  42. new-styles new-text … and a "new-styles" branch which is actually

    the original "new-styles" plus all the changes from "new-text". You can see how this might be confusing, for a couple reasons. First is the way the diagram looks. It’s not a straight line, but the combination of two branches.
  43. new-styles new-text which is why it is usually drawn more

    this. This version shows the commits from new-text branch (on the left) being applied “afterwards” (above) the ones in “new-styles”, but in parallel. The other reason it is confusing is because there is a branch called “new-styles” which is really a combination branch.
  44. new-styles new-text new-styles-and-text To avoid this sort of confusion, you

    could have first created a third branch called “new-styles- and-text” from “new-styles”
  45. new-styles new-text Let’s go back to just two branches again.

    You can also, if you decide both branches are great, merge BOTH into master.
  46. ## I want to access this repository from another machine

    You are working all nicey-nice on your work machine, and you realize you want to be able to keep working from your machine at home.
  47. How can you get your repository from one machine to

    the other? If you want two copies of the same repository on two machines, technically, you could share directly between them. Assuming you have a copy on two machines A and B:
  48. B A

  49. B A push to B B is remote from A

    From the perspective of A, B is "remote" and you "push" to it.
  50. B A A is remote from B pull from A

    From the perspective of B, A is "remote" and you "pull" from it.
  51. B A C In practice, everyone puts a third copy

    somewhere and then keeps both machine A and machine B in sync with the other copy. That third copy is kept somewhere that is easy to access using any internet connection.
  52. B A C From machine A, you push to C,

    and then on machine B you pull from C.
  53. B A C Later, if you make changes on machine

    B, you pushes changes back to C, and pull onto A.
  54. B A C “origin” By convention, that third remote location

    is usually given the name "origin" regardless of which machine had the original version of the repository on it. This convention can be confusing for beginners, so you should be aware of it. So, the best way to share between two copies is to actually have a third copy somewhere on the publicly accessible internet.
  55. B A Github Enter Github. Github is a lot of

    things, but at its core, Github is a place on the internet where you can store copies of your git repository so that you (or anyone else you allow) can keep in sync with.
  56. Github is free and has a free GUI application for

    Mac that you can use to manage all your git repositories on Github. With a free Github account, all your repositories are public, which means that anyone can see them and so of course can get a copy. If you want to have private repositories, then you need a paid account.
  57. B A So now that we have introduced the idea

    of sharing a repository across two (or more!) machines, we can also address another situation: ## My friend is going to help me on my project, and we to work in the same repository.
  58. your friend you If you friend also has a Github

    account, this is dead easy: Give your friend edit access on the repository (you can do this from the admin area of the repository on Github). Your friend then "clones" the repository, and after making changes "pushes" back to Github. If you make changes, you also push to Github. Anytime you want to get back in sync, you pull instead of push. Sometimes, you must pull before you push, because your version is an older version that the version on Github.
  59. back-end front-end ## I did some work in a branch

    called "front-end" and my friend did work in a branch called "back-end". We want all the changes to be in the master branch.
  60. YOUR EDITS YOUR FRIEND’S EDITS ## Blarg. We both made

    changes on the same file. [describe the picture]
  61. YOUR EDITS YOUR FRIEND’S EDITS RESULT + = git is

    really good at understanding that both of sets of changes on a single file are wanted, especially if the changes are on different lines. Try merging both, since it may work right away. IF those changes are on the same line in the file, or for some other reason git is confused, git will combine all the changes, and warn you that there was a conflict.
  62. Content that was merged without a problem before the conflict.

    <<<<<<< HEAD [The edits from the branch you merged into are in this space] ======= [The edits from the branch you merged in are in this space] >>>>>>> incoming-branch Content that was merged without a problem after the conflict. Then you can open the files and manually delete the unwanted changes. This is how that looks.
  63. Content that was merged without a problem before the conflict.

    <<<<<<< HEAD [The edits from the branch you merged into are in this space] ======= [The edits from the branch you merged in are in this space] >>>>>>> incoming-branch Content that was merged without a problem after the conflict. What are seeing is the combined edits, plus some helper text that git has added so you can easily find the conflict and correct it. The helper text is the two rows of arrows and the double line separating the two sections.
  64. Content that was merged without a problem before the conflict.

    The edits from the branch you merged into and from the one you merged in are now both in this space. And we delete the helper text extra things git added Content that was merged without a problem after the conflict. So you make updates, and remove the helper text. Then we commit this change and complete the merge.
  65. Now we know enough to ask the most important question

    that we can ask ourselves that version control can answer: