Version Control Systems (Part 2) Devin J. Pohly - - PowerPoint PPT Presentation

version control systems part 2
SMART_READER_LITE
LIVE PREVIEW

Version Control Systems (Part 2) Devin J. Pohly - - PowerPoint PPT Presentation

Systems and Internet i Infrastructure Security i Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Version Control Systems (Part 2) Devin J.


slide-1
SLIDE 1

CMPSC 311: Introduction to Systems Programming Page 1

Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA

Systems and Internet Infrastructure Security

i i

Version Control Systems (Part 2)

Devin J. Pohly <djpohly@cse.psu.edu>

slide-2
SLIDE 2

Page 2 CMPSC 311: Introduction to Systems Programming

Goal for today

  • I’ll have a walkthrough
  • Feel free to play with

things as you go along

  • There may be time for

exploration at the end

slide-3
SLIDE 3

Page 3 CMPSC 311: Introduction to Systems Programming

First things first

  • Git uses a name and email to identify the author of each commit
  • So you need to let it know who you are
  • Note: all Git commands are run by the program git, and the

first argument is the command

  • I will leave out the “git” part in bullet points, e.g., “we will use the

config command to set up a name and email.”

git config --global user.name "Your Name" git config --global user.email "foo4242@psu.edu"

slide-4
SLIDE 4

Page 4 CMPSC 311: Introduction to Systems Programming

Clone a repository

  • Let’s start by making a clone rather than creating our own

repository.

  • This is done using the clone command and the URL for a repository.
  • We’ll make a clone for Bob too. Don’t worry, we get to be Alice first.
  • Note: Git will automatically create a working copy when cloning

by checking out the head revision.

git clone https://github.com/djpohly/text.git git clone text bob

slide-5
SLIDE 5

Page 5 CMPSC 311: Introduction to Systems Programming

Make some changes

  • This is your own copy, so you won’t hurt anything!
  • The original version is safely kept in your repository.
  • Go ahead, insert some nonsense into a song.
  • To see a list of what files have changed, use the status

command.

cd text vim frozen.txt git status

slide-6
SLIDE 6

Page 6 CMPSC 311: Introduction to Systems Programming

More detail, please

  • To see exactly what changes have been made, use the

diff command.

  • The output of this command is called a “diff” or a “patch,”

and it’s one way of sharing your changes with someone else, especially if they don’t have a Git repository.

git diff

slide-7
SLIDE 7

Page 7 CMPSC 311: Introduction to Systems Programming

Gold star I tried

  • Try to check in your changes using the commit

command.

  • What happens? What does Git say?
  • What does the status command tell you about your

changes? git commit git status

slide-8
SLIDE 8

Page 8 CMPSC 311: Introduction to Systems Programming

Git’s staging area (index)

  • Changes aren’t committed

by default

  • Instead, you stage them
  • To stage changes: add
  • To unstage: reset
  • To be even more selective,

give the -p (patch) flag

  • Lets you decide exactly

what goes into a commit

  • Clean commits
  • Understandable history
slide-9
SLIDE 9

Page 9 CMPSC 311: Introduction to Systems Programming

For real this time

  • Add your changes to the staging area and commit them.
  • Never be afraid to commit. You can always undo it later.
  • Describe what you changed in the commit message.
  • Commit message format: one line summary, a blank line, and

then any further description needed.

git add frozen.txt git commit

slide-10
SLIDE 10

Page 10 CMPSC 311: Introduction to Systems Programming

Okay, now what?

  • Does status show your changes anymore?
  • Use the log command to see the entire history.
  • Hey, there’s your commit. Nice job!
  • The -p (patch) flag will show exactly what changed, kind of

like a combined log/diff.

git status git log

slide-11
SLIDE 11

Page 11 CMPSC 311: Introduction to Systems Programming

Commit IDs

  • Look at the log output again...
  • No simple 1, 2, 3 revision numbers!
  • It’s actually impossible in a distributed VCS to assign numbers like this

that will be the same for everyone.

  • Git uses a hash: that bunch of hex digits you see after “commit”
  • Git lets you abbreviate these to the first 4-6 characters. Try it!

git log git log ba4f

slide-12
SLIDE 12

Page 12 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • Multiple lines of development aren’t necessarily multiple people!
  • We can create a branch locally with the branch command.

D Repository master

slide-13
SLIDE 13

Page 13 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • This creates newidea, but master is still the current branch.
  • Type the branch command with no arguments to see which

branch we are currently on.

D Repository master newidea git branch newidea

slide-14
SLIDE 14

Page 14 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • When we make a commit, the current branch follows

along to track our progress.

D E Repository master newidea git commit

slide-15
SLIDE 15

Page 15 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • Suppose we want to work on that new idea now.
  • We can switch branches with git checkout.

D E F Repository master git commit newidea

slide-16
SLIDE 16

Page 16 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • Git checks out revision D into our working copy and

makes newidea the current branch.

D E F Repository git checkout newidea newidea master

slide-17
SLIDE 17

Page 17 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • Any new commits now update the newidea branch.
  • Notice that other branches are left alone.

D E G F Repository master newidea git commit

slide-18
SLIDE 18

Page 18 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • We can continue working on newidea even if there are
  • ther commits on master.

D E G F Repository master newidea H git commit

slide-19
SLIDE 19

Page 19 CMPSC 311: Introduction to Systems Programming

Local branches in Git

  • Just a reference to the tip
  • f the branch
  • Work on multiple ideas

simultaneously

  • Follow other developers’

repositories

  • Save some temporary

changes and throw them away later

  • Many other possibilities
slide-20
SLIDE 20

Page 20 CMPSC 311: Introduction to Systems Programming

Make a local branch

  • If you haven’t already, make a newidea branch and check it out
  • PROTIP: you can combine this using checkout -b.
  • Add a new file “hello” and commit it.
  • Check out master and notice the file isn’t there.
  • Check out newidea and your changes are back.

git checkout -b newidea vim hello git add hello git commit git checkout master git checkout newidea

slide-21
SLIDE 21

Page 21 CMPSC 311: Introduction to Systems Programming

Bob’s turn

  • OK, let’s pretend to be Bob for a moment. Change

directories into his copy.

  • Bob adds a file called “foo” and commits it.

cd ../bob vim foo git add foo git commit

slide-22
SLIDE 22

Page 22 CMPSC 311: Introduction to Systems Programming

Poor Bob... always second

  • Now Bob is going to try to push his changes to Alice’s repository.
  • Go ahead, try the push command. What does Git say?
  • Remember: you can only push a new revision if it is a descendant
  • f the existing one!
  • Git calls this a “fast-forward” because all it has to do is move the branch

reference forward along the commit graph.

git push

slide-23
SLIDE 23

Page 23 CMPSC 311: Introduction to Systems Programming

Pull first

  • Bob needs to use pull to get Alice’s commits first.
  • Note: Git’s pull command will attempt to merge the changes automatically. To

avoid this, use fetch instead.

  • Now take a look at the commit graph (--oneline gives short descriptions
  • nly): the merge revision has two parents, and one is the tip from Alice’s

repository.

  • Bob’s latest revision is a descendant of Alice’s, so he can push now!

git pull git log --graph --oneline git push

slide-24
SLIDE 24

Page 24 CMPSC 311: Introduction to Systems Programming

Back to Alice

  • Recall Alice has been working on newidea.
  • Bob pushed to the master branch, so it wasn’t affected.
  • She decides it’s ready to be an official part of master.
  • First switch to the master branch, then use merge to bring in the

commits from newidea.

  • Now master has both “hello” from newidea and “foo” from Bob!

cd ../text git checkout master git merge newidea ls

slide-25
SLIDE 25

Page 25 CMPSC 311: Introduction to Systems Programming

Cleaning up

  • Take a look at the commit graph now.
  • All of the newidea commits are part of master since we

merged the branches.

  • We don’t need newidea anymore, so we can delete it

with branch -d. git log --graph --oneline git branch -d newidea

slide-26
SLIDE 26

Page 26 CMPSC 311: Introduction to Systems Programming

Regret and blame

  • You know, I shouldn’t have added a title to Mending Wall.

None of the other files have titles. Let’s undo that.

  • First we have to find out what commit we want to undo. Let’s

use the blame command.

  • What’s the ID of the commit in which the title was added?

git blame frost.txt

slide-27
SLIDE 27

Page 27 CMPSC 311: Introduction to Systems Programming

Undoing mistakes

  • OK, so we want to revert commit 1eb8.
  • This will actually create a new commit which undoes

the old one. None of the history is lost.

  • For example, you can revert the revert to get it back.

git revert 1eb8 git log

slide-28
SLIDE 28

Page 28 CMPSC 311: Introduction to Systems Programming

One last first command

  • We started by cloning an

existing repository

  • To set up a new repository

in a directory, use the init command.

  • To convert an existing

directory:

  • Change to it.
  • git init.
  • git add any files you want

Git to track.

  • git commit.
slide-29
SLIDE 29

Page 29 CMPSC 311: Introduction to Systems Programming

Best practices

  • One change per commit
  • Small commits
  • Easy to isolate problems
  • Easy to revert mistakes
  • Update your code often
  • Communicate!
  • Version control is a great

collaborative tool, but it doesn’t replace actual teamwork!

slide-30
SLIDE 30

Page 30 CMPSC 311: Introduction to Systems Programming

Good sources

  • I referred to a number of these when making this lecture;

you may find them helpful in learning VCS/Git:

  • Hands-on

◾ Try Git: try.github.io ◾ Learn Git Branching: pcottle.github.io/learnGitBranching ◾ Git Immersion (with Ruby scripts): gitimmersion.com

  • Video presentation (from linux.conf.au conference)

◾ Git for Ages 4 and Up: youtu.be/1ffBJ4sVUb4

  • Web

◾ Git Magic:

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

◾ Pro Git: git-scm.com/book ◾ Version Control by Example: ericsink.com/vcbe

slide-31
SLIDE 31

Page 31 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Caveat: Subversion and

Git are fundamentally different!

  • These commands are

similar, but not always equivalent.

  • The reference focuses on

the practical aspects

  • Help Subversion users

make the switch

slide-32
SLIDE 32

Page 32 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • First, the most important command of all:

svn help git help svn help COMMAND git help COMMAND

slide-33
SLIDE 33

Page 33 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Get the source from a remote location:

svn checkout URL git clone URL

  • Update my existing copy with the latest changes:

svn update git pull

  • (Remember: git pull is the same as fetch+merge)
slide-34
SLIDE 34

Page 34 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Add a new file in the next commit:

svn add foo.c git add foo.c

  • Remove a file in the next commit:

svn rm foo.c git rm foo.c

slide-35
SLIDE 35

Page 35 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Summarize the files I’ve changed but haven’t yet

committed: svn status git status

  • Show me exactly what I’ve changed but haven’t yet

committed: svn diff git diff

slide-36
SLIDE 36

Page 36 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Undo my uncommitted changes to a file:

svn revert foo.c git checkout foo.c

  • Undo an earlier commit that was a mistake:

svn merge -c -REVNUM; svn commit git revert REVID

slide-37
SLIDE 37

Page 37 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Commit all the changes I’ve made to my working copy

and send them to the remote repository: svn commit git commit -a; git push

  • Commit all local changes without sending to the

remote repository (Git only): git commit -a

slide-38
SLIDE 38

Page 38 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Staging area (index) is Git-only!
  • Stage certain changes and commit only those:

git add foo.c foo.h ... git commit

  • Interactively choose changes to stage (cool!):

git add -i

  • Unstage the changes made to a particular file:

git reset foo.c

slide-39
SLIDE 39

Page 39 CMPSC 311: Introduction to Systems Programming

SVN-Git reference

  • Show a list of all commits made, most recent first:

svn log git log

  • Show line-by-line history of a file, including who

changed what and when: svn blame foo.c git blame foo.c

slide-40
SLIDE 40

Page 40 CMPSC 311: Introduction to Systems Programming

Other Git commands

  • Create a new lightweight local branch:

git branch BRANCHNAME

  • Create a new branch and switch to it:

git checkout -b BRANCHNAME

  • Switch to an existing branch:

git checkout BRANCHNAME

  • Merge another branch into the current one:

git merge OTHERBRANCH