Git - tips & tricks

Git - tips & tricks

Presentation prepared for Meet & Code event at the Silesian University of Technology organized on 20.10.2017

E8bb35e5d8c868e7dffcd9859248d6f1?s=128

Piotr Wittchen

October 21, 2017
Tweet

Transcript

  1. tips & tricks Piotr Wittchen wittchen.io github.com/pwittchen

  2. Why do we need to manage versions of the software?

    • Tracking project history • Searching for a change in the code • Revert changes, when something is broken or not desired in the upcoming release • Integrating work within a team • Verify changes before they will become part of the shipped product • Increase project quality
  3. How can we do it? ... v. 0.1 v. 0.2

    v. 0.3 v. 0.n Is there a better way?
  4. How about SVN? Central repository developer developer developer developer

  5. How about Git?

  6. A bit of history • Created in 2005 as VCS

    for maintaining Linux kernel • Created by Linus Torvalds (creator of Linux) • Its goal was to create distributed VCS (opposite idea to existing, centralized systems)
  7. Who is using Git? Almost everyone in the software industry!

  8. Are we still bothering about SVN? I don’t think so

  9. Git is distributed Version Control System Repository Server Repository Repository

    Repository developer developer developer commit changes push changes pull changes
  10. Can I use GUI tool for Git?

  11. Basic user configuration git config --global user.name "Piotr Wittchen" git

    config --global user.email piotr.wittchen@sap.com git config --global core.editor vim # to see configuration: vim ~/.gitconfig
  12. Creating the repository # let’s create a directory and go

    inside it mkdir test-repo cd test-repo # let’s initialize git repository git init Initialized empty Git repository in ~/test-repo/.git/
  13. Adding remote repository git remote add origin git@github.com:pwittchen/test-repo.git # to

    see remote repository url git remote -v # to remove remote repository (origin) git remote remove origin developer Repository Repository Server push pull commit
  14. Cloning the repository git clone git@github.com:pwittchen/test-repo.git Automatically adds remotes and

    creates local clone of the remote repository
  15. Inspecting repository git status # check current status git log

    # show whole repository log git reflog # show our local changes # pretty log with branches (you don’t have to memorize it ;-) git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all git log | grep message # search history git log --help # and more!
  16. Branches master b1 b2 b3 b3_1 b3_2 b2_1 b2_2 merge

    Switching to branch master git checkout master Creating branch b1 from master git checkout -b b1
  17. Typical workflow with Git developer master feature-1 commit commit commit

    review & tests merge developer We can extend our workflow with e.g. develop branch, epic branches, release branches, hot-fix branches, etc. depending on our project needs. See: https://www.atlassian.com/git/tutorials/comparing-workflows feature-2 commit merge review & tests
  18. Merging branches # switch to master git checkout master #

    merge feature-1 to master git merge feature-1 --no-ff # delete feature-1 locally git branch --delete feature-1 # delete feature-1 on remote git push origin --delete feature-1 developer master feature-1 merge
  19. Solving conflicts during merge An exception confirming the rule: this

    operation is easier from GUI tool (e.g. IntelliJ IDEA)
  20. Commit git commit -m “implemented feature-1” # or git commit

    # and edit commit message inside your favourite editor Remember about writing good and descriptive commit messages! It helps in browsing and analyzing log as well as keeping project history clean. Links: • https://chris.beams.io/posts/git-commit/ • https://robots.thoughtbot.com/5-useful-tips-for-a-better-commit-message • http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html • https://github.com/pwittchen/craplog
  21. Staging area # perform changes and move all of them

    to the staging area git add -A # next, perform a commit git commit -m “I’ve made changes” commit unstaged area staging area
  22. Reviewing our own changes before the commit # review changes

    before passing them to staging area git add -p # discard unstaged changes git checkout -- . # commit reviewed changes git commit -m “my beautiful, reviewed update”
  23. # rebase last 3 commits git rebase -i HEAD~3 #

    edit commits in your editor pick 740077e adding NOTES.md file squash 9099c9d Update README.md squash 1c9cbe4 Revert "Update README.md" # update commit message git commit --amend Rebase helps to keep history clean. Be very careful with rewriting history, because you can loose the code, when you do something wrong! Know what you do! Read more at: https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History Rewriting the history git rebase -i HEAD~3 git commit --amend squashed commits: 1, 2, 3 brand new commit message
  24. Push git commit -m “I’ve made a change” git push

    developer Repository Repository Server commit push
  25. Fetch and Pull # get changes from the remote repository

    git fetch # see changes between remote and local repository git diff origin/master # apply remote changes locally git merge git pull = git fetch + git merge developer Repository Repository Server pull
  26. Undoing things # discard uncommitted changes git reset --hard #

    undo last commit on the current branch git reset --hard HEAD~1 # undo last commit and move it into staging area git reset --soft HEAD~1 # create revert commit for a given change git revert 9099c9d
  27. Undoing things # discard uncommitted changes git reset --hard make

    changes without commit decide to discard local changes git reset --hard work from the scratch again
  28. Undoing things # undo last commit on the current branch

    git reset --hard HEAD~1 make changes decide to undo changes git reset --hard HEAD~1 work from the scratch again commit
  29. Undoing things # undo last commit and move it into

    staging area git reset --soft HEAD~1 make changes decide to undo changes git reset --soft HEAD~1 work from the scratch again commit staging area Note: if we already pushed changes to remote repository, We need to push changes with force next time (git push -f). Sometimes it may be blocked and not possible.
  30. Undoing things # create revert commit for a given change

    git revert 9099c9d make changes decide to undo changes git revert 9099c9d work from the scratch again commit 9099c9d revert commit
  31. Stash # make some changes # stash them git stash

    git stash list # apply stashed changes git stash apply # to clear stash list git stash clear # to drop one item git stash drop stash@{0} # to apply and drop git stash pop make changes without commit stop the work git stash work on something else git stash apply
  32. Cherry Pick git checkout master git cherry-pick 34a9541 a8dc3d1 31dae79

    34a9541 740077e 34a9541 branch-1 master git cherry-pick 34a9541
  33. Alises (we don’t have to memorize all the commands!) vim

    ~/.gitconfig [user] … [core] … [alias] list-aliases = !git config -l | grep alias | cut -c 7- | sort review-changes = add -p … An example: https://github.com/pwittchen/dotfiles/blob/master/.gitconfig
  34. GitHub, BitBucket and GitLab Websites with hosting for Git repositories

    and web UI: • https://github.com • https://bitbucket.org • https://gitlab.com They’re also open-source ecosystems and services with many integrations (e.g. CI servers) used by professionals, students and hobbyists. Most of them are free for open-source!
  35. Summary • When you write serious software, control it with

    Git! • Use Git from terminal • Learn about the commands before you use them • Try to experiment on a “dummy” project before using it in a serious project • Use Git Flow • Use aliases • Review your changes before the commit • Care about commit messages • Be careful and know what you do • If you don’t know how to do something, then read, learn, experiment and ask others for help or an explanation
  36. Useful resources Links and books: • https://git-scm.com/book/en/v2 (Pro Git -

    free e-book) • https://www.atlassian.com/git • https://github.com/open-source • https://www.slideshare.net/ktoso/effective-git • https://www.slideshare.net/ktoso/git-tak-po-prostu-sfi-version • http://wittchen.io/tags/git/
  37. tips & tricks Piotr Wittchen wittchen.io github.com/pwittchen Thank you for

    the attention! Questions? Slides from the presentation are published at: https://speakerdeck.com/pwittchen