Lecture 5 Remotes Sign in on the attendance sheet! Turn in homework at the front!
Today Fetching Pushing Github
Scenario: Alice, Bob, and Charlie collaborate on a project, but each on their own computer b4e2c29: initial commit AliceBobCharlie 8fc42c6: heapchecker no longer infloops
Scenario: Alice, Bob, and Charlie collaborate on a project, but each on their own computer b4e2c29: initial commit AliceBobCharlie 8fc42c6: heapchecker no longer infloops 6f96cf3: don’t create headers in free blocks 8fc42c6: heapchecker no longer infloops
Scenario: Alice, Bob, and Charlie collaborate on a project, but each on their own computer b4e2c29: initial commit AliceBobCharlie 8fc42c6: heapchecker no longer infloops 6f96cf3: don’t create headers in free blocks 8fc42c6: heapchecker no longer infloops How does Alice get commit 6f96cf3 to Bob and Charlie?
Scenario: Alice, Bob, and Charlie collaborate on a project, but each on their own computer b4e2c29: initial commit AliceBobCharlie 8fc42c6: heapchecker no longer infloops 6f96cf3: don’t create headers in free blocks 8fc42c6: heapchecker no longer infloops How does Alice get commit 6f96cf3 to Bob and Charlie? Idea 1: Bob and Charlie each fetch the commit from Alice. Idea 2: Alice pushes the commit to Bob and Charlie.
Fetching (both Bob and Charlie must do this) Two steps: 1.Tell git to treat Alice’s repository as a “remote repository” or a “remote” and tell git where to find it. This needs to happen only once. 2.Tell git to fetch the commits from the remote repository. Note: if you used `git clone` to get your repository, the url that you cloned from was automatically set as a remote repository.
git remote add Example use: git remote add origin (not my actual IP address) Adds a remote repository called “origin” located at (through SSH) “origin” is the default name for a remote, since often times the first remote you have is the one you clone from
git remote Example use: git remote Lists the names of all your remotes, i.e. origin afs alice
git remote -v Example use: git remote -v Verbosely lists the names of all your remotes, i.e. origin afs alice github
Git Remote URLs 4 choices: HTTPS, SSH, local, git HTTPS: git objects and commits are transferred over web protocols. Pretty easy to set up, unless you need authentication. SSH: git objects and commits are transferred over the SSH protocol. Requires a ssh daemon to be running, allows authentication.
git fetch Example use: git fetch origin Downloads and updates all branches published by the remote. Stores these branches as /. For example, if origin has an updated master branch, then running `git fetch origin` will update your local branch called “origin/master”. These branches like origin/master are called “remote-tracking branches”, because their states should match the origin’s branches’ states. Does NOT affect your own branches, like master!
Example use: git branch -r git branch -a Git branch -r lists all remote-tracking branches. Git branch -a lists all branches, whether local or remote-tracking. git branch -r or git branch -a
How do you actually bring in the remote changes?
How do you actually *merge* in the remote changes?
git merge origin/master
git pull Example use: git pull origin Runs `git fetch `, then `git merge / `. Ex) runs `git fetch origin`, then `git merge origin/master`.
Sometimes pulling is inconvenient In order for people to pull from you, you have to be running a web server or a SSH daemon or something requiring setup, usually. If you’re not collaborating with other people, no one else has your changes and your version control isn’t very distributed.
Pushing is hard too If everyone was allowed to push to you, then people could push random commits to troll you. Even if you kept a whitelist of everyone that was allowed to push to you, it’s rather inconvenient for everything in your working directory to get wiped every time someone pushes. -> With git, you’re not allowed to push to a normal repository. This requires a “bare repository”.
Bare Repositories Created with `git init --bare` Has no working directory, just the contents of what the.git folder would have had Is meant to only be pushed to Fortunately, you rarely have to deal with doing this since Github does all of this behind the scenes for you
Using Github as a remote
git push Example use: git push origin master Updates the remote-tracking branch ( / ) and uploads the necessary commits for that branch to origin You must be fully up-to-date with the remote in order to push! Otherwise your push will be rejected.
What happens if you run `git push`? A. git exception B. git pushes current branch to origin (default remote) C. git pushes all branches to origin (default remote) D. git pushes current branch to all remotes E. git pushes all branches to all remotes F. git pushes to a random remote G. segfault
What happens if you run `git push`? A. git exception B. git pushes current branch to origin (default remote) git2.0 default C. git pushes all branches to origin (default remote) pre-git2.0 default D. git pushes current branch to all remotes E. git pushes all branches to all remotes F. git pushes to a random remote G. segfault
Summary Configuring remotes: git remote [-v] – lists remotes [verbosely] git remote add - configure a new remote git branch –r – lists remote-tracking branches Fetching: git fetch - downloads updates to all remote-tracking branches to match the remote git pull - runs `git fetch`, then merges in updates to the current branch Pushing: git push - uploads changes in your branches to the remote