Open main menu

CDOT Wiki β

Changes

DPS909 & OSD600 Winter 2017 - Git Walkthrough 2

6,185 bytes added, 13:09, 12 January 2017
Step 3: Cloning a Forked Repository
# checked-out the latest version of the code to our <code>./Spoon-Knife/</code> working directory
Let's take a look at what we have in our new cloned repo:
 
<pre>
$ cd Spoon-Knife
$ ls
README.md index.html styles.css
$ git show
commit d0dd1f61b33d64e29d8bc1372a94ef6a2fee76a9
Author: The Octocat <octocat@nowhere.com>
Date: Wed Feb 12 15:20:44 2014 -0800
 
Pointing to the guide for forking
 
diff --git a/README.md b/README.md
index 0350da3..f479026 100644
--- a/README.md
+++ b/README.md
@@ -6,4 +6,4 @@ Creating a *fork* is producing a personal copy of someone else's project. Forks
 
After forking this repository, you can make some changes to the project, and submit [a Pull Request](https://github.com/octocat/Spoon-Knife/pulls) as practice.
 
-For some more information on how to fork a repository, [check out our guide, "Fork a Repo"](https://help.github.com/articles/fork-a-repo). Thanks! :sparkling_heart:
+For some more information on how to fork a repository, [check out our guide, "Forking Projects""](http://guides.github.com/overviews/forking/). Thanks! :sparkling_heart:
</pre>
 
===Step 4: Syncing Changes Between Repositories===
 
Let's make a change to our cloned Spoon-Knife repo, then sync that change with our remote Github forked repository. We start by making a change locally.
 
In your editor, open the <code>Spoon-Knife/index.html</code> file and make a change to the text, for example:
 
{| class="wikitable"
! style="font-weight: bold;" | Before
! style="font-weight: bold;" | After
|-
| <pre>
<!-- Feel free to change this text here -->
<p>
Fork me? Fork you, @octocat!
</p>
</pre>
| <pre>
<p>
Look, Mom, I'm on Github!
</p>
</pre>
|}
 
Now we can '''add''' and '''commit''' our change:
 
<pre>
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
 
modified: index.html
 
no changes added to commit (use "git add" and/or "git commit -a")
$ git add index.html
$ git commit -m "Update text in index.html"
[master 57f7209] Update text in index.html
1 file changed, 1 insertion(+), 2 deletions(-)
$ git show
commit 57f7209f758c4ba32be537434196b9168b2d53dc
Author: David Humphrey (:humph) david.humphrey@senecacollege.ca <david.humphrey@senecacollege.ca>
Date: Thu Jan 12 11:48:56 2017 -0500
 
Update text in index.html
 
diff --git a/index.html b/index.html
index a83618b..e4cff78 100644
--- a/index.html
+++ b/index.html
@@ -11,9 +11,8 @@
 
<img src="forkit.gif" id="octocat" alt="" />
 
-<!-- Feel free to change this text here -->
<p>
- Fork me? Fork you, @octocat!
+ Look, Mom, I'm on Github!
</p>
 
</body>
</pre>
 
Before our commit, there were 3 commits in the repo's log. Now there are 4:
 
<pre>
$ git log
commit 57f7209f758c4ba32be537434196b9168b2d53dc
Author: David Humphrey (:humph) david.humphrey@senecacollege.ca <david.humphrey@senecacollege.ca>
Date: Thu Jan 12 11:48:56 2017 -0500
 
Update text in index.html
 
commit d0dd1f61b33d64e29d8bc1372a94ef6a2fee76a9
Author: The Octocat <octocat@nowhere.com>
Date: Wed Feb 12 15:20:44 2014 -0800
 
Pointing to the guide for forking
 
commit bb4cc8d3b2e14b3af5df699876dd4ff3acd00b7f
Author: The Octocat <octocat@nowhere.com>
Date: Tue Feb 4 14:38:36 2014 -0800
 
Create styles.css and updated README
 
commit a30c19e3f13765a3b48829788bc1cb8b4e95cee4
Author: The Octocat <octocat@nowhere.com>
Date: Tue Feb 4 14:38:24 2014 -0800
 
Created index page for future collaborative edits
</pre>
 
Our local repo and our forked repo on Github are not out of sync: our local repo is '''ahead''' of the remote fork by '''1 commit'''. By default, repos don't stay in sync. You can do whatever you want locally, and only share commits between repos when you want/need to do so.
 
Let's send our local changes (i.e., the new commit) to the remote forked repo. We do this using git's <code>push</code> command. Before we use <code>push</code>, we need to mention another git command, <code>remote</code>.
 
We said above that git repos aren't automatically kept in sync with one another, which is true. However, a git repo can be made aware of remote repos (i.e., other copies of the same repo that you or someone else owns) that you care about. For example, you might be working on a team of 3 developers, and want to tell git about each of their forks on Github, as well as your own. This lets you share commits between various remote repos.
 
When you clone a repo, git automatically sets up remote for you called <code>origin</code>. You can see it by doing this:
 
<pre>
$ git remote -v
origin git@github.com:humphd/Spoon-Knife.git (fetch)
origin git@github.com:humphd/Spoon-Knife.git (push)
</pre>
 
Here git shows two remote URLs, one for '''fetch''' and one for '''push'''. By default they are the same, although it's possible [http://sleepycoders.blogspot.ca/2012/05/different-git-push-pullfetch-urls.html to have different URLs for the same remote]. We can also add more remotes by giving a '''remote name''' and a '''git URL''', like so:
 
<pre>
$ git remote add upstream https://github.com/octocat/Spoon-Knife.git
$ git remote -v
origin git@github.com:humphd/Spoon-Knife.git (fetch)
origin git@github.com:humphd/Spoon-Knife.git (push)
upstream https://github.com/octocat/Spoon-Knife.git (fetch)
upstream https://github.com/octocat/Spoon-Knife.git (push)
</pre>
 
Now we have 2 remotes:
 
* <code>origin</code> - our personal forked copy on Github
* <code>upstream</code> - the so-called ''upstream'' version of the repo, the one we originally forked
 
This is a very common way to set up your remotes. You can obviously use any names you like vs. <code>origin</code> and <code>upstream</code> (you can also use <code>git remote rename</code> if you want to change the names later).
 
With our remotes set up, we're ready to '''push''' our local change (commit) to our forked repo on Github:
 
<pre>
$ git push origin
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 423 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To github.com:humphd/Spoon-Knife.git
d0dd1f6..57f7209 master -> master
</pre>
 
If everything goes according to plan, you should be able to see your commit on Github. For me, it's visible via the following URL:
 
https://github.com/humphd/Spoon-Knife/commits/master
 
Change it to use your username, and make sure you see your changes:
 
https://github.com/{your-username}/Spoon-Knife/commits/master
 
My full commit is visible at https://github.com/humphd/Spoon-Knife/commit/57f7209f758c4ba32be537434196b9168b2d53dc
* Client Server (SVN) and Distributed (Git)
* Snapshots vs. versioned files.
** Walkthrough -
** Checksums, SHA-1 ([http://www.miraclesalad.com/webtools/sha1.php try online])
** Starting a Git Repo:
*** git init
*** git clone
** File States:
*** Untracked (not known to git)
*** Tracked: modified, staged, committed
** The staging area
* Basic Git Commands and Concepts
** git help ''<command>''
** git add
** git commit, git commit -m, git commit -a
** git rm
** git mv
** git status
** git log
** git diff, git diff --staged
** .gitignore
** Branches
*** HEAD, master
*** git checkout, git checkout -b
*** git branch, git branch -a, git branch -d, git branch --merged, git branch --contains
*** git merge
*** git rebase
** Remotes
*** origin, origin/''branch''
*** git push
** Github, Pull Requests
 
** .gitignore
** Branches
*** HEAD, master
*** git checkout, git checkout -b
*** git branch, git branch -a, git branch -d, git branch --merged, git branch --contains
*** git merge
*** git rebase