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

Understanding Git

Understanding Git

An unconventional Git workshop starts with thinking in "patch."

Zhihao Yuan

February 15, 2018
Tweet

More Decks by Zhihao Yuan

Other Decks in Programming

Transcript

  1. Feb 6, 2018 Zhihao Yuan <[email protected]> Tanu Malik <[email protected]> School

    of Computing College of Computing and Digital Media Understanding Git
  2. To teach Git as a "better" SVN To teach Git

    as a version control system To teach Git rebase workflow There have been lots of Git tutorials…
  3. Before we start Update your .gitconfig with some aliases which

    will be used during this workshop: https://goo.gl/Bgs5rN git clone https://github.com/lichray/nvi2/
  4. git lg (customized log) git checkout git show git init

    (create repo) git status git add (stage file) git commit Basic Git
  5. What is a patch A patch is a file, such

    that given a file A and a file B, a patch represents "changes" between A and B Let's consider A: common.h, B: common.h.new To generate a patch diff -u common.h common.h.new
  6. + Added Lines '+++' = new file Line numbers '---'

    = old file An example of patch file - Deleted Lines <No symbol> Untouched Lines
  7. Some styles for file naming in patch  --- v2.0.29/prog/README

    Mon Mar 10 15:13:12 1997 +++ v2.0.30/prog/README Mon Mar 17 14:58:22 1997  --- www/palemoon/distinfo (revision 454992) +++ www/palemoon/distinfo (working copy)  --- media/libcubeb/src/cubeb_sndio.c.orig 2017-01-12 17:53:15 UTC +++ media/libcubeb/src/cubeb_sndio.c  --- a/common/conv.c +++ b/common/conv.c
  8. -p When you see the specified relative path under the

    current working directory, patch -p0 < some.patch When you want to strip the first component from the specified path, patch -p1 < some.patch
  9. Examples  --- v2.0.29/prog/README Mon Mar 10 15:13:12 1997 +++

    v2.0.30/prog/README Mon Mar 17 14:58:22 1997  --- www/palemoon/distinfo (revision 454992) +++ www/palemoon/distinfo (working copy)  --- media/libcubeb/src/cubeb_sndio.c.orig 2017-01-12 17:53:15 UTC +++ media/libcubeb/src/cubeb_sndio.c  --- a/common/conv.c +++ b/common/conv.c -p1 -p0
  10. Play with Git patches git show (commit as a patch)

    git diff (difference between commits as a patch) git apply -R git apply patch -p1 < some_git_style_patch.diff
  11. Why do we need Git? What does this patch do?

    What are these numbers for? Where do I save this patch set? Can I evolve a software with just patches?
  12. Problems Patches need to be documented Patches often need to

    be applied in order You don't want to save a patch set along with source code
  13. In Git, can I apply two patches in a different

    order? Exchange? An output of git lg
  14. In Git, can I apply two patches in a different

    order? Very often, you can git checkout -b new_branch # suggested git rebase -i 328d2f5 # before the range Manually exchange the two commits in editor
  15. Additional operations while rebasing in an editor Merge two patches

    into one (fixup) Edit the commit message (reward) Edit the patch itself (edit) Delete a patch (drop) Same thing
  16. Can I apply two patches in a different order? When

    you cannot, there is git mergetool
  17. Play with Git branches git branch (backup) git checkout -b

    (new branch) git checkout git push -u, git push
  18. "How do I merge a branch into master?" Stop, you

    are doing it wrong Remember, patch set
  19. Methodology to take Turn branch into a superset of branch

    git checkout git rebase -i git checkout git merge # now in "fast-forward" mode
  20.  to view: git diff HEAD  to unstage: git

    discard  git commit Staged changes  to view: git diff  to stage: git add / add -u  to drop: "git checkout ." Unstaged changes A patch not yet committed
  21. Frequently needed tasks Change last commit's message git commit --amend

    Fold the unstaged changes into the last commit git add -u git fix Checkout a branch with uncommitted changes
  22. git stash (push) git stash pop git stash drop git

    stash show -p (as a patch) A stack of uncommitted patches Introducing git stash