You are on page 1of 30

Intro to Git

Purdue ACM SigApp

Workflow

Git is Distributed Version Control

Vs. Centralized Version Control

Staging Area

Staging Area

Setup

Setup Name and Email


$ git config --global user.name Your Name $ git config --global user.email your_email@whatever.com

Setup Line Ending Preferences


On Linux/Mac: $ git config --global $ git config --global On Windows: $ git config --global $ git config --global

core.autocrlf input core.safecrlf true core.autocrlf true core.safecrlf true

Create A Project

Create a git repository from scratch


$ mkdir hello # Make a new directory hello $ cd hello # Change directory into hello

(create a file) hello.c


#include<stdio.h> int main() { printf(Hello, World!\n); return 0; }

Create the Repository


$ git init Initialized empty Git repository in /homes/etemplin/git/hello/.git/

Add the program to the repository


$ git add hello.c $ git commit -m First Commit
[master (root-commit) 9416416] First Commit 1 files changed, 6 insertions(+), 0 deletions(-) create mode 100644 hello.c

Check the status of the repository


$ git status # On branch master nothing to commit (working directory clean)

Change the Hello, World program.


#include<stdio.h> int main() { printf(Hello, (your name here)!\n); return 0; }

Check the status


$ git status # On branch master # Changes not staged for commit: # (use git add <file>... to update what will be committed) # (use git checkout -- <file>... to discard changes in working directory)

#
# # no changes added to commit (use git add and/or git commit a) modified: hello.c

Add Changes
$ $ # # # # # # git add hello.c git status On branch master Changes to be committed: (use git reset HEAD <file>... to unstage) modified: hello.c

Staging and Committing


$ git add a.c $ git add b.c $ git commit -m Changes for a and b

$ git add c.c $ git commit -m Unrelated change to c

Staging and Committing


$ git add a.c $ git add b.c $ git commit -m Changes for a and b

$ git add c.c $ git commit -m Unrelated change to c

By separating staging and committing, you have the ability to fine tune what goes into each commit.

Committing Changes
When weve used git commit, weve always included the -m flag that gave a comment on the command line. What if we omit the -m flag?

Committing Changes

Committing Changes
git will pop you into the editor of your choice. The editor is chosen from the following list (in priority order): GIT_EDITOR environment variable core.editor configuration setting VISUAL environment variable EDITOR environment variable

Committing Changes
$ git status # On branch master nothing to commit (working directory clean)

Every change must be added and then committed

Creating a Branch
$ git checkout -b my_branch # ^ Creates a new branch my_branch, and checks it out (switches to that branch) $ git status # Should say something about being on the my_branch branch

Make some changes, then add and commit them

Switching back to the master branch


$ git checkout master Switched to branch master # Check if your changes are still there $ git checkout my_branch # Your changes are back!

Merging
$ git checkout my_branch $ git merge master

Sources
gitimmersion.com gitref.org

You might also like