Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel...

31
1/20/13 Git tutorial file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 1/31 Git tutorial Mike Nolta

Transcript of Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel...

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 1/31

Git tutorial

Mike Nolta

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 2/31

1. Basics

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 3/31

Tell git who you are

The last line turns on color syntax highlighting.

$ git config --global user.name "Your Name"$ git config --global user.email [email protected]$ git config --global color.ui true

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 4/31

Initialize repository

Git stores everything in a single root .git/ directory, unlike svn, which puts a .svn/ dir in everysubdirectory.

Make a mistake? Want to start fresh? Just delete the toplevel .git/ directory.

$ mkdir /path/to/repo$ cd /path/to/repo$ git initInitialized empty Git repository in /path/to/repo/.git/

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 5/31

Status

$ git status# On branch master

#

# Initial commit

#

nothing to commit (create/copy files and use "git add" to track)

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 6/31

Make a change

$ echo "Some text" > README

$ git status# On branch master

#

# Initial commit

#

# Untracked files:

# (use "git add ..." to include in what will be committed)

#

# README

nothing added to commit but untracked files present (use "git add" to track)

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 7/31

Stage the change

$ git add README

$ git status# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use "git rm --cached ..." to unstage)

#

# new file: README

#

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 8/31

Commit the change

$ git commit -m "my first commit; added README"[master (root-commit) 1ae4083] my first commit; added README 1 file changed, 1 insertion(+) create mode 100644 README

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 9/31

History of changes

$ git logcommit 1ae40839800b9f7d7c3e1ce033cd9c496ec65141Author: Mike Nolta <[email protected]>Date: Wed Jan 16 15:06:36 2013 -0500

my first commit; added README

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 10/31

Make another change

$ echo "some more text" >> README

$ git diffdiff --git a/README b/READMEindex 7b57bd2..1f89c65 100644--- a/README+++ b/README@@ -1 +1,2 @@ some text+some more text

$ git add README$ git commit -m "added another line to README"[master 6102a1c] added another line to README 1 file changed, 1 insertion(+)

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 11/31

Now the history is...

$ git logcommit 6102a1c01cc1026acd27c21f83a47267870d6cdfAuthor: Mike Nolta <[email protected]>Date: Wed Jan 16 15:07:53 2013 -0500

added another line to README

commit 1ae40839800b9f7d7c3e1ce033cd9c496ec65141Author: Mike Nolta <[email protected]>Date: Wed Jan 16 15:06:36 2013 -0500

my first commit; added README

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 12/31

File lifecycle

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 13/31

Refering to commits

$ git show 1ae40839800b9f7d7c3e1ce033cd9c496ec65141commit 1ae40839800b9f7d7c3e1ce033cd9c496ec65141Author: Mike Nolta <[email protected]>Date: Wed Jan 16 15:06:36 2013 -0500

my first commit; added README

diff --git a/README b/READMEnew file mode 100644index 0000000..7b57bd2--- /dev/null+++ b/README@@ -0,0 +1 @@+some text

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 14/31

Working with commits

How has the code changed?

Retrieve commit:

Undo commit:

$ git diff commit

$ git checkout commit

$ git revert commit

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 15/31

Toss out all uncommited changes

$ git reset --hard # gets rid of all changes

$ git checkout -- filename # gets rid of changes in just filename

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 16/31

2. Branching & Merging

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 17/31

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 18/31

$ git branch testing

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 19/31

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 20/31

$ git checkout testing

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 21/31

$ git commit ...

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 22/31

$ git checkout master

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 23/31

$ git commit ...

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 24/31

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 25/31

$ git checkout master$ git merge iss53

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 26/31

3. Modifying history

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 27/31

Change the last commit's message

$ git commit --amend

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 28/31

Add a file to the last commit

$ git add filename$ git commit --amend

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 29/31

Uncommitting

$ git reset HEAD~1 # undoes the last commit

$ git reset HEAD~2 # undoes the last 2 commits

$ git reset HEAD~2 --hard # '' and discards changes

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 30/31

Learning more

http://git-scm.com/book

http://www-cs-students.stanford.edu/~blynn/gitmagic/

http://www.sbf5.com/~cduan/technical/git/

1/20/13 Git tutorial

file:///Users/nolta/github/reveal.js/git.html?print-paper#/ 31/31