Index and Working Copy $ # Add content to bar $ echo "Content added to bar" >> bar $ Modifs not in index Modifs in index Add file baz master Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 36
Index and Working Copy $ # Add content to bar $ echo "Content added to bar" >> bar $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: foo Changes not staged for commit: (use "git add <file>..." to update what will Modifs not in index be committed) Modifs in index (use "git checkout -- <file>..." to discard changes in working directory) Add file baz master modified: bar Log rev 3 Log rev 2 $ Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 37
Illustra=ons : diff Modifs not in index git diff Modifs in index git diff HEAD git diff --staged Add file baz master Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 38
Index and Working Copy $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: foo Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard Modifs not in index changes in working directory) Modifs in index modified: bar Add file baz master $ Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 39
Index and Working Copy $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: foo Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard Modifs not in index changes in working directory) Modifs in index modified: bar Add file baz master $ # Add more content to foo Log rev 3 $ echo “more content” >> foo Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 40
Index and Working Copy $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: foo Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard Modifs not in index changes in working directory) Modifs in index modified: bar Add file baz master modified: foo Log rev 3 $ Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 41
Bypassing the index $ # Commit working copy state of foo $ git commit foo Modifs not in index Modifs in index Add file baz master Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 42
Bypassing the index $ # Commit working copy state of foo $ git commit foo [master 5e60acc] Add content to foo 1 file changed, 2 insertions(+) $ Modifs not in index master Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 43
Bypassing the index $ # Commit working copy state of foo $ git commit foo [master 5e60acc] Add content to foo 1 file changed, 2 insertions(+) $ $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard Modifs not in index changes in working directory) master Add content to foo modified: bar Add file baz $ Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 44
Bypassing the index $ # Commit working copy state of all tracked files $ git commit -a -m "Add content to bar” Modifs not in index master Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 45
Bypassing the index $ # Commit working copy state of all tracked files $ git commit -a -m "Add content to bar" [master 119dfba] Add content to bar 1 file changed, 1 insertion(+) $ git status On branch master nothing to commit, working directory clean $ master Add content to bar Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 46
Configuring git # Configure your name and e-mail address (almost mandatory) # If you don’t, you’ll get: $ git commit *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. # Configure your name and e-mail address $ git config --global user.name "David Parsons" $ git config --global user.email david.parsons@inria.fr DAVID PARSONS - INTRODUCTION TO GIT 47
Configuring git $ # Configure the editor git will open when needed $ git config --global core.editor nano $ $ # Setup a few aliases, either simple shorthands... $ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.s status $ $ # ... or including options $ git config --global alias.lg "log --pretty=format:\"%h - %an : %s\"" $ $ # You can even create new commands $ git config --global alias.unstage "reset HEAD” DAVID PARSONS - INTRODUCTION TO GIT 48
5 INTERACTING WITH A REMOTE Scenario DAVID PARSONS - INTRODUCTION TO GIT DAVID PARSONS - INTRODUCTION TO GIT 49
Working in parallel clone clone master Central repo DAVID PARSONS - INTRODUCTION TO GIT 50
Working in parallel r/o/master r/o/master master master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 51
Working in parallel master master r/o/master r/o/master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 52
Working in parallel push master master r/o/master r/o/master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 53
Working in parallel r/o/master master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 54
Working in parallel push r/o/master master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 55
Working in parallel push r/o/master master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 56
Working in parallel fetch r/o/master master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 57
Working in parallel r/o/master master master r/o/master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 58
Working in parallel master r/o/master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 59
Working in parallel push master r/o/master master master r/o/master Central repo DAVID PARSONS - INTRODUCTION TO GIT 60
Working in parallel r/o/master master master r/o/master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 61
Working in parallel pull r/o/master master master r/o/master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 62
Working in parallel r/o/master master r/o/master master master Central repo DAVID PARSONS - INTRODUCTION TO GIT 63
6 INTERACTING WITH A REMOTE Corresponding Commands DAVID PARSONS - INTRODUCTION TO GIT DAVID PARSONS - INTRODUCTION TO GIT 64
Cloning a repository $ # Clone a remote repository $ # git clone <remote-repo-url> [local-name] $ DAVID PARSONS - INTRODUCTION TO GIT 65
Cloning a repository $ # Clone a remote repository $ # git clone <remote-repo-url> [local-name] $ git clone […]/collab.git Cloning into ‘collab'... done. $ DAVID PARSONS - INTRODUCTION TO GIT 66
Cloning a repository $ # Clone a remote repository $ # git clone <remote-repo-url> [local-name] $ git clone […]/collab.git Cloning into ‘collab'... done. $ cd collab # Don’t forget to cd into the local repo dir $ Add content to bar v0.1 master remotes/origin/master Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 67
Other user’s viewpoint $ git clone […]/collab.git Cloning into ‘collab'... done. $ cd collab # Don’t forget to cd into the local repo dir $ Add content to bar v0.1 master remotes/origin/master Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 68
Remotes $ # Clone a remote repository $ # git clone <remote-repo-url> [local-name] $ git clone […]/collab.git Cloning into ‘collab'... done. $ cd collab # Don’t forget to cd into the local repo dir $ git remote origin $ git remote –v origin […]/collab.git (fetch) origin […]/collab.git (push) $ Add content to bar v0.1 master remotes/origin/master Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 69
Remote tracking branch $ # Commit stuff $ echo "This is the content of bar" > bar $ Modifs not in index Add content to bar v0.1 master remotes/origin/master Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 70
Remote tracking branch $ # Commit stuff $ echo "This is the content of bar" > bar $ git commit bar -m "Modify bar" $ master Modify bar v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 71
Push $ # Push your commits onto the remote repository $ git push master Modify bar v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 72
Push $ # Push your commits onto the remote repository $ git push Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 318 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To […]/collab.git 119dfba..a8f9783 master -> master $ master remotes/origin/master Modify bar v0.1 Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 73
Other user’s viewpoint $ Add content to bar v0.1 master remotes/origin/master Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 74
Other user’s viewpoint $ # Commit stuff $ echo "This is foo’s new content" > foo $ git commit foo -m "Change foo" $ master Change foo v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 75
Failed push $ git push To […]/collab.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to ‘[…]/collab.git’ hint: Updates were rejected because the remote contains work that you hint: do not have locally. This is usually caused by another hint: repository pushing to the same ref. You may want to first hint: integrate the remote changes (e.g., 'git pull ...') before hint: pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ master Change foo v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 76
Fetch $ git fetch master Change foo v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 77
Fetch $ git fetch From […]/collab.git 119dfba..a8f9783 master -> origin/master $ Change foo master Modify bar remotes/origin/master v0.1 Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 78
Merge $ git merge Merging consists in injec=ng the accumulated Change foo master changeset of r/o/master into master Modify bar remotes/origin/master A merge is embodied by a merge commit that has two v0.1 Add content to bar parents and that includes the changes from both sides Add content to foo Add file baz The branch being merged is untouched DAVID PARSONS - INTRODUCTION TO GIT 79
Merge without conflicts $ git merge Merge made by the 'recursive' strategy. bar | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bar $ Merge remote-tracking branch 'origin/master' master Modify bar remotes/origin/master Change foo When there are no conflicts between the two changesets, v0.1 Add content to bar git merge automa=cally triggers the crea=on of the merge Add content to foo commit. It will open your core editor with a predefined log msg Add file baz DAVID PARSONS - INTRODUCTION TO GIT 80
Push $ git push $ Merge remote-tracking branch 'origin/master' master remotes/origin/master Modify bar Change foo v0.1 Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 81
Pull $ git pull master Modify bar v0.1 remotes/origin/master Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 82
Pull $ git pull […] $ Merge remote-tracking branch 'origin/master' master remotes/origin/master Modify bar Change foo v0.1 Add content to bar Add content to foo Add file baz DAVID PARSONS - INTRODUCTION TO GIT 83
Git push configura=on $ # When pushing for the first time, you might get a warning message such as this one: warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple $ # If you do, choose the new behaviour, which is safest: $ git config --global push.default simple DAVID PARSONS - INTRODUCTION TO GIT 84
7 MANAGING CONFLICTS DAVID PARSONS - INTRODUCTION TO GIT DAVID PARSONS - INTRODUCTION TO GIT 85
Managing conflicts $ git merge Change bar master Modify bar r/o/master v0.1 Add content to bar Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 86
Managing conflicts $ git merge Auto-merging bar CONFLICT (content): Merge conflict in bar Automatic merge failed; fix conflicts and then commit the result. $ Change bar master Modify bar r/o/master v0.1 Add content to bar Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 87
Managing conflicts $ git merge Auto-merging bar CONFLICT (content): Merge conflict in bar Automatic merge failed; fix conflicts and then commit the result. $ $ cat bar This is line 1 This is line 2 Change bar master This is line 3 <<<<<<< HEAD Modify bar r/o/master This is the fourth line v0.1 Add content to bar ======= This is line number 4 Add content to foo >>>>>>> featureA Add file baz This is line 5 This is line 6 Log rev 3 This is line 7 This is line 8 Log rev 2 $ Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 88
Managing conflicts $ # You can edit the conflicting files directly and then stage them and commit, or you can use a merge tool $ $ git mergetool Change bar master Modify bar r/o/master v0.1 Add content to bar Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 89
mergetool – p4merge DAVID PARSONS - INTRODUCTION TO GIT 90
Configuring a mergetool $ git config --global merge.tool meld $ git config --global mergetool.meld.cmd 'meld $LOCAL $MERGED $REMOTE' $ git config --global mergetool.meld.trustExitCode false $ DAVID PARSONS - INTRODUCTION TO GIT 91
8 Branches DAVID PARSONS - INTRODUCTION TO GIT DAVID PARSONS - INTRODUCTION TO GIT 92
What is a branch ? Each branch contains an alterna=ve version of the same set of files master branch Git is meant for an extensive use of branches « master » is the main branch, i.e. that which is checked out by default DAVID PARSONS - INTRODUCTION TO GIT 93
Branch types Git does not know about branch types, it is the usage you make of a branch that determines its “type” master A branch may remain local to a user’s repo or branch be published (pushed) to the central repo Common use cases for branches include: • Feature • Bugfix • LTS • … DAVID PARSONS - INTRODUCTION TO GIT 94
Feature and Bugfix branches These branches are useful for developing a feature or a patch without messing with the master master featureA They are usually meant to be merged back into the master when the feature or patch is ready The workflow is yours (your team’s) to define DAVID PARSONS - INTRODUCTION TO GIT 95
Merging branches Here we have merged branch featureA into branch master. This means we have injected the accumulated changeset of master featureA into master featureA Merging a local branch is just the same as merging a remote- tracking branch This merge is embodied by a commit (unless it is a fast- forward) that has two parents and that include the changes made to both branches (here 5 changesets) Branch featureA is untouched DAVID PARSONS - INTRODUCTION TO GIT 96
Maintenance branches master v1.2.0 Branches can be used for integra=ng bugfixes in earlier v1.1.2 maintained releases ( e.g. LTS) v1.1.1 These branches are not meant to be merged into the v1.1.0 master DAVID PARSONS - INTRODUCTION TO GIT 97
Warning for SVN users • At any =me, your working copy contains only the content of one single branch DAVID PARSONS - INTRODUCTION TO GIT 98
Branches $ git branch * master $ git branch featureA v0.1 master Add content to bar Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 99
Branches $ git branch * master $ git branch featureA $ v0.1 featureA Add content to bar master Add content to foo Add file baz Log rev 3 Log rev 2 Log rev 1 DAVID PARSONS - INTRODUCTION TO GIT 100
Recommend
More recommend