Guest User

Untitled

a guest
Nov 21st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. ➜ ~/projects/trygit/[master]>cat trygit-tutorials/1-intro/tutorial.txt
  2. Try git!
  3.  
  4.  
  5. Let's get to it!
  6.  
  7. <run init-stuff.sh>
  8.  
  9. So, you've got a folder of important files. Type 'ls' to see what's going on there..
  10.  
  11. > cd stuff
  12. > ls
  13.  
  14. Notice this is more or less the same content, where you've made various backups and copies every time you change something major.
  15.  
  16. Over time, it gets a bit messy. You don't know which file is the newest, you don't know where you wrote that new chapter, and so on.
  17.  
  18. Historically, programmers have been especially prone to this kind of mess, and most of them have at some point come up with their own organized way of making copies and backups, to keep track of what code the put together at what time.
  19.  
  20. To deal with this very typical problem, source code management (SCM), or revision-control systems came into existence.
  21.  
  22. Now there are some very popular SCMs in use today, like CVS, Subversion and so on. The problem with these alternatives are that they require a lot of setup, a server, and generally a lot of infrastructure to get up and running. Frankly speaking, they are also pretty crappy in terms of functionality, compared to modern SCMs like Git.
  23.  
  24. Enough babbling. You want to use Git to take care of your stuff! In Git terms, you want to turn your 'stuff' folder into a _repository_. This is how:
  25.  
  26. > git init
  27.  
  28. Well done! You have now created a git repository. It is still empty though. Even though there are a bunch of files in the directory, none of them have physically entered the repository yet. To actually see this, type:
  29.  
  30. FGTD (Funky Git Term Definition): A git repository usually looks like a folder that contains a hidden folder '.git/' - inside of which all the history of your files are stored. You usually have one repository for each project you have going at the time. This could be a school-assignment, your life-long diary, or an open source project. It is generally a bad idea to put different things that don't belong together in the same repository. You can create as many as you like, so make a new one for each "thing" you're doing.
  31.  
  32. > git status
  33.  
  34. The result tells us that there are some "untracked" files here. Let's try "tracking" our essay. We'll start with the oldest version we have, and track that first. Let's say the one simply called "essay.txt" is the oldest:
  35.  
  36. > git add essay.txt
  37.  
  38. Now, let's check that status again:
  39.  
  40. > git status
  41.  
  42. Now git tells us that there is a new file to be committed. Let's do that:
  43.  
  44. > git commit -m "First version of the essay"
  45.  
  46. Congratulations! You are now a "committer".
  47.  
  48. The sentence after -m is our _commit message_. It's a note to our future self on why we made this change, and as you can imagine, these messages become pretty valuable over time when we're trying to remember why we did what.
  49.  
  50. FGTD: Although we're speaking of adding *files* to the repository, we are actually adding *changes*. This time the changes happens to be a new file, but later on, we will be adding changes in the shape of modifications to files that already exist.
  51.  
  52. Let's study what we just did a bit.
  53.  
  54. Entering files into the repository is a two-step process:
  55.  
  56. 1) Add them to the staging area, where you prepare which of your files will go into the repository
  57. 2) Commit the staging area into the repository.
  58.  
  59. FGTD: The staging area is more correctly called 'the index'. Although it might seem a bit of effort to *first* add stuff to the index, and *then* commit them, this turns out quite handy when you've made a lot of local changes, but you only want to commit a few of them in one go.
  60.  
  61. Commits are the main citizens of any SCM. The history of your project is made up of a chain of commits, all linked together. Our repository now has its first commit, so let's have a look at it:
  62.  
  63. > git show
  64.  
  65. Here you see all that went down in your latest commit:
  66.  
  67. * A cryptic looking series of 40 numbers and letters. This is the unique commit identifier, aka the SHA, or hash.
  68. * Name and email of the one who made the commit (you)
  69. * Date
  70. * Diff, which is a line-by-line breakdown of what was added and/or removed in this commit. In this case, we have only added one line. You can see it is added because it has a plus (+) sign in front of it. Removed lines are marked with a minus (-).
  71.  
  72. FGTD: You can actually use git show on a variety of things, like different commits, branches, tags, files, and so on. If you don't specify anything, it will implicitly show you the HEAD commit. This is a very important term in git. HEAD is like a symbolic flag that is always pointing at your latest commit.
  73.  
  74. So, we have tracked the first and original version of our essay. Now let's proceed to the next version. We'll overwrite the essay.txt with the file we happen to know was the next one we saved. Don't worry about accidentally losing old stuff in essay.txt, we can always retrieve it from history.
  75.  
  76. > mv essay-2011.10.12.txt essay.txt
  77.  
  78. Alright, now before we commit, let's see what changes we have:
  79.  
  80. > git status
  81.  
  82. The essay.txt has been changed, but not staged for commit. We'll fix that in a moment. Let's see what exactly has changed first:
  83.  
  84. > git diff
  85.  
  86. Ah, we have added a new line to the essay. That should be safe to commit.
  87.  
  88. > git add essay.txt
  89. > git commit -m "Second version, saved on 2011-10-12"
  90.  
  91. Well done! You can do another 'git show' to see that it worked as expected.
  92.  
  93. Onwards with the next version of the file:
  94.  
  95. > mv essay.new.txt essay.txt
  96. > git diff
  97.  
  98. Now a third line should be added.
  99.  
  100. This time, instead of staging essay.txt again with 'git add', we'll commit all modified files in one swoop with the -a parameter:
  101.  
  102. > git commit -a -m "Third version"
  103.  
  104. Now we only have one unversioned file left. This one contains a chapter our friend Bob contributed during the holidays. We can't quite remember when he wrote it, but let's move it onto the essay.txt file and look at the diff:
  105.  
  106. > mv essay.txt.bob essay.txt
  107. > git diff
  108.  
  109. It appears that the latest file version replaces the contents of chapter three. That means Bob has written a chapter 3, although we already had one. This isn't what we intended for him to do, but he probably based his work on an older copy of our essay.
  110.  
  111. Let's fix this. We could say that Bob "branched out" after we had written (or committed) the second chapter. We can express exactly this in the revision history using a git branch. We first create a branch called "bobs-chapter" based on the second version (the last version Bob and us have in common).
  112.  
  113. > git branch bobs-chapter HEAD~2
  114.  
  115. We will now switch to work on this branch, and commit Bob's work:
  116.  
  117. > git checkout bobs-chapter
  118.  
  119. [this is as far as I got]
Add Comment
Please, Sign In to add comment