Share
Embed
Git - tips & tricks
Presentation prepared for Meet & Code event at the Silesian University of Technology organized on 20.10.2017
Piotr Wittchen
October 21, 2017
Transcript
37 slides
-
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 -
How can we do it? ... v. 0.1 v. 0.2
v. 0.3 v. 0.n Is there a better way? -
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 -
Basic user configuration git config --global user.name "Piotr Wittchen" git
config --global user.email [email protected] git config --global core.editor vim # to see configuration: vim ~/.gitconfig -
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/ -
Adding remote repository git remote add origin [email protected]: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 -
Cloning the repository git clone [email protected]:pwittchen/test-repo.git Automatically adds remotes and
creates local clone of the remote repository -
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! -
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 -
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 -
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 -
Solving conflicts during merge An exception confirming the rule: this
operation is easier from GUI tool (e.g. IntelliJ IDEA) -
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 -
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” -
# 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 -
Push git commit -m “I’ve made a change” git push
developer Repository Repository Server commit push -
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 -
Cherry Pick git checkout master git cherry-pick 34a9541 a8dc3d1 31dae79
34a9541 740077e 34a9541 branch-1 master git cherry-pick 34a9541 -
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 -
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 -
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/ -
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