Difference between revisions of "DPS909 & OSD600 Winter 2017 - Git Walkthrough"
(Created page with "=Git Walkthrough= ===Step 1: get some source=== For this walkthrough, we'll need some source code. Normally you'll write code, but for this walkthrough, we'll borrow some p...") |
|||
Line 23: | Line 23: | ||
branches description info refs | branches description info refs | ||
</pre> | </pre> | ||
+ | |||
+ | NOTE: normally we <code>git clone</code> to clone (also known as fork) a repository. We'll do that in a bit. | ||
+ | |||
+ | ===Step 3: adding files=== | ||
+ | |||
+ | Let's inspect the current status of our repo: | ||
+ | |||
+ | <pre> | ||
+ | $ git status | ||
+ | On branch master | ||
+ | |||
+ | Initial commit | ||
+ | |||
+ | Untracked files: | ||
+ | (use "git add <file>..." to include in what will be committed) | ||
+ | |||
+ | .editorconfig | ||
+ | .eslintignore | ||
+ | .gitattributes | ||
+ | .gitignore | ||
+ | .hound.yml | ||
+ | .houndignore | ||
+ | .travis.yml | ||
+ | CHANGELOG.md | ||
+ | CNAME | ||
+ | CONTRIBUTING.md | ||
+ | Gemfile | ||
+ | Gemfile.lock | ||
+ | Gruntfile.js | ||
+ | ISSUE_TEMPLATE.md | ||
+ | LICENSE | ||
+ | README.md | ||
+ | _config.yml | ||
+ | bower.json | ||
+ | composer.json | ||
+ | dist/ | ||
+ | docs/ | ||
+ | grunt/ | ||
+ | js/ | ||
+ | nuget/ | ||
+ | package.js | ||
+ | package.json | ||
+ | sache.json | ||
+ | scss/ | ||
+ | |||
+ | nothing added to commit but untracked files present (use "git add" to track) | ||
+ | </pre> | ||
+ | |||
+ | Our '''Working Directory''' (i.e., <code>bootstrap-4.0.0-alpha.6/</code>) has a lot of files and folders that are '''untracked''' (i.e., git has no record of them in its history). | ||
+ | |||
+ | We include a file in git's history by adding it to the '''index''' (also known as the '''staging area'''). Let's add the <code>README.md</code> file: | ||
+ | |||
+ | <pre> | ||
+ | $ git add README.md | ||
+ | $ git status | ||
+ | On branch master | ||
+ | |||
+ | Initial commit | ||
+ | |||
+ | Changes to be committed: | ||
+ | (use "git rm --cached <file>..." to unstage) | ||
+ | |||
+ | new file: README.md | ||
+ | |||
+ | Untracked files: | ||
+ | (use "git add <file>..." to include in what will be committed) | ||
+ | |||
+ | .editorconfig | ||
+ | .eslintignore | ||
+ | .gitattributes | ||
+ | .gitignore | ||
+ | .hound.yml | ||
+ | .houndignore | ||
+ | .travis.yml | ||
+ | CHANGELOG.md | ||
+ | CNAME | ||
+ | CONTRIBUTING.md | ||
+ | Gemfile | ||
+ | Gemfile.lock | ||
+ | Gruntfile.js | ||
+ | ISSUE_TEMPLATE.md | ||
+ | LICENSE | ||
+ | _config.yml | ||
+ | bower.json | ||
+ | composer.json | ||
+ | dist/ | ||
+ | docs/ | ||
+ | grunt/ | ||
+ | js/ | ||
+ | nuget/ | ||
+ | package.js | ||
+ | package.json | ||
+ | sache.json | ||
+ | scss/ | ||
+ | </pre> | ||
+ | |||
+ | This time <code>git status</code> continues to report our '''Untracked files''', but also includes a new section: '''Changes to be committed'''. The <code>README.md</code> file has been added to the staging area, and is ready to be commited to git's history. | ||
+ | |||
+ | We record a '''snapshot''' of our files by committing the staging area's contents: | ||
+ | |||
+ | <pre> | ||
+ | $ git commit -m "Added README file" | ||
+ | [master (root-commit) 03c9551] Added README file | ||
+ | 1 file changed, 135 insertions(+) | ||
+ | create mode 100755 README.md | ||
+ | </pre> | ||
+ | |||
+ | Let's look at our status again: | ||
+ | |||
+ | <pre> | ||
+ | git status | ||
+ | On branch master | ||
+ | Untracked files: | ||
+ | (use "git add <file>..." to include in what will be committed) | ||
+ | |||
+ | .editorconfig | ||
+ | .eslintignore | ||
+ | .gitattributes | ||
+ | .gitignore | ||
+ | .hound.yml | ||
+ | .houndignore | ||
+ | .travis.yml | ||
+ | CHANGELOG.md | ||
+ | CNAME | ||
+ | CONTRIBUTING.md | ||
+ | Gemfile | ||
+ | Gemfile.lock | ||
+ | Gruntfile.js | ||
+ | ISSUE_TEMPLATE.md | ||
+ | LICENSE | ||
+ | _config.yml | ||
+ | bower.json | ||
+ | composer.json | ||
+ | dist/ | ||
+ | docs/ | ||
+ | grunt/ | ||
+ | js/ | ||
+ | nuget/ | ||
+ | package.js | ||
+ | package.json | ||
+ | sache.json | ||
+ | scss/ | ||
+ | |||
+ | nothing added to commit but untracked files present (use "git add" to track) | ||
+ | </pre> | ||
+ | |||
+ | Here's how things stand now: | ||
+ | * Our working copy continues to have lots of untracked files and folders. However, <code>README.md</code> is not among them | ||
+ | * Our staging area is empty | ||
+ | * Our git repo has 1 commit that added 1 file, which we can see if we use <code>git log</code> | ||
+ | |||
+ | <pre> | ||
+ | $ git log | ||
+ | commit 03c95518ce08f59e850409a2d82bacad110151a1 | ||
+ | Author: David Humphrey (:humph) david.humphrey@senecacollege.ca <david.humphrey@senecacollege.ca> | ||
+ | Date: Wed Jan 11 11:07:09 2017 -0500 | ||
+ | |||
+ | Added README file | ||
+ | </pre> | ||
+ | |||
+ | Every record in our history is called a '''commit''', and it's a snapshot of our repo's filesystem along with metadata: | ||
+ | * a unique commit identifier (SHA-1), in this case <code>03c95518ce08f59e850409a2d82bacad110151a1</code>, which is often shortened to the first 7 characters: <code>03c9551</code>. | ||
+ | * an author, every commit is done by somebody, and we want to keep track of this | ||
+ | * a date and time | ||
+ | * a commit message, describing what this commit changed, added, deleted, etc. | ||
+ | |||
+ | |||
+ | |||
+ | |||
* Client Server (SVN) and Distributed (Git) | * Client Server (SVN) and Distributed (Git) |
Revision as of 12:23, 11 January 2017
Contents
Git Walkthrough
Step 1: get some source
For this walkthrough, we'll need some source code. Normally you'll write code, but for this walkthrough, we'll borrow some pre-existing code to make things easier. Let's download the Bootstrap source:
https://github.com/twbs/bootstrap/archive/v4.0.0-alpha.6.zip
Expand the zip file, and open a terminal to your bootstrap-4.0.0-alpha.6/
directory.
Step 2: start a git repo
$ cd bootstrap-4.0.0-alpha.6 $ git init
Note the presence of a new bootstrap-4.0.0-alpha.6/.git/
directory:
$ ls .git HEAD config hooks objects branches description info refs
NOTE: normally we git clone
to clone (also known as fork) a repository. We'll do that in a bit.
Step 3: adding files
Let's inspect the current status of our repo:
$ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .editorconfig .eslintignore .gitattributes .gitignore .hound.yml .houndignore .travis.yml CHANGELOG.md CNAME CONTRIBUTING.md Gemfile Gemfile.lock Gruntfile.js ISSUE_TEMPLATE.md LICENSE README.md _config.yml bower.json composer.json dist/ docs/ grunt/ js/ nuget/ package.js package.json sache.json scss/ nothing added to commit but untracked files present (use "git add" to track)
Our Working Directory (i.e., bootstrap-4.0.0-alpha.6/
) has a lot of files and folders that are untracked (i.e., git has no record of them in its history).
We include a file in git's history by adding it to the index (also known as the staging area). Let's add the README.md
file:
$ git add README.md $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md Untracked files: (use "git add <file>..." to include in what will be committed) .editorconfig .eslintignore .gitattributes .gitignore .hound.yml .houndignore .travis.yml CHANGELOG.md CNAME CONTRIBUTING.md Gemfile Gemfile.lock Gruntfile.js ISSUE_TEMPLATE.md LICENSE _config.yml bower.json composer.json dist/ docs/ grunt/ js/ nuget/ package.js package.json sache.json scss/
This time git status
continues to report our Untracked files, but also includes a new section: Changes to be committed. The README.md
file has been added to the staging area, and is ready to be commited to git's history.
We record a snapshot of our files by committing the staging area's contents:
$ git commit -m "Added README file" [master (root-commit) 03c9551] Added README file 1 file changed, 135 insertions(+) create mode 100755 README.md
Let's look at our status again:
git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) .editorconfig .eslintignore .gitattributes .gitignore .hound.yml .houndignore .travis.yml CHANGELOG.md CNAME CONTRIBUTING.md Gemfile Gemfile.lock Gruntfile.js ISSUE_TEMPLATE.md LICENSE _config.yml bower.json composer.json dist/ docs/ grunt/ js/ nuget/ package.js package.json sache.json scss/ nothing added to commit but untracked files present (use "git add" to track)
Here's how things stand now:
- Our working copy continues to have lots of untracked files and folders. However,
README.md
is not among them - Our staging area is empty
- Our git repo has 1 commit that added 1 file, which we can see if we use
git log
$ git log commit 03c95518ce08f59e850409a2d82bacad110151a1 Author: David Humphrey (:humph) david.humphrey@senecacollege.ca <david.humphrey@senecacollege.ca> Date: Wed Jan 11 11:07:09 2017 -0500 Added README file
Every record in our history is called a commit, and it's a snapshot of our repo's filesystem along with metadata:
- a unique commit identifier (SHA-1), in this case
03c95518ce08f59e850409a2d82bacad110151a1
, which is often shortened to the first 7 characters:03c9551
. - an author, every commit is done by somebody, and we want to keep track of this
- a date and time
- a commit message, describing what this commit changed, added, deleted, etc.
- Client Server (SVN) and Distributed (Git)
- Snapshots vs. versioned files.
- Walkthrough -
- Checksums, SHA-1 (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 remote
- git remote add
- git fetch
- git pull
- git push
- Github, Pull Requests