CMT302 E-commerce & Innovation
Session 1 – LAB 1
Version Control with Git Dr
Office: C/2.02
Copyright By PowCoder代写 加微信 powcoder
Learning objectives
Learn and practice how to use git for version control, working with local and remote repositories
‘Pro Git’ book
– 2nd ed, 2014, available at:
– https://git-scm.com/book/en/v2
– of particular relevance Ch. 2 and parts of 5
(but other chapters might be of interest if you want to learn more)
Other (printed) books, e.g.
Working smarter, not harder!
Using Command line
e.g. Windows, see: ‘MS-DOS and command line overview’ -> e.g. https://docs.microsoft.com/en-us/ windows-server/administration/windows-commands/ windows-commands, or other resources on the web, e.g. http://www.computerhope.com/overview.htm
Documentation on our systems, software, etc: http://docs.cs.cf.ac.uk/
COMSC’s GitLab
https://gitlab.cs.cf.ac.uk/
Basic intro: http://docs.cs.cf.ac.uk/notes/using-gitlab/
Basic Git Commands
Create your repository (repo) Copy (clone) your repo
Add a file to your repo Configure the commit author Make a commit
View your commits
View commit differences (diff) Remove and rename files
More “Advanced” Git
Merge/ Rebase Remote Git Distributed Git
In this module, we will consider remote git, but not the other topics (these will be explored
in other modules).
Main Sections of Git Project
The Git directory is where Git stores the metadata and object database for
Wo sta dir
rki gin ect
Three States of Git
you made changes to a file, but not committed to the DB yet
you have marked a modified file in its current version to go into your next commit snapshot
your data is safely stored in DB
As you edit files, Git sees them as modified, because you’ve changed them since your last commit. You stage these modified files and then commit all your
Lifecycle of the status of your files
staged changes, and the cycle repeats.
Initializing a Repository in an Existing Directory
If you want to get a copy of an existing Git repository – for example, a project
down by default when you run git clone. In fact, if your server disk gets cor- you’d like to contribute to – the command you need is git clone. If you’re fa-
Working with a Repo
rupted, you can often use nearly any of the clones on any client to set the server
miliar with other VCS systems such as Subversion, you’ll notice that the com-
If you’re starting to track an existing project in Git, you need to go to t
back to the state it was in when it was cloned (you may lose some server-side
mand is “clone” and not “checkout”. This is an important distinction – instead project’s directory and type:
hooks and such, but all the versioned data would be there – see “Getting Git of getting just a working copy, Git receives a full copy of nearly all data that the
to start tracking a project:
on a Server” for more details).
server has. Every version of every file for the history of the project is pulled
mit that with a few git add commands that specify the Y with git clone [url]. For example, if you want to
. You can accomplish ou clone a repository
$ git init
n by default when yo
dowurungitclone.Infact,ifyourserverdiskgetscor- fciloens eyothuewGaitnltintokatrbaleckli,bfroallroywcaeldlebdyliabgitt2, ycoumcmaint:do so like this:
rupted, you can often use nearly any of the clones on any client to set the server
to start tracking files:
back to the state it was in when it was cloned (you may lose some server-side This creates a new subdirectory named .git that contains all of your nec
hooethere–see“GettingGit git2
ks and such, but all the versioned data would b
$ git clone https://github.com/libgit2/lib $ git add *.c
sar At this point, nothing in y
y repository files – a Git repository skeleton.
$ git add LICENSE Server” for more details).
ject is tracked yet. (See Chapter 10 for more i $ git commit -m ‘initial project version’
pro nformation about exactly w
Y or example, if you want to
ou clone a repository with git clone [url]. F
That creates a directory named “libgit2”, initializes a .git directory inside it, files are contained in the .git directory you just created.)
clone the Git linkable library called libgit2, you can do so like this:
pulls down all the data for that repository, and checks out a working copy of the
to clone a repo:
If you want to start version-controlling existing files (as opposed to an em
We’ll go over what these commands do in just a minute. At this point, you latest version. If you go into the new libgit2 directory, you’ll see the project
direesanddoaninitialco havt. fileslonetherepository
ctory), you should probably begin tracking those fil e a Git repository with tracked files and an initial commi i$ngthietrec,lroenaedyhtotpbse:w//ogriktehduob.ncorm/ulsiebdg.iIfty2o/uliwbagint2to c
into a directory named something other than “libgit2”, you can specify that as
you can clone a repo to a specific directory of your
Cloning an Existing Repository
the next command-line option:
That creates a directory named “libgit2”, initializes a .git directory inside it, choice:
pulls down all the data for that repository, and checks out a working copy of the
Ifyple,aproject late the project
ou want to get a copy of an existing Git repository – for exam
$ git clone https://github.com/libgit2/libgit2 mylibgit st version. If you go into the new libgit2 directory, you’ll see
you’d like to contribute to – the command you need is git clone. If you’re fa- filesinthere,readytobeworkedonorused.Ifyouwanttoclonetherepositor!1y0
miliar with other VCS systems such as Subversion, you’ll notice that the com-
into a directory named something other than “libgit2”, you can specify that as That command does the same thing as the previous one, but the target di-
tracked and modified files. Git also doesn’t see any untracked files, or they
The main tool you use to determine which files are in which state is the git would be listed here. Finally, the command tells you which branch you’re on
staRtuescoommradndi.nIf ygouCrunhtahisncogmemasnd directly after a clone, you should and informs you that it has not diverged from the same branch on the server.
sFeoer nsomw,etthaint gbrlaiknechthiissa: lways “master”, which is the default; you won’t worry about it here. Chapter 3 will go over branches and references in detail.
to check the status of your files (e.g. staged, committed): Let’s say you add a new file to your project, a simple README file. If the file
$ git status
’t exist before, and you run git status, you see yo
didnuruntrackedfilelike
wou and For abo
no modified or tracked files
On branch master
nothing to commit, working directory clean
$ echo ‘My Project’ > README
his means you have a clean working directory – in other words, there are no
$ git status
ked and modified files. Git also doesn’t see any untracked files, or they
On branch master
lUdntbreacliksetdedfihlerse:. Finally, the command tells you which branch you’re on
(use “git add
informs you that it has not diverged from the same branch on the server. now, that branch is always “master”, which is the default; you won’t worry
ut it here. Chapter 3 will go over branches and references in detail.
didn’t exist before, and you run git status, you see your untracked file like
et’s say you add a new file to your project, a simple README file. If the file
nothing added to commit but untracked files present (use “git add” to track)
You can see that your new README file is untracked, because it’s under the so: README
“Untracked files” heading in your status output. Untracked basically means
is untracked
that Git sees a file you didn’t have in the previous snapshot (commit); Git won’t
$ echo ‘My Project’ > README
start including it in your commit snapshots until you explicitly tell it to do so. It
$ git status
does this so you don’t accidentally begin including generated binary files or
$ git add README
In order to begin tracking a new file, you use the command git add.
Tracking New Files
tracking the README file, you can run this:
If you run your status command again, you can see that your now o be committed:
IrREAD now
tracked and staged t
$ git add README
f you run your status command again, you can see that you
$ git status Otnracbkreadnacnhdmsatasgtedrto be committed: Changes to be committed:
(use “git reset HEAD
$ git status
On branch master
new file: README Changes to be committed:
(use “git reset HEAD
READMEis tracked and staged,
You can tell that it’s staged because it’s under the “Changes
new file: README
and now needs to be committed
ted” heading. If you commit at this point, the version of the file a ran git add is what will be in the historical snapshot. You may re
You can tell that it’s staged because it’s under the “Changes to be you ran git init earlier, you then ran git add (files) – tha
tw ted” heading. If you commit at this point, the version of the file at the tim
Staging Modified Files
Staging Modified Files (1)
Let’s change a file that was already tracked. If you change a previously tracked file called “CONTRIBUTING.md” and then run your git status command
Say, we modified the already tracked CONTRIBUTING.md file. again, you get something that looks like this:
When you run git status command, the output look like:
$ git status
On branch master Changes to be committed:
(use “git reset HEAD
new file: README
Changes not staged for commit:
(use “git add
(use “git checkout —
modified: CONTRIBUTING.md
The “COTNTraRIcBkUeTIdNGfi.mled”Cfoilne taprpieabrus tunidnerga.smedctihonansamed “Changes not been modified but NOT staged
staged for commit” – which means that a file that is tracked has been modified in the working directory but not yet staged. To stage it, you run the git add command. git add is a multipurpose command – you use it to begin tracking
new files, to stage files, and to do other things like marking merge-conflicted
new files, to stage files, and to do other things like marking merge-conflicted
files as resolved. It may be helpful to think of it more as “add this content to the next commit” rather than “add this file to the project”. Let’s run git add now
Staging Modified Files (2)
to stage the “CONTRIBUTING.md” file, and then run git status again:
$ git add CONTRIBUTING.md $ git status
On branch master
Changes to be committed:
(use “git reset HEAD
new file: README
modified: CONTRIBUTING.md
Both files are staged and will go into your next commit. At this point, sup-
Both files are now staged
pose you remember one little change that you want to make in CONTRIBU- TING.md before you commit it. You open it again and make that change, and you’re ready to commit. However, let’s run git status one more time:
$ vim CONTRIBUTING.md
$ git status
On branch master Changes to be committed:
(use “git reset HEAD
new file: README
Both files are staged and will go into your next commit. At this point, sup-
ke in CONTRIBU- )
TING.md before you commit it. You open it again and make that change, and you’re ready to commit. However, let’s run git status one more time:
Say, we forgot to change something in CONTRIBUTING.md file andmodifyitagain. Let’srungitstatuscommandagain:
$ vim CONTRIBUTING.md
$ git status
On branch master Changes to be committed:
(use “git reset HEAD
new file: README
modified: CONTRIBUTING.md
Changes not staged for commit:
(use “git add
(use “git checkout —
modified: CONTRIBUTING.md
If you run git commit, What the heck? Now CONTRIBUTING.md is listed as both staged and unstag-
What is going on?
which one will be committed?
ed. How is that possible? It turns out that Git stages a file exactly as it is when
TING.md as it was when you last ran the git add command is how it wil r
Staging Modified Files (4)
into the commit, not the version of the file as it looks in your working di
when you run git commit. If you modify a file after you run git add, yo to run git add again to stage the latest version of the file:
You need to run git add first:
$ git add CONTRIBUTING.md $ git status
On branch master
Changes to be committed:
(use “git reset HEAD
new file: README
modified: CONTRIBUTING.md
Short Status
While the git status output is pretty comprehensive, it’s also quite wo !16
also has a short status flag so you can see your changes in a more compa
fied output from the command:
Short Status
$ git status -s M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
Left column: file is staged; the right column: file is modified
NAew- fiAleddsedthtoatheasrteangi’ntgtarraecaked have a ?? next to them,
M – Modified
been added to the staging area have an A, modified files h
MM – Modified, staged, modified (contains changes that are both, There are two columns to the output – the left hand colum
staged and unstaged) ?? – non-tracked files
file is staged and the right hand column indicates that it’s
ample in that output, the README file is modified in the w
you can create a file listin Ignoring Files an example .gitignore
You may want to ignore some files, e.g. log files, system and temporary files, etc.
For this purpose, create a .gitignore file: ✦ WheredoIputthisfile?
The first line tells Git
$ cat .gitignore *.[oa]
You can put a .gitignore in any (and every) single directory of your project. archive files that may b
However, better practice is to have one tells Git to ignore all files global .gitignore the project root directory editors such as Emacs to
or pid directory; automat Also, possible to create one global local .ag.igtitiiggnorefile befor
file, that will manage all of your git repositories:
accidentally commit files e
$ git config –global core.excludesfile ~/.gitignore_global The rules for the patt
.gitignore file also need to be tracked and committed
• Blank lines or lin
• Standard glob patt
• You can start patterns
what you changed, not just which files were changed – you can use the git diff command. We’ll cover git diff in more detail later, but you’ll probably
Viewing Your Staged and Unstaged Changes (1)
use it most often to answer these two questions: What have you changed but
not yet staged? And what have you staged that you are about to commit? Al-
though git status answers those questions very generally by listing the file git diff command is used to see exactly what you
names, git diff shows you the exact lines added and removed – the patch, as have changed
Let’s say you edit and stage the README file again and then edit the CONTRI-
E.g. you edited and staged the README file again, and BUTING.md file without staging it. If you run your git status command, you
then edited the CONTRIBUTING.md file without staging it: once again see something like this:
$ git status
On branch master Changes to be committed:
(use “git reset HEAD
modified: README
Changes not staged for commit:
(use “git add
(use “git checkout —
modified: CONTRIBUTING.md
Viewing Your Staged and Unstaged Changes (2)
To see what you have changed but not yet staged, use To seecwohamt yomu’veachnandgedgbuit ntot yetdstiagefd,ftypecgoitmdimff waitnh ndo ,othw- hich compares the
er arguments:
data in your working directory with your staging area
$ git diff
diff –git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
— a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
Please include a nice description of your changes when you submit your PR;
if we have to read the whole diff to figure out why you’re contributing
in the first place, you’re less likely to get feedback and have your change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if your patch is
+longer than a dozen lines.
If you are starting to work on a particular area, feel free to submit a PR
git diff doesn’t that highlights your work in progress (and note in the PR title that it’s
That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you ha- ven’t yet staged.
If you want to see what you’ve staged that will go into your next commit, you
show all the changes since your last commit – only changes that are still unstaged
can use git diff –staged. This command compares your staged changes
Comparing Your Staged Changes With Your Last Commit
That command compares what is in your working directory with what is in
your staging area. The result tells you the changes you’ve made that you ha
ven’t yet staged.
To see what you have staged that will go
If you want to see what you’ve staged that will go into your next commit, you
into your next commit: use git diff —
can use git diff –staged. This command compares your staged change
toyosurtlastgcoemdmict:ommand:
$ git diff –staged
diff –git a/README b/README new file mode 100644
index 0000000..03902a1
— /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project
It’s important to note that git diff by itself doesn’t show all change
made since your last commit – only changes that are still unstaged. This can be
git diff doesn’t show all the changes since your last commit – only changes that are still unstaged
s confusing, because if you’ve staged all of your changes, git diff will give you
Viewing Your Staged and Unstaged Changes
Another example: stage the CONTRIBUTING.md file and then edit it, then use git diff to see
the staged and unstaged changes:
$ git add CONTRIBUTING.md
$ echo ‘# test line’ >> CONTRIBUTING.md $ git status
On branch master
Changes to be committed:
(use “git reset HEAD
modified: CONTRIBUTING.md
Changes not staged for commit:
(use “git add
(use “git checkout —
modified: CONTRIBUTING.md
Now you can use git diff to see what is still unstaged:
Recording Changes to the Reposit
Changes not staged for commit:
(use “git add
Viewing Your Staged and Unstaged Changes
(use “git checkout —
modified: CONTRIBUTING.md
Use git diff to see what is still Now you can use git diff to see what is still unstaged:
$ git diff
diff –git a/CONTRIBUTING.md b/CONTRIBUTING.md index 643e24f..87f08c8 100644
— a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,3 +119,4 @@ at the
## Starter Projects
See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).
+# test line
and git diff –cached to see what you’ve staged so far (–staged and — cached are synonyms):
$ git diff –cached
diff –git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644
— a/CONTRIBUTING.md
## Starter Projects
See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com