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

Git crash course

Git crash course

Presentation made for GTI-350 students at ÉTS in Montréal.

Nicolas Brassard

September 24, 2014
Tweet

More Decks by Nicolas Brassard

Other Decks in Programming

Transcript

  1. GTI-350 git crash course Automne 2014 - Groupe 3 License

    MIT http://nitriques.mit-license.org/ © Nicolas Brassard 2014
  2. Pre-requisites 1. Really basic knowledge of the command line (cd,

    mkdir, rm/del) 1.1. bash/csh/zsh/*sh for *nix 1.2. cmd/power shell/cygwin for Windows 2. git installed and available in the PATH 3. git properly configured (more on that later) I assume your already know one SCM system...
  3. Summary 1. What’s up git? 2. Vocabulary 3. Creating a

    repository 4. Commiting 5. Status + Log 6. Branching + Merging + Tagging 7. Remotes + Cloning 8. Varia (Config, Aliases, Tools, etc.)
  4. What’s up git? git: because it was not already taken!

    By and for Linus Torvalds in 2005 Inspired by BitKeeper (proprietary) http://git-scm.com/
  5. git: Distributed SCM Almost every SCM systems are designed around

    a client-software architecture. git does not require a server: git does not ship with a server executable! ssh+git@..., http(s)://..., file://...
  6. git: Distributed SCM Centralized SCM uses commit ID as ordering:

    This creates a linear history. All new commits goes after the last one (if not, reject it).
  7. git: Distributed SCM Offline support ? Must be able to

    commit. Must be able to show all history.
  8. git: Distributed SCM How ? Everything is local. Do not

    use incremental IDs: use hash instead. (Easy to generate uniqueness across machines) Creates an history tree.
  9. git: Distributed SCM You can even build a forest! Each

    branch is a separate view (instances) of a tree.
  10. Vocabulary (as defined by me) Git is so special, it

    comes with it’s own jargon. git folder: .git directory at the root of the project. Holds everything git needs to keep track of (file oriented database). git command: git offers commands. it’s the first arguement in the CLI. (git help) Object: a record (file) in the database. Referenced by its hash (SHA-1) Blob: an object that contains data (text or binary). Tree: an object that represents a structure of blob(s) (name of the files). Commit: an object that links a new tree to a parent commit and a message.
  11. Vocabulary Index: “staging” area: things that are “staged” for commit.

    HEAD: Pointer to the current (often the last) commit the index points to. Branch: Pointer to the current tree on which the index operates. Master branch: The “default” or “main” branch, typically called master Feature branch: One of the best practice is to isolate changes into seperate branches, called feature branches. “Feature” can also mean bug fix here. Tag: Name/synonym for a commit (commit-ish ref git rev-parse). Anotated tag: shareable, persistant and signable tag.
  12. CLI: The best human-machine interface “For a lot of things,

    the CLI is still the best way for an expert user to interact with a computer.” - Me, now.
  13. Commiting The first thing to do is to add files

    to the index git add . # entire folder git add *.txt # all .txt files Then you can git commit
  14. Commiting Instead of returning to the prompt, an editor (vi/vim,

    emac, nano, notepad…) should come up… git needs a commit message. Commit title (80 chars max for Linus) Commit message on multiple lines
  15. Commiting One liners git commit *.txt # adds *.txt and

    commits git commit *.txt -m “!” # message included git commit -a # adds all “dirty” files
  16. Commiting But what about removing a change from the index

    ? git rm --cached path/to/file BEWARE, the --cached prevents the files from being deleted! --cached only affects the index
  17. Status + Log In order to check the index state,

    issue: git status # or git diff See the log of the current branch git log
  18. Branching git init created a branch called master... Creating a

    branch: git branch <branch-name> Switching to a branch: git checkout <branch-name>
  19. Branching git checkout is a tricky command: It can both

    act on branches and on files git checkout file.txt # reset the working copy # to the last commited version
  20. Merging Merge on-top or before ? Standard merge (recursive strategry:

    Find the closest common parent) git checkout <base-branch> git merge <feature-branch> # merges # feature-branch on top of base-branch
  21. Merging What happens if we git log now? Git may

    have made a merge commit! Why? git needs to create a new commit that will have two parents in order to unify things...
  22. Merging Doing the opposite: getting changes from master into feature

    branch. git checkout <feature-branch> git rebase <base-branch> # “inserts” all things # in base-branch before feature
  23. Merging Rebase == Rewriting history BEWARE! But commits are not

    lost, they are duplicated… That’s where blobs come in handy… (avoid duplication!)
  24. Merging Rebase == Rewriting history BEWARE #2! Never rebase master!

    This will make sharing things a lot harder!
  25. Merging Dealing with conflicts: 1. Manual edit, then git add|rm

    && git commit 2. Abort try other strategy (see git help merge) 3. Abort and try interactive rebase (git rebase -i <commit-ish>)
  26. Tagging git tag <my-tag> # creates a local tag #

    not very usefull… git tag -a <my-tag> # annotated tag! One liner: git tag -a <my-tag> -m “msg”
  27. Branching + Merging + Tagging git checkout master git checkout

    -b my-feature … git commit -a [-m “”] # x times git checkout master git merge my-feature git tag -a 1.0.1a -m ‘My feature’
  28. Remotes + Cloning In order to share code you need

    to add at least one remote to your your git repo. Remotes are “remotes working copies” They can be anywhere: on your friend’s laptop or on github/gilab/bitbucket/your own server
  29. Remotes By convention, the first repo is called ‘origin’ You

    can add as many remotes as you want! You can access read-only and read-writes remotes
  30. Remotes Let’s add a origin remote for our repo git

    remote add origin [email protected]: nitriques/git.git # ssh git remote add origin https://github. com/nitriques/git.git # https
  31. Remotes Fetch all the history git fetch <remote-name> git fetch

    <remote-name> <remote-branch> git fetch --all This does not modify the working copy!
  32. Remotes We need to merge with the remote. git merge

    <remote-name>/<branch-name> Let’s say we are in the master branch and want to update it to the remote’s version: git merge origin/master
  33. Remotes But there is a shortcut! git pull <remote-name> <branch-name>

    Space vs slash: The team that coded fetch won when they had to collaborate with the team responsible for merge...
  34. Remotes If you are lucky enough, you have write (push)

    acces to the repro, you can push to any branch (as long as it does not already exist) git push origin master # pushes the current # branch into origin/master
  35. Remotes If you rebased, you will need to do a

    forced update or else git with yell at you. git push <remote-name> <branch-name> --force Never do this on the master branch: git pull won’t work.
  36. Remotes + Branching + Merging + Tagging git checkout master

    git pull origin master # makes sure local master is sync git checkout -b my-feature # creates new feature branch … git commit -a [-m “”] # x times git push origin my-feature # create a pull request … or pull it yourself! git checkout master git pull origin my-feature git push origin master git tag -a 1.0.1a -m ‘My feature’ # optional git push --tags # this is how you push anotated tags
  37. Cloning git clone <repo-url> git clone [email protected]:nitriques/<repo-name>.git But cloning mostly

    is… mkdir <repo-name> cd <repo-name> git init git remote add origin <repo-url> git fetch origin git merge origin/master cd ..
  38. Config Initial (post-install) configuration git config --global user.name "YOUR NAME"

    git config --global user.email "YOUR EMAIL ADDRESS" + Auth https://help.github.com/articles/set-up-git
  39. Aliases git aliases are a great way to deal with

    git’s crazy CLI arguments… git lol... git config --global --add alias.lol "log --graph --decorate -- pretty=oneline --abbrev-commit" https://gist.github.com/nitriques/17117c251c30b72a8aff
  40. Tools gitk: git’s native UI, bundle with it ghwd: https://github.com/zeke/ghwd

    tortoisegit: https://code.google. com/p/tortoisegit/ dotfiles: the CLI is your friend, don’t let it suck http://dotfiles.github.io/ Google/StackOverflow: “git how to…”
  41. Fun fact The only “official” linux kernel repo resides on

    Linus personal computer. He pulls from 5 other guys he trusts. They trust other people who trusts other people. But Linus can always be sure that his copy is intact since everything is hashed.