Download presentation
Presentation is loading. Please wait.
Published byRobert Ross Modified over 9 years ago
1
Falcons Git Usage Andre Pool Version 2.0 October 2015 / Veldhoven
2
Contents Why git Git basics Rules Part 1: Example add/commit/push/pull Part 2: Example branch/merge Part 3: Undo Way of working
3
Why git Performance State before merge from remote is stored Non blocking workflow No network connection needed to continue – Distributed version control system No tampering possible / database assurance Local commits are managed as small branches Easy to mere Easy to compare everything with everything – workspace, branch, version,..
4
Git basics Lots of options / Overwhelming Steep learning curve Fortunately you are up and running with only 16 commands Start simple Skip explaining underlying git database structure (interesting but not essential to know for day to day usage)
5
4 stages Working directory – sandbox Index = stage – proposed next – commit snapshot HEAD – last commit snapshot, – Local repository Remote – e.g. shared remote – repository
6
Most commonly used git commands 1 of 2 addAdd file contents to the index branchList, create, or delete branches checkoutCheckout a branch or paths to the working tree cloneClone a repository into a new directory commitRecord changes to the repository diffShow changes between commits, commit and working tree, etc. fetchDownload objects and refs from another repository logShow commit logs
7
Most commonly used git commands 2 of 2 mergeJoin two or more development histories together mvMove or rename a file, a directory, or a symlink pullFetch from and merge with another repository or a local branch pushUpdate remote refs along with associated objects rebaseForward-port local commits to the updated upstream head resetReset current HEAD to the specified state rmRemove files from the working view and from the index statusShow the working tree status tagCreate, list, delete or verify a tag object signed with GPG
8
DVCS Every working directory is a complete repository Commit on this local repository (fast) Be careful with large files (will be around in every working directory for ever for every different version) Do not forget to synchronize with a shared location before deleting working directory (a commit stays in the working directory)
9
Rules No large and or generated files in code repository! Clean code repository: – High performance – Fast clone – Limited disk space required for each project (clone) If required put large files in data repository git rm myLargeFile Removes the file from the workspace, but NOT from the repository!
10
Setup: ~./gitconfig file [user] name = John Doo email = john.doo@gmail.com [alias] coa = commit -a -m st = status graph = log --graph --abbrev-commit --pretty=oneline logp = log --abbrev-commit --pretty=oneline logf = log --follow
11
Part 1 The basics
12
Git Basic Example add/commit/push/pull Basic commands which cover the day to day usage Create repository User Barry does “things” User John does “things” Conflict Solve conflict GUI view
13
Basic Example Create (shared/central) repository git init --bare ~/tmp/my_rep.git Initialized empty shared Git repository in /home/apool/tmp/my_rep.git/ That’s all --barerepository without “working directory” Note1: the option “--shared” can be used when the repository is shared amongst several users, but this adds restrictions e.g. wrong pushed commits can not be removed! Note2: most git users will never need to create a repository
14
Basic Example user Barry clone git clone ~/tmp/my_rep.git barry Cloning into 'barry'... warning: You appear to have cloned an empty repository. Done. So now barry has its own “working directory” with full blown repository
15
Basic Example user Barry status cd barry echo "barry his first line" > readme.txt git status # On branch master # # Initial commit # # Untracked files: # (use "git add..." to include in what will be committed) # # readme.txt nothing added to commit but untracked files present (use "git add" to track)
16
Basic Example user Barry add git add. git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached..." to unstage) # # new file: readme.txt #
17
Basic Example user Barry commit & push git commit -m "barry his first line" [master (root-commit) 33a9791] initial commit from barry 1 file changed, 1 insertion(+) create mode 100644 readme.txt git push Counting objects: 3, done. Writing objects: 100% (3/3), 233 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To /home/apool/tmp/my_rep.git * [new branch] master -> master Note: the hashes 33a9791 is not from the file but from the full repository !
18
Basic Example user John clone/commit/push git clone ~/tmp/my_rep.git john cd john echo "john his first line" >> readme.txt git commit -a -m "john his first line" git push Note: skipped the “git add command”
19
Basic Example user Barry merge from John cd ~/tmp/barry git pull remote: Counting objects: 8, done. remote: Compressing objects: 100% (3/3), done. remote: Total 6 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (6/6), done. From /home/apool/tmp/my_rep 33a9791..4784152 master -> origin/master Updating 33a9791..4784152 Fast-forward readme.txt | 3 +++ 1 file changed, 3 insertions(+)
20
Basic Example create conflict cd ~/tmp/barry echo "barry his conflicting line" >> readme.txt git commit -a -m "barry his conflicting line" No push here ! cd ~/tmp/john echo "john his conflicting line" >> readme.txt git commit -a -m "john his conflicting line" Now “barry” repository and “john” repository different data on the same line ! git push Now central repository and “barry” repository different data on the same line !
21
Basic Example outdated local repository cd ~/tmp/barry git push To /home/apool/tmp/my_rep.git ! [rejected] master -> master (already exists) error: failed to push some refs to '/home/apool/tmp/my_rep.git' hint: Updates were rejected because the destination reference already exists hint: in the remote. Barry cannot push his repository because the HEAD of the remote master has moved to a newer commit. Barry needs first to get his local repository to move to the HEAD of the remote master. (git pull => fetch + merge)
22
Basic Example update / experience conflict git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From /home/apool/tmp/my_rep 1ed438d..ac6682c master -> origin/master Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result.
23
Basic Example view merge fail result cat readme.txt barry his first line john his first line <<<<<<< HEAD barry his conflicting line ======= john his conflicting line >>>>>>> ac6682c38e131e9653ca14045c8f1011a509d35c
24
Basic Example manual resolve conflict edit / correct readme.txt to solve conflict git commit -a -m "barry resolved merge manual" [master 555baa9] barry resolved merge manual git push Counting objects: 10, done. Delta compression using up to 12 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 634 bytes, done. Total 6 (delta 1), reused 0 (delta 0) To /home/apool/tmp/my_rep.git ac6682c..555baa9 master -> master
25
Basic Example check if john got merge cd ~/tmp/john git pull Updating ac6682c..555baa9 Fast-forward readme.txt | 4 ++++ git log --abbrev-commit --pretty=oneline bb80fe1 barry resolved merge manual f99d0d9 barry his conflicting line e1f3f4f john his conflicting line 60a2fb1 barry his second line f604bf7 barry his first line Note: above SHA1 keys are not correct in this example
26
Basic Example: tag Pointer to specific hash Create git tag v1.0 git push tag Show git tag v1.0 Tag Conventions: to be defined A tag can afterwards be moved to a different hash !!
27
Basic Example Final: gitk To better understand how to use git Use gitk for a nice history view But use command line commands for control gitk
28
Basic Example: diff git diff my_branch master index = stage
29
Basic Example.gitignore file git status Changed files = want to see Untracked files – Forgotten files = want to see – Created files:.o files, … = do not want to see Might cause missing the files we want to see Possible to create everywhere a.gitignore file with blocking rules *.o *.bak tmp/* build/*
30
Part 2 Branches
31
Branch Example: Overview John creates branch Modify file Commit Push branch remote Barry gets Johns branch Merges branch in master Barry removes local and remote branch of John Rebase
32
Branch Example: Create & List at John Create branch “john_br” ( = copy of current master ) git checkout -b john_br List both local and remote-tracking branches git branch -a * john_br master remotes/origin/HEAD -> origin/master remotes/origin/master
33
Branch Example: Modify, commit and push echo "john add line branch john_br" >> readme.txt git commit -a -m "add line branch john_br" For the first time explicit push the new branch in the remote!! git push --set-upstream origin john_br To /home/apool/tmp/my_rep.git * [new branch] john_br -> john_br
34
Branch Example: View at Barry Check if barry can see the remote branch of john ~/tmp/barry git pull From /home/apool/tmp/my_rep * [new branch] john_br -> origin/john_br Already up-to-date. git branch -a * master remotes/origin/john_br remotes/origin/master
35
Branch Example: Barry merges John his branch Barry merges the remote branch in his local master branch git merge origin/john_br master Updating bb80fe1..d3c2d2d Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+) Barry pushes the merge results from his local master to the remote git push To /home/apool/tmp/my_rep.git bb80fe1..d3c2d2d master -> master
36
Branch Example: Work on John his branch You do not need to merge: You can also work on john his branch You can cherry pick from john his branch You can checkout differences between master and john his branch … git checkout john_br Branch john_br set up to track remote branch john_br from origin. Switched to a new branch 'john_br' git branch -a * john_br master remotes/origin/john_br remotes/origin/master
37
Branch Example: Manage branches Diff between branches (works also for dirs and files) git diff origin/john_br master Toggle between branches git checkout john_br git checkout master Delete local branch git branch -D john_br Delete remote branch git branch -rd origin/john_br ? Git push probably needed? Other “users” need to use remote prune (instead of pull) to remove the deleted remote branch john_br from their remote list git remote prune origin
38
Rebase Move split moment to current master git checkout john_br git merge master Often this makes the git history cleaner (branches) Fast Forward
39
Part 3 Undo
40
Undo Example Correct commit message Look at previous state Remove commit
41
Undo Example: replace last commit message git commit -a -m "wrong message" [master 9a20789] wrong message 1 file changed, 1 insertion(+) git log --abbrev-commit --pretty=oneline -n 2 9a20789 wrong message d3c2d2d john added line to branch john_br git commit --amend -m "good message" [master 9bd6247] good message 1 file changed, 1 insertion(+) git log --abbrev-commit --pretty=oneline -n 2 9bd6247 good message d3c2d2d john added line to branch john_br
42
Undo Example: step back 2 commits git log --abbrev-commit --pretty=oneline -n 4 d3c2d2d john added line to branch john_br bb80fe1 barry resolved merge manual f99d0d9 barry his conflicting line e1f3f4f john his conflicting line git checkout HEAD~2 Note: checking out 'HEAD~2'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. HEAD is now at f99d0d9... barry his conflicting line
43
Undo Example: status git log --abbrev-commit --pretty=oneline -n 2 f99d0d9 barry his conflicting line e1f3f4f john his conflicting line We are not on branch master anymore git branch -a * (no branch) master remotes/origin/master
44
Undo Example: return to master git checkout master Previous HEAD position was f99d0d9... barry his conflicting line Switched to branch 'master‘ git log --abbrev-commit --pretty=oneline -n 4 d3c2d2d john added line to branch john_br bb80fe1 barry resolved merge manual f99d0d9 barry his conflicting line e1f3f4f john his conflicting line
45
Undo Example: remove last local commit git commit -a -m "bad commit" [master 03fbf2a] bad commit 1 file changed, 1 insertion(+) git log --abbrev-commit --pretty=oneline -n 2 03fbf2a bad commit 9bd6247 barry his second line git reset --hard HEAD^ HEAD is now at 9bd6247 barry his second line git log --abbrev-commit --pretty=oneline -n 1 9bd6247 barry his second line
46
Way of working Check if no dangling file git status Check diff before commit git diff – disabled crucial part of code? – added debug code? Use one explaining one liner, starting with module name git commit -a -m "vision: fixed error in ball size calculation" git pull Correct merge if auto merge failed git push
47
Pit-holes git commit –m “my story“ But did not add files, so nothing will be committed forget to do git push before removing working directory (including repository) forget a push of another branch before removing working directory forget to push newly created branches to the remote Think you are working on master branch
48
Good luck
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.