How to use git? Contents Jiang Lin

How to use git?
Jiang Lin∗†
This version: 03/18/2014
Contents
1 Why git?
4
2 Install git
4
3 Overview
5
4 Initial config
5
5 First project
5
5.1
Repository hosting . . . . . . . . . . . . . . . . . . . . . . . . . .
5
5.2
Clone remote repository . . . . . . . . . . . . . . . . . . . . . . .
6
5.3
Ignore list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
5.4
Add files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
5.5
Show changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
5.6
First commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
5.7
First push . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
5.8
Back to the beginning . . . . . . . . . . . . . . . . . . . . . . . .
6
5.9
Remove git repository . . . . . . . . . . . . . . . . . . . . . . . .
7
6 Git maintenance
6.1
∗ email:
7
Local vs remote . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
6.1.1
basic steps
. . . . . . . . . . . . . . . . . . . . . . . . . .
7
6.1.2
git remote origin . . . . . . . . . . . . . . . . . . . . . . .
7
[email protected];
imlinj.github.io.
† homepage:
1
6.1.3
additional tools . . . . . . . . . . . . . . . . . . . . . . . .
7
6.1.4
change remote URL . . . . . . . . . . . . . . . . . . . . .
7
Tracked files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
6.2.1
list contents . . . . . . . . . . . . . . . . . . . . . . . . . .
8
6.2.2
update .gitignore . . . . . . . . . . . . . . . . . . . . . . .
8
Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
6.3.1
list branches . . . . . . . . . . . . . . . . . . . . . . . . .
8
6.3.2
new branches . . . . . . . . . . . . . . . . . . . . . . . . .
8
6.3.3
push to remote . . . . . . . . . . . . . . . . . . . . . . . .
9
6.3.4
copy files across
. . . . . . . . . . . . . . . . . . . . . . .
9
6.3.5
merge a branch . . . . . . . . . . . . . . . . . . . . . . . .
9
6.3.6
delete a branch . . . . . . . . . . . . . . . . . . . . . . . .
9
6.3.7
compare branches . . . . . . . . . . . . . . . . . . . . . .
9
Stash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
6.4.1
what is stash . . . . . . . . . . . . . . . . . . . . . . . . .
9
6.4.2
discard unstaged changes . . . . . . . . . . . . . . . . . .
9
Compare commits . . . . . . . . . . . . . . . . . . . . . . . . . .
9
6.5.1
compare a specific file . . . . . . . . . . . . . . . . . . . .
9
Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
6.6.1
fetch/merge vs. pull . . . . . . . . . . . . . . . . . . . . .
10
6.6.2
mergetool . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
6.6.3
solve merge conflicts . . . . . . . . . . . . . . . . . . . . .
10
6.7
Export commits . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
6.8
Revert commits . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6.8.1
git diff commits difference . . . . . . . . . . . . . . . . . .
11
6.8.2
change HEAD
. . . . . . . . . . . . . . . . . . . . . . . .
11
6.8.3
revert commits . . . . . . . . . . . . . . . . . . . . . . . .
11
Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6.9.1
create a tag . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6.9.2
list all tags . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6.9.3
checkout a specific tag . . . . . . . . . . . . . . . . . . . .
11
6.9.4
git describe . . . . . . . . . . . . . . . . . . . . . . . . . .
11
6.2
6.3
6.4
6.5
6.6
6.9
2
7 Git with SSH
11
7.1
SSH with Windows . . . . . . . . . . . . . . . . . . . . . . . . . .
12
7.2
Set up SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
7.3
Configure local repository . . . . . . . . . . . . . . . . . . . . . .
12
8 Miscellaneous
12
8.1
Help Documentation . . . . . . . . . . . . . . . . . . . . . . . . .
12
8.2
Take a try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
8.3
git-rev-parse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
8.4
GitLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
8.5
Gitweb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
8.6
master vs testing . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
8.7
https vs git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
8.8
git-extras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
8.9
Create build numbers . . . . . . . . . . . . . . . . . . . . . . . .
13
8.10 index.lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
8.11 A sample .gitignore file . . . . . . . . . . . . . . . . . . . . . . . .
14
3
1
Why git?
• Version control is an essential part of research.
– What is version control?
The management of changes to documents, programme source codes,
and other forms of text-based information.
– What can a version control system do?
∗ Keep track of editing history;
∗ Coordinate team collaboration.
• What is git?
– Git is a distributed version control system1 ;
– A tool to implement version control efficiently.
• What does “distributed” mean?
– Local machines keep a full history of versions;
– No need to get connected to a server.
Git was initially designed and written in 2005 by Linus Torvalds, the creator of
Linux, for the convenience of maintaining the Linux kernel, so far the biggest
OpenSource project on earth. Since then, git has been used to help organize
the collaboration of authors from all over the world working on Linux, very
efficiently2 .
Take-away message
• For not-so-heavy users other than IT programmers, git can be used to:
1.
2.
3.
4.
2
track revisions of your codes/documents (text-based3 );
backup version history;
sync documents across local and remote machines;
make collaboration with your co-authors easier.
Install git
Windows users could download installation files form official homepage: www.gitscm.com4 .
Under Linux: (1) Arch Linux: sudo pacman -S git; (2) Debian/Ubuntu: sudo
apt-get install git.
1 The
opposite is the “centralized version control”, CVS.
lines added; 1800 lines removed; 1500 lines modified per day from 2007-2008”,
according to Greg Hartman’s talk on the development of Linux Kernel.
3 To confirm this, check whether the documents could be open by a text editor like gvim
or notepad.
4 SCM is short for “Source Code Management”.
2 “4300
4
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Overview
usage : git [ - - version ] [ - - help ] [ - C < path >] [ - c name = value ]
[ - - exec - path [= < path >]] [ - - html - path ] [ - - man - path ] [ - - info - path ]
[ - p | - - paginate | - - no - pager ] [ - - no - replace - objects ] [ - - bare ]
[ - - git - dir = < path >] [ - - work - tree = < path >] [ - - namespace = < name >]
< command > [ < args >]
The most commonly used git commands are :
add
Add file contents to the index
bisect
Find by binary search the change that introduced a bug
branch
List , create , or delete branches
checkout
Checkout a branch or paths to the working tree
clone
Clone a repository into a new directory
commit
Record changes to the repository
diff
Show changes between commits , commit and working tree , etc
fetch
Download objects and refs from another repository
grep
Print lines matching a pattern
init
Create an empty Git repository or reinitialize an existing one
log
Show commit logs
merge
Join two or more development histories together
mv
Move or rename a file , a directory , or a symlink
pull
Fetch from and integrate with another repository or a branch
push
Update remote refs along with associated objects
rebase
Forward - port local commits to the updated upstream head
reset
Reset current HEAD to the specified state
rm
Remove files from the working tree and from the index
show
Show various types of objects
status
Show the working tree status
tag
Create , list , delete or verify a tag object signed with GPG
’ git help -a ’ and ’ git help -g ’ lists available subcommands and some
concept guides . See ’ git help < command > ’ or ’ git help < concept > ’
to read about a specific subcommand or concept .
4
Initial config
git config –global user.name your name
git config –global user.email [email protected]
git config –global core.editor vim 5
git config –global merge.tool vimdiff
git config –list
5
First project
5.1
Repository hosting
Github: free for public project;
Bitbucket: free for both public and private project.
5 Under
Windows, try: git config –global core.editor notepad .
5
5.2
Clone remote repository
Clone via https:
git clone https://[email protected]/HelloLj/replicatemodel/
Clone via SSH:
[email protected]:HelloLj/replicatemodel.git
Git clone to a specific directory:
git clone [email protected]:flazz/vim-colorschemes.git bundle/colorschemes
5.3
Ignore list
Local:
.gitignore
Global:
git config –global core.excludesfile ’~/.gitignore’
A local ignore list is recommended, which is useful when editing from other
remote machines (by doing that, you can easily sync the ignor file with the
repository together).
5.4
Add files
git add –all
5.5
Show changes
git diff
git diff #commit1 #commit2
5.6
First commit
git commit -m “repository initialization”
5.7
First push
git push origin master
5.8
Back to the beginning
git init
git remote add origin [email protected]:HelloLj/learnjulia.git
git push origin master
6
5.9
Remove git repository
rm -rf .git or delete .git folder directly.
Be very cautious!
6
Git maintenance
6.1
6.1.1
Local vs remote
basic steps
1. stage: git add
2. commit: git commit
3. push: git push
local: 1 and 2.
remote: 3.
6.1.2
git remote origin
What is origin?
6.1.3
additional tools
git merge ...
git pull ...
6.1.4
change remote URL
1. Say I had a repository that I have mistakenly set the remote url. To check
this, do the git info . It returns the following,
## Remote URLs:
origin https://[email protected]/linjecon/linjecon.jekyll.git (fetch)
origin https://[email protected]/linjecon/jekyll.pages.git (push)
So, I have different origin url’s for the fetch and push labels. Sometimes
it is exactly what you want, however for most cases it is not. In order to
fix this, I have to run the following line:
git remote set-url origin https://[email protected]/linjecon/jekyll.pages.git
Do the git info line again, which gives,
## Remote URLs:
origin https://[email protected]/linjecon/jekyll.pages.git (fetch)
origin https://[email protected]/linjecon/jekyll.pages.git (push)
7
2. A more tedious/inefficient way could be as follows,
git remote rm origin #Removes old origin
git remote add origin https://[email protected]/your.new.repo #Adds
new origin pointing to BitBucket
git push -u origin #Pushes commits to new repo
6.2
6.2.1
Tracked files
list contents
http://stackoverflow.com/questions/424071/list-all-the-files-for-a-commit-in-git
git ls-tree –name-only HEAD
git ls-tree –name-only HEAD -r
git ls-files
6.2.2
update .gitignore
http://stackoverflow.com/questions/1274057/making-git-forget-about-a-file-thatwas-tracked-but-is-now-gitignored
git tracks any files that have been cached. This means newly updated .gitignore
files has no effect on relevant files cached previousely. Instead, one has to remove
those files manully.
git rm –cached *.aux
or
git rm -cached . and git add .
Be cautiouse when doing the git rm command. Try git rm –cached *.aux -n to
do a dry run.
6.3
6.3.1
Branches
list branches
local: git branch
local & remote: git branch -a
6.3.2
new branches
git branch testing
git checkout testing
“checkout”: it dose not mean “getting out”, instead it means “have a look at it”,
i.e., “check-in”.
8
6.3.3
push to remote
git push origin master
git push origin testing
6.3.4
copy files across
Copy a file form master branch to the testing branch:
git checkout testing
git checkout master – notebook/RMagic.ipynb
git branch -a # check all branches, both local and remote.
6.3.5
merge a branch
Merge branch testing to master:
git checkout master
git merge testing
6.3.6
delete a branch
git branch -d testing
6.3.7
compare branches
Compare local/master and remote/testing
git diff master origin/testing
6.4
Stash
6.4.1
what is stash
6.4.2
discard unstaged changes
6.5
Compare commits
git diff #commit1 #commit2
6.5.1
compare a specific file
git diff master~1:Note-UTF.tex Note-UTF.tex > diff.tmp
git diff 4d8d54d4:Note-UTF.tex f018329a:Note-UTF.tex
git diff f018329a Note-UTF.tex
git log -p -1 Note-UTF.tex
9
6.6
Merge
http://svn.cs.swarthmore.edu/cs40/wiki/git/merge
6.6.1
fetch/merge vs. pull
...
6.6.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
mergetool
$ git pull
...
CONFLICT ( content ) : Merge conflict in plugins / go / go . plugin . zsh
...
Auto - merging plugins / battery / battery . plugin . zsh
Auto - merging lib / termsupport . zsh
Removing lib / rvm . zsh
Removing lib / rbenv . zsh
...
Automatic merge failed ; fix conflicts and then commit the result
$ git mergetool
Merging : plugins / go / go . plugin . zsh
Symbolic link merge conflict for ’ plugins / go / go . plugin . zsh ’:
{ local }: modified file
{ remote }: a symbolic link -> ’../ golang / golang . plugin . zsh ’
Use ( l ) ocal or ( r ) emote , or ( a ) bort ? r
$ ...
6.6.3
solve merge conflicts
Assume there is a merge conflict with filename.c. There are three options to
solve the conflict:
1. keep local changes:
$ git checkout –ours filename.c
2. adopt remote changes:
$ git checkout –theirs filename.c
3. mix both versions by hand and then add the merged result:
$ git add filename.c
$ git commit -m "mixed changes from both local and remote"
6.7
Export commits
git archive –format zip –output /full/path/to/zipfile.zip 29435bc
10
6.8
Revert commits
On one branch, there usually are many a history of commites along the time.
When doing git checkout, git treats those commits as if they are different
branches.
Switching between diffent commits is usually callled changing HEAD in git,
where HEAD acts like a pointer pointing to a specific commit that you are
currently working with on the timeline of a branch .
6.8.1
git diff commits difference
6.8.2
change HEAD
git log
git checkout f018329a
6.8.3
revert commits
6.9
Tags
6.9.1
create a tag
git tag -a v1.4 -m ’create version 1.4’
6.9.2
list all tags
git tag
git tag -l ’v1.2.’
6.9.3
checkout a specific tag
git clone [email protected]:julialang/julia.git
git tag -l
git checkout tags/v0.2.0
6.9.4
git describe
http://git-scm.com/docs/git-describe
7
Git with SSH
SSH is short for “secure shell”. By using SSH, one can conveniently log into
online git hosting services without typing username and password repeatedly.
Both github.com and bitbucket.org support SSH login.
11
7.1
SSH with Windows
Three options:
1. Install Cygwin; in Cygwin, install OpenSSH.
2. Install Github client for windows, and once installed, OpenSSH should be
available in Gitshell;
3. Install Gitbash, OpenSSH would have already been included.
7.2
Set up SSH
Three steps:
1. Get bash terminal
2. ssh-v
3. ssh-keygen
4. copy ~/.ssh/id_ras.pub to Github or Bitbucket
7.3
Configure local repository
switch to SSH from https:
git remote set-url https:\\[email protected]\repository.git
and switch it back to SSH:
git remote set-url [email protected]:username\repository.git
8
8.1
Miscellaneous
Help Documentation
http://git-scm.com/doc;
GitImpression
8.2
Take a try
try.github.io/
8.3
git-rev-parse
https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html
12
8.4
GitLab
GitLab (https://www.gitlab.com/) is an OpenSource project with which users
could build their own git hosting server. It is particularly suitable for enthusiastic academic teams/institutions and startup firms.
8.5
Gitweb
Build simple HTTP server for git repository browsing: http://git-scm.com/book/en/Giton-the-Server-GitWeb.
1. install HTTP webserver, e.g., lighttpd or WEBrick;
2. [optional] modify .git/description to add custom description of the repositiory;
3. git instaweb
8.6
master vs testing
Assume that you have created two branches for your thesis: master and testing
with a consideration that “master” branch is used for milestone updates while
“testing” is just for some daily maintenance.
Make sure only checkout master branch when you want to update it.
Do not try to update files when checking out the master branch, otherwise it
may cause some conflicts, though usually dose not make a big fuzz. But for a
good manner of branch management, those could be avoided.
8.7
https vs git
If you are behind a firewall and you need to use the https protocol instead of
the git protocol:
git config –global url."https://".insteadOf git://
8.8
git-extras
git-extras is a set of utilities that provides extra tools for the use of git.
Using git-extras under Widows would be a little bit tricky. One can install gitextras in MinGW or Cygwin environment. As an alternative, see here for the
installation in Windows.
8.9
Create build numbers
http://blog.marcingil.com/2011/11/creating-build-numbers-using-git-commits/
13
8.10
1
index.lock
fatal : Unable to create ’/. git / index . lock ’: File exists . If no other git
process is currently running , this probably means a git process crashed
in this repository earlier . Make sure no other git process is running
and remove the file manually to continue .
Solution: (1) check all other git processes have been terminated; (2) if (1) is
confirmed, rm .git/index.lock.
8.11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#
A sample .gitignore file
Edited based on a sample file from : https :// help . github . com / articles /
ignoring - files by LJ
. ipynb_checkpoints /
# Compiled source #
# ##################
*. com
*. class
*. dll
*. exe
*. o
*. so
# Packages #
# ###########
# it ’ s better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7 z
*. dmg
*. gz
*. iso
*. jar
*. rar
*. tar
*. zip
# Logs and databases #
# #####################
*. log
*. sql
*. sqlite
# OS generated files #
# #####################
. DS_Store
. DS_Store ?
._*
. Spotlight - V100
. Trashes
ehthumbs . db
Thumbs . db
14