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
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)
Git is distributed Version Control System Repository Server Repository Repository Repository developer developer developer commit changes push changes pull changes
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/
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
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
# 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
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
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
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
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
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.
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
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
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!
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
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