Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

How can we do it? ... v. 0.1 v. 0.2 v. 0.3 v. 0.n Is there a better way?

Slide 4

Slide 4 text

How about SVN? Central repository developer developer developer developer

Slide 5

Slide 5 text

How about Git?

Slide 6

Slide 6 text

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)

Slide 7

Slide 7 text

Who is using Git? Almost everyone in the software industry!

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Git is distributed Version Control System Repository Server Repository Repository Repository developer developer developer commit changes push changes pull changes

Slide 10

Slide 10 text

Can I use GUI tool for Git?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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/

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Cloning the repository git clone git@github.com:pwittchen/test-repo.git Automatically adds remotes and creates local clone of the remote repository

Slide 15

Slide 15 text

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!

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Solving conflicts during merge An exception confirming the rule: this operation is easier from GUI tool (e.g. IntelliJ IDEA)

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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”

Slide 23

Slide 23 text

# 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

Slide 24

Slide 24 text

Push git commit -m “I’ve made a change” git push developer Repository Repository Server commit push

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Cherry Pick git checkout master git cherry-pick 34a9541 a8dc3d1 31dae79 34a9541 740077e 34a9541 branch-1 master git cherry-pick 34a9541

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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!

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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/

Slide 37

Slide 37 text

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