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

Getting Good with Git

Getting Good with Git

Francis Addai

February 24, 2018
Tweet

More Decks by Francis Addai

Other Decks in Programming

Transcript

  1. About me Passionate about mentoring and introducing young software engineers

    to good engineering practices. • Technology Teaching Fellow @MESTAfrica • @PythonGhana Convener • Community Admin @DevCongress Follow me on Twitter @faddai
  2. In the next 30min • What is a version control

    system? • Why should you care about it? • Git me if you can • Git me some use cases
  3. Why should I care? • Collaboration. Come on, you’re not

    a lone wolf anymore. • Even if you are, having a copy of your code away from your laptop is wise. • Freedom to try out your wild ideas without messing up
  4. Git

  5. What is Git? Git is a distributed version control system

    for tracking changes in computer files and coordinating work on those files among multiple people
  6. • Working directory • Working tree • Staging area (index)

    • Repository (local, remote) • Branch (master, feature) Let’s familiarize ourselves with Git lingo
  7. Let git know who you are A rookie mistake is

    to use git without adding your name and email address. Own your commits. $ git config --global user.email "[email protected]" $ git config --global user.name "Francis Addai"
  8. Activity 1 Let’s practice the commands we’ve learned You need

    to have a recent version of Git installed. https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
  9. Git me some more commands • branch • checkout •

    merge • clone • push • pull • revert • rebase • reflog • rm • reset
  10. Git branches • git branch • git branch <branchName> •

    git checkout <branchName> • git checkout -b <branchName> # git branch <branchName> + git checkout <branchName> • git branch -d <branchName> / git branch -D <branchName> • git branch -m <oldBranchName> <newBranchName> Let’s create a branch, add some changes and merge those changes back into master.
  11. Discard changes in the working directory $ git checkout file.txt

    You can actually undo or get rid of changes you have made to a staged file before committing.
  12. Undoing your last commit $ git reset HEAD~1 --soft If

    you’re not interested in the changes $ git reset HEAD~1 --hard
  13. You need more commitment in life Commit code as often

    as possible and if you end up with incoherent commits, make them cohesive by squashing commits using rebase. http://bit.ly/2HInPHI If you are the kind of person that dreads committing often, rebase to the rescue! Don’t think about rebase as an option once your code has been pushed to a remote repository.
  14. What if you make a destructive mistake? # find the

    commit before things went haywire $ git reflog # assuming we identified 891bef as the commit of interest $ git checkout -b 891bef <branchName> Work on the changes you need to make and merge the branch back into your main branch.
  15. More interesting things to explore • Bare repository (host your

    git repo on Dropbox :) ) ▪ git init --bare ▪ no working tree, you can't checkout here • Hooks ▪ enforce adherence to style guides ▪ enforce guidelines for pushing code (all commits must have a reference to an issue # for example) • Submodules • Aliases • Gitflow