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

Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

May 25, 2018

Download

Documents

hacong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

Git tutorial

Mike Nolta

Page 2: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

1. Basics

Page 3: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 4: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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/

Page 5: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "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)

Page 6: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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)

Page 7: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

#

Page 8: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 9: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 10: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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(+)

Page 11: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 12: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

File lifecycle

Page 13: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 14: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 15: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 16: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

2. Branching & Merging

Page 17: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

Page 18: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git branch testing

Page 19: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

Page 20: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git checkout testing

Page 21: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git commit ...

Page 22: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git checkout master

Page 23: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git commit ...

Page 24: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

Page 25: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

$ git checkout master$ git merge iss53

Page 26: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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

3. Modifying history

Page 27: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 28: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 29: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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

Page 30: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

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/

Page 31: Git tutorial - SciNetWiki · 1/20/13 Git tutorial ... Want to start fresh? Just delete the toplevel .git/ directory. ... nothing added to commit but untracked files present (use "git

1/20/13 Git tutorial

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