Advertisement
Guest User

Untitled

a guest
Jul 12th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.56 KB | None | 0 0
  1. # Make room for this example
  2. if [[ -d /tmp/git-example ]]; then
  3. rm -rf /tmp/git-example
  4. fi
  5.  
  6.  
  7. # Start Here
  8.  
  9. mkdir -p /tmp/git-example
  10. cd /tmp/git-example
  11.  
  12. # This will be the equivalent to the user's fork on github
  13. # This will be the first user
  14. mkdir user0
  15. cd user0 && git init && cd ..
  16. USER0="user0 <user0@localhost>"
  17. # This will be the second user
  18. mkdir user1
  19. cd user1 && git init && cd ..
  20. USER1="user1 <user1@localhost>"
  21.  
  22. # Now we need to make two repo's that will act as the github equivalent to a local user's fork
  23.  
  24. mkdir user0-repo
  25. cd user0-repo && git init --bare && cd ..
  26.  
  27. mkdir user1-repo
  28. cd user1-repo && git init --bare && cd ..
  29.  
  30. # Make another repo to represent the github ooi repository
  31. mkdir upstream
  32. cd upstream && git init --bare && cd ..
  33.  
  34. # user0 will initialize the project with a README
  35. cd user0
  36. cat << EOF > README.md
  37. An empty README file, this is only used for
  38. example purposes.
  39. EOF
  40. git add README.md
  41. git commit -m "Initial Commit with README" --author $USER0
  42. # Add the origin (user fork)
  43. git remote add origin /tmp/git-example/user0-repo
  44.  
  45. # Add the upstream (ooici repo)
  46. git remote add upstream /tmp/git-example/upstream
  47.  
  48. # Initialize the repo by making a new branch "master"
  49. git push origin master
  50. # Initialize upstream by making a new branch "master"
  51. git push upstream master
  52.  
  53. # Make a new branch REL2.0
  54. git checkout -B REL2.0
  55.  
  56. # Push the branch to the two repos
  57. git push origin REL2.0
  58. git push upstream REL2.0
  59.  
  60. # Back to the master branch
  61. git checkout master
  62.  
  63. #--------------------------------------------------------------------------------
  64. # user0 is going to do some standard work and push to the repos directly
  65. # this is not recommended but just shows what happens
  66. #--------------------------------------------------------------------------------
  67.  
  68. cat << EOF > hello.py
  69. def hello():
  70. print "Hello, World!"
  71. if __name__ == '__main__':
  72. hello()
  73. EOF
  74.  
  75. # git-add saves the changes or adds the new file to the git-filesystem. Git objects are whole files
  76. # (not diffs, contrary to popular belief) and the name of the file is a SHA1 hash of the file contents and a unique
  77. # string to git
  78. git add hello.py
  79.  
  80. # If we run git-status we can see that the file is "staged" to be committed. Staged files are git-objects that are new
  81. # to the git file-system and are flagged to be saved if the changes are committed.
  82. git status
  83.  
  84. #------------- OUTPUT ---------------------------------------------------------
  85. #
  86. # On branch master
  87. # Changes to be committed:
  88. # (use "git reset HEAD <file>..." to unstage)
  89. #
  90. # new file: hello.py
  91. #
  92. #--------------------------------------------------------------------------------
  93.  
  94. # We can save our work thus far by committing it, a commit is the sha1 hash of a git tree-object which contains a list
  95. # of the git-objects it points at. We can look at our current tree's files with the git cat-file command
  96.  
  97. git cat-file -p master\^{tree}
  98.  
  99. #------------- OUTPUT ---------------------------------------------------------
  100. # 100644 blob d2617a67f7fd50e85356270658ac6531f7d24bb5 README.md
  101. #--------------------------------------------------------------------------------
  102.  
  103. # Now we commit our work which generates a new tree object and the commit sha is the SHA1 hash of that tree object that
  104. # now has our new file in it.
  105.  
  106. git commit -m "Added hello.py" --author $USER0
  107.  
  108.  
  109. #------------- OUTPUT ---------------------------------------------------------
  110. #[master 4738aa0] Added hello.py
  111. # 1 file changed, 4 insertions(+)
  112. # create mode 100644 hello.py
  113. #--------------------------------------------------------------------------------
  114.  
  115. # Now let's take a look at our tree
  116. git cat-file -p master\^{tree}
  117.  
  118. #------------- OUTPUT ---------------------------------------------------------
  119. # 100644 blob d2617a67f7fd50e85356270658ac6531f7d24bb5 README.md
  120. # 100644 blob f714a668e17b09bf82809a5b4b3e663d0b250b5c hello.py
  121. #--------------------------------------------------------------------------------
  122.  
  123. # A commit object contains the previous commit sha, the tree sha and some data about the commit itself, the author, an
  124. # email, date time and a message about the commit.
  125.  
  126. COMMIT=$(git rev-parse HEAD) # It's 4738aa0 for me but will be different for you since your email and timestamp differs
  127. git cat-file -p $COMMIT
  128.  
  129. #------------- OUTPUT ---------------------------------------------------------
  130. # tree 379876d0691c6d83c4e6ff7e89e432abcd159db9
  131. # parent 671ce2e444ee1c54728a2957dce00dacbe672874
  132. # author Luke Campbell <luke.s.campbell@gmail.com> 1380031913 -0400
  133. # committer Luke Campbell <luke.s.campbell@gmail.com> 1380031913 -0400
  134. #
  135. # Added hello.py
  136. #--------------------------------------------------------------------------------
  137.  
  138. # Now to push our local changes to our repositories
  139.  
  140. git push origin master
  141.  
  142. #------------- OUTPUT ---------------------------------------------------------
  143. # Counting objects: 4, done.
  144. # Delta compression using up to 8 threads.
  145. # Compressing objects: 100% (3/3), done.
  146. # Writing objects: 100% (3/3), 349 bytes, done.
  147. # Total 3 (delta 0), reused 0 (delta 0)
  148. # To /tmp/git-example/repo
  149. # 671ce2e..4738aa0 master -> master
  150. #--------------------------------------------------------------------------------
  151.  
  152. # Our current branch "master" which is a named symbol that points to the commit 4738aa0 has now been "pushed" to the
  153. # origin repository. So now inside origin, its version of master is identical to user0's.
  154.  
  155. # Normally, we would make a pull request here but since user0 believes he owns the upstream as well, he'll go ahead and
  156. # push directly to upstream without using a pull request.
  157.  
  158. git push upstream master
  159.  
  160. # Now, it's time for user1 to do some work
  161.  
  162. cd ../user1
  163.  
  164. # user1 needs to grab the latest changes
  165. # the first thing user1 needs to do is make a remote, something that tells git where to look
  166.  
  167. git remote add origin /tmp/git-example/user1-repo
  168. git remote add upstream /tmp/git-example/upstream
  169.  
  170. # Now, to grab the latest changes
  171.  
  172. # This pull command with the --ff-only tells git to download the commit history from upstream for the branch named
  173. # "master", then to fast-forward the current HEAD pointer to the latest commit in that branch. If it's impossible to
  174. # traverse the commit tree to reach the latest commit then the trees are disjoint and there is a problem.
  175. git pull --ff-only upstream master
  176.  
  177. #--------------------------------------------------------------------------------
  178. # remote: Counting objects: 6, done.
  179. # remote: Compressing objects: 100% (5/5), done.
  180. # remote: Total 6 (delta 0), reused 0 (delta 0)
  181. # Unpacking objects: 100% (6/6), done.
  182. # From /tmp/git-example/upstream
  183. # * branch master -> FETCH_HEAD
  184. #--------------------------------------------------------------------------------
  185.  
  186.  
  187.  
  188. cat << EOF > work
  189. This is where work goes
  190. EOF
  191.  
  192. git add work
  193. git commit -m "Adds more work" --author $USER1
  194.  
  195. git push origin master
  196. cd ..
  197.  
  198. # Now user1 has to submit a pull request, I can't demonstrate a pull request from the command line but user0 gets an
  199. # email or a notification that user1 has submitted a pull request, he or she reviews the pull request and determines
  200. # that the work needs to be merged.
  201.  
  202. cd user0
  203. git checkout master # Ensure that I'm on the master branch
  204. # The syntax for git-pull is
  205. # git pull <repo> <branch>
  206. git pull /tmp/git-example/user1 master
  207.  
  208. # If I look at the log, I can see that the changes from user1's work were merged in:
  209.  
  210. git log --graph --oneline
  211.  
  212. # * a5ae7e9 Adds more work
  213. # * ee89100 Added hello.py
  214. # * 92495f8 Initial Commit with README
  215.  
  216. cd ..
  217.  
  218. # Now, let's see how collaborative and concurrent work looks.
  219.  
  220. # user0 is going to work on a file concur0 while user1 is going to work on concur1 concurrently
  221.  
  222. cd user0
  223.  
  224. # Check out a branch for this feature
  225. git checkout -b concurrent-work
  226.  
  227. cat << EOF > concur0
  228. Some concurrent work goes here
  229. EOF
  230.  
  231. git add concur0
  232. git commit -m "User0 concurrent work" --author $USER0
  233. git push origin concurrent-work
  234. cd ..
  235.  
  236. # user0 now submits a pull-request from his github account concurrent-work:upstream/master
  237.  
  238.  
  239. cd user1
  240.  
  241. git checkout -b more-work
  242. cat << EOF > concur1
  243. Some other concurrent work goes here
  244. EOF
  245.  
  246. git add concur1
  247. git commit -m "User1 concurrent work" --author $USER1
  248. git push origin more-work
  249. cd ..
  250.  
  251. # user1 submits a pull request for review and merging from his github-account more-work:upstream/master
  252.  
  253. # user0 will review and merge the pull-requests
  254.  
  255. cd user0
  256. git checkout master
  257. git pull /tmp/git-example/user1-repo more-work
  258. git push upstream master
  259.  
  260. git pull /tmp/git-example/user0-repo concurrent-work
  261. git push upstream master
  262.  
  263. # Now let's look at the log
  264. git log --oneline --graph
  265.  
  266. #--------------------------------------------------------------------------------
  267. # * bf7c52e Merge branch 'concurrent-work' of /tmp/git-example/user0-repo
  268. # |\
  269. # | * d2176fd User0 concurrent work
  270. # * | 1f3e488 User1 concurrent work
  271. # |/
  272. # * a08c6b8 Adds more work
  273. # * ad7be0f Added hello.py
  274. # * a7aadf8 Initial Commit with README
  275. #--------------------------------------------------------------------------------
  276.  
  277. cd ..
  278.  
  279. # Now let's work with Rebasing
  280. cd user1
  281.  
  282. git checkout -b rebase-work
  283.  
  284. cat << EOF >> work
  285. More work goes inside here
  286. EOF
  287.  
  288. git add work
  289. git commit -m "Updates work file" --author $USER1
  290.  
  291. # We'll do some more work
  292. cat << EOF > rebase-file
  293. Some text
  294. EOF
  295.  
  296. git add rebase-file
  297.  
  298. git commit -m "Adds new file rebase-file" --author $USER1
  299.  
  300. # Our commit history now looks like
  301.  
  302. # * 974ca2d Adds new file rebase-file
  303. # * 08b26d4 Updates work file
  304. # * 363fb9e User1 concurrent work
  305. # * e5f0f90 Adds more work
  306. # * f21e055 Added hello.py
  307. # * b525af5 Initial Commit with README
  308.  
  309. # If we were to make a pull request as it is, it would end up as a merge commit and it couldn't be cleanly integrated
  310. # into the master tree. If we rebased this history so that all of our new work was "based on" the upstream/master's tree
  311. # then it would be an absolutely clean integration without the need of a merge commit and the tree wouldn't branch.
  312.  
  313.  
  314. git fetch upstream # updates the local cache of the upstream-repo's information
  315. git rebase -p upstream/master
  316.  
  317. # Our commit history now looks like
  318.  
  319. # * 5dca673 Adds new file rebase-file
  320. # * 41b9da6 Updates work file
  321. # * b433b18 Merge branch 'concurrent-work' of /tmp/git-example/user0-repo
  322. # |\
  323. # | * 3ae59b9 User0 concurrent work
  324. # * | 828b36d User1 concurrent work
  325. # |/
  326. # * f99f07a Adds more work
  327. # * a6917fd Added hello.py
  328. # * 396382f Initial Commit with README
  329.  
  330. # Our current work, starting at "Updates work file" is now directly on top of the upstream/master's tree.
  331.  
  332. git push origin rebase-work
  333. cd ..
  334. # user1 now submits a pull request for this work, user0 receives the pull request and pulls it in
  335.  
  336. cd user0
  337. # Update our current understanding of upstream
  338. git fetch upstream
  339. # Checkout the raw tree upstream/master
  340. git checkout upstream/master
  341. # Name this commit "pulling"
  342. git checkout -B pulling
  343. git pull /tmp/git-example/user1 rebase-work
  344.  
  345. # Output:
  346. # Updating 2934f21..8126db2
  347. # Fast-forward
  348. # rebase-file | 1 +
  349. # work | 1 +
  350. # 2 files changed, 2 insertions(+), 0 deletions(-)
  351. # create mode 100644 rebase-file
  352.  
  353.  
  354. # Update the upstream repo with these changes
  355. git push upstream pulling:master # This pushes our local pulling branch to the master branch on upstream
  356.  
  357. # Let's take a look at the new history or tree of the upstream/master tree, it shouuld be clean, it doesn't split, it
  358. # didn't merge the rebase-work branch from user1 and linear.
  359. git fetch upstream
  360. git log --oneline --graph upstream/master
  361.  
  362. # * 4cf5c2d Adds new file rebase-file
  363. # * f91dab8 Updates work file
  364. # * 6eaf9df Merge branch 'concurrent-work' of /tmp/git-example/user0-repo
  365. # |\
  366. # | * 25d1d09 User0 concurrent work
  367. # * | 08de451 User1 concurrent work
  368. # |/
  369. # * afd8b73 Adds more work
  370. # * 7a57979 Added hello.py
  371. # * 079e069 Initial Commit with README
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement