You are on page 1of 36

98-174

Modern Version Control with Git

Basic Git Commands


Costas Akrivoulis
January 23, 2013

Review from last lecture

Course logistics
What is version control?
Why use a VCS (motivation)?
Brief history / evolution of version control
What is Git? Where does it fit in?

Our first command git clone

Agenda

Administration
Configuring Git
Basic git commands
File states

Getting Git
git clone git://github.com/gitster/git.git
Sorry for the confusion

Please use ~/public/98174/


Not ~/Public/, not ~/98174/

Git Basics
Configuring Git
git config

Initializing a repository
git init, clone

Inspecting a repository
git status, log, diff

Modifying a repository
git add, rm, mv, commit

Getting Help

git help [<cmd>]

man git-[<cmd>] usually does the same

(use between multi-word commands)

Google, Stack Overflow

Additional reading

Pro Git (http://progit.org/) [ excellent free online book ]


Tons of Git tutorials online

Email me!

Configuring Git

Git has its own configuration settings that


control things like:

Author identity (name, email)

Editor, merge tool

Automation (line endings, scripts)

Etc.

Useful for you and others that work with your repo

Configuring Git

git config [--global] <key> <value>

--global sets the information to persist

across a users repos


Usually what you want
does your name change often?

Configuring Git

git config --global user.name Costas Akrivoulis

git config --global user.email costas@cmu.edu

git config --global core.editor vim

git config [--global] -list

shows what keys are set

Configuring Git
Color!

git config --global color.status auto

git config --global color.diff auto

git config --global color.branch auto

Getting a repository
Two ways of getting a repository
1. Cloning someone elses
2. Making your own

Either way will make a secret .git folder


This is the root of the Git project universe

Git stores everything describing the repo there


Configuration files, all version tracked objects, etc.

Getting a repository git clone


Clone someone elses!
git clone < url | path > [folder_name]
git clone git://github.com/gitster/git.git
completely* copies the entire history of a project

Clone supports many protocols


local: filesystem, and remote: HTTP(S), ssh, git

Useful for getting the source code / history for


(OSS) projects

Getting a repository git init

Make your own!

git init

Can run even if there are already files/folders

Warning (!) Those files are not yet being tracked!

git status

Information about repo

You may find yourself spamming it sometimes

What files are new, changed or which have been


deleted!
Basically like ls for Git
So often that you might want to make an alias
(.bashrc)
$ alias gs=git status

Reports info about file states too

File States

Untracked

Unmodified

Modified

Staged

Git has no idea what


this is

File is being tracked,


hasn't been changed
Tracked, changed
since last commit
Will be saved at next
commit

Adding files to the repository

git add [-u] [ files | dirs ]

Tells Git to add files to the staging area (index)

Being staged means it'll get saved the next time


you make a commit (a snapshot of your repo)
Can add folders or files, even those that are
already added

Adding files to the repository

Add all C source files in the current folder

Add all C source files in the current folder and all


subfolders

git add *.c

git add \*.c

Add everything (be careful!)

git add .

git commit

Saves a snapshot of the current repo


Staged files are now considered unmodified
files, they are being tracked by Git if they
weren't already before
Need to write in a comment for the commit
when you run the command

git commit

Commit everything that's staged

Stage all tracked files and commit in one go


(doesn't affect untracked files)

git commit

git commit -a

Hate opening up the editor to type one line?

git commit -m <comment>

git commit

If you make a commit and immediately realize


that you forgot to include a change/misspelled
your comment/didn't want to do that at all etc.

Change the working directory as you wanted it for


the last commit

git commit --amend

Only works for previous commit, we'll see more


powerful commands in a later lecture

git rm

Removes files from being tracked by Git

Caution (!), deletes the file by default!

git rm

git rm <file>

Removes <file> from Git and deletes it as well

git rm --cached <file>

Leaves it hanging in the directory, it's just gone


from Git

git mv

Moving or renaming a file


Stages the change, you still need to do a
commit!
git mv <src> <dest>

Logging

Every time we made a commit we saved our


changes and a message
Now that we've got some commits, we can
look through our history!

git log

git log

git log <path>

Lists entire commit history, starts from most recent

Only shows commits that affected files in <path>

Has flags to show commits between certain


dates, authors, specific messages etc.

Check git help log

diff'ing

Have a repo, changed some files a while ago


We want to see the difference between what
we changed and the repo's most recent
commit

git diff

git diff

git diff --cached

Diff between working directory and staged files

Diff between staged files and repo

Like git log, has ten billion flags for


controlling output

Annoying Untracked Files

Case study: Alex is using Git to manage his


programming project
Project causes lots of object files to be built that
Alex doesn't want to save

If he saves them, they will change nonstop and Git


will keep telling him to add + commit them
If he doesn't save them, then git status will never
shut up about untracked files

.gitignore

Simple text file you can put in any directory


under your project folder
Has a list of patterns that Git will use to
ignore files or even folders
Since its a file, it can also be tracked by Git!

.gitignore
[alex@pavilion c0co]$ cat .gitignore
*.swp
*.c0
*.o
*.hi
*.s
*.old
*.ll
a.out
a.result
a.output

No more commands!
Phew, thats it for now
Lot to take in, obviously cant remember all of
them
Best way to learn is to actually USE the
commands

Homework 2
Create a new repository at:
~/public/98174/hw2

Make 2 new text files, foo.txt and bar.txt


Commit them (initial commit)

Change foo.txt somehow


Commit that

Remove bar.txt from your repo


Commit that

Summary
Configuring Git
git config

Initializing a repository
git init, clone

Inspecting a repository
git status, log, diff
File states

Modifying a repository
git add, rm, mv, commit

Thank you!
Questions?

You might also like