git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · merging branches •...
TRANSCRIPT
![Page 1: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/1.jpg)
Git branches and remotes
Landon Cox February 2, 2017
![Page 2: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/2.jpg)
Basic terminology and overview • Commit
• Object pointing to a snapshot • Named by hash and stored as a tree • Commits have parents that point to the previous snapshot
• Branch • Pointer to a commit (and its snapshot) • e.g., “master”
• Remote • Collection of branches stored on a remote server • e.g., a gitlab or github project
https://git-scm.com/book/en/v2/
![Page 3: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/3.jpg)
Basic terminology and overview • Commit
• Object pointing to a snapshot • Named by hash and stored as a tree • Commits have parents that point to the previous snapshot
• Branch • Pointer to a commit (and its snapshot) • e.g., “master”
• Remote • Collection of branches stored on a remote server • e.g., a gitlab or github project
![Page 4: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/4.jpg)
A commit and its tree
$> git add README test.rb LICENSE$> git commit -a -m “Initial commit”
What are these funny hex numbers?
![Page 5: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/5.jpg)
A commit and its tree
$> git add README test.rb LICENSE$> git commit -a -m “Initial commit”
Why will the commit hash change if I modify a file?
![Page 6: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/6.jpg)
A commit and its tree
$> git add README test.rb LICENSE$> git commit -a -m “Initial commit”
best
![Page 7: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/7.jpg)
Commits and their parents What do I need to move from one
commit to another?
![Page 8: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/8.jpg)
Basic terminology and overview • Commit
• Object pointing to a snapshot • Named by hash and stored as a tree • Commits have parents that point to the previous snapshot
• Branch • Pointer to a commit (and its snapshot) • e.g., “master”
• Remote • Collection of branches stored on a remote server • e.g., a gitlab or github project
![Page 9: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/9.jpg)
A branch and its commit history
![Page 10: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/10.jpg)
Two branches w/ same commits
$> git branch testing
![Page 11: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/11.jpg)
HEAD pointing to master branch
$> git log --oneline --decorate f30ab (HEAD -> master, testing) add …34ac2 Fixed bug #1328 - stack overflow …98ca9 The initial commit of my project
![Page 12: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/12.jpg)
HEAD pointing to testing branch
$> git checkout testing
![Page 13: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/13.jpg)
A new commit under testing
$> vi test.rb$> git commit -a -m “made a change”
![Page 14: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/14.jpg)
Checking out master
$> git checkout master
![Page 15: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/15.jpg)
A new commit under master
$> vi test.rb$> git commit -a -m “another change”
Given the diffs I have, can I integrate my testing changes into master?
![Page 16: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/16.jpg)
Merging branches
![Page 17: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/17.jpg)
Merging branches
$> git branch iss53$> git checkout iss53
$> git checkout -b iss53
![Page 18: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/18.jpg)
Merging branches
$> vi index.html$> git commit -a -m “added a new footer [issue 53]”
![Page 19: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/19.jpg)
Merging branches
$> git checkout master$> git checkout -b hotfix$> git commit -a -m “fixed broken link”
![Page 20: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/20.jpg)
Merging branches
$> git checkout master$> git merge hotfix
![Page 21: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/21.jpg)
Merging branches
$> git branch -d hotfix$> git checkout iss53$> vi index.html$> git commit -a –m “finished footer [issue 53]”
![Page 22: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/22.jpg)
Merging branches
![Page 23: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/23.jpg)
Merging branches
$> git checkout master$> git merge iss53
Merge commit: special commit created from three-
way merge
![Page 24: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/24.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
$> git merge iss53Auto-merging index.htmlCONFLICT (content): Merge conflict in index.htmlAutomatic merge failed; fix conflicts and then commit the result.
![Page 25: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/25.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
$ git statusOn branch masterYou have unmerged paths. (fix conflicts and run "git commit")
Unmerged paths: (use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Edit files to manually resolve the conflict.
![Page 26: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/26.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
<<<<<<< HEAD:index.html<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at [email protected]</div>>>>>>>> iss53:index.html
Inside the conflicted file (index.html in example)
![Page 27: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/27.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
<<<<<<< HEAD:index.html<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at [email protected]</div>>>>>>>> iss53:index.html
Content in master branch (since HEAD pointed to master during merge)
Content in iss53 branch
![Page 28: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/28.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
<div id="footer"> please contact us at [email protected]</div>
Content of conflicted file after manual resolution.
(i.e., kept the iss53 content, removed <<<, ===, and >>> lines)
![Page 29: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/29.jpg)
Merging branches • Sometimes merges fail due to conflicts
• Git cannot create the diffs to move one commit to another • Often due to multiple changes to the line in a file
• Once conflict has been resolved, add and commit
$> git add index.html$> git commit
![Page 30: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/30.jpg)
Rebasing branches
![Page 31: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/31.jpg)
Basic terminology and overview • Commit
• Object pointing to a snapshot • Named by hash and stored as a tree • Commits have parents that point to the previous snapshot
• Branch • Pointer to a commit (and its snapshot) • e.g., “master”
• Remote • Collection of branches stored on a remote server • e.g., a gitlab or github project
![Page 32: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/32.jpg)
Remote branches
“origin” = default name of remote repo “master” = branch of remote repo
![Page 33: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/33.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git fetch origin$> git merge origin/master
Equivalent commands to clone.
![Page 34: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/34.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git fetch origin$> git merge origin/master
“origin” is local name for this remote
![Page 35: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/35.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git fetch origin$> git merge origin/master
fetch retrieves data for remote
merge remote/branch into local repo
![Page 36: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/36.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add jane [email protected]:project.git$> git fetch jane$> git merge jane/master
Can name remote whatever you want, e.g., “jane”
![Page 37: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/37.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git fetch origin$> git merge origin/master
“git pull” is shorthand for fetch and merge
![Page 38: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/38.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git pull origin master
“git pull” is shorthand for fetch and merge
![Page 39: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/39.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git pull origin master
Note that because pull merges, it can lead to conflicts
![Page 40: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/40.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git remote –v
“git remote –v” prints all the remotes you’ve linked
![Page 41: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/41.jpg)
Remote branches
$> mkdir project$> cd project$> git init$> git remote add origin [email protected]:project.git$> git remote rm origin
“git remote rm” removes a remote
![Page 42: Git branches and remotesdb.cs.duke.edu/.../notes/feb2/git2-spring17.pdf · Merging branches • Sometimes merges fail due to conflicts • Git cannot create the diffs to move one](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd3beef9fb81f499c03d7fe/html5/thumbnails/42.jpg)
How to work with your group project