Purdue ACM SigApp Intro to Git

30
Intro to Git Purdue ACM SigApp

description

A brief overview of many popular git features and commands. This presentation was given at an evening meeting of our SigApp chapter in Fall of 2013.

Transcript of Purdue ACM SigApp Intro to Git

Intro to GitPurdue 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 “[email protected]

Setup Line Ending Preferences

On Linux/Mac:$ git config --global core.autocrlf input

$ git config --global core.safecrlf trueOn Windows:$ git config --global core.autocrlf true$ git config --global 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 initInitialized 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 masternothing 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)

#

# modified: hello.c

#

no changes added to commit (use “git add” and/or “git commit -a”)

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 we’ve used “git commit”, we’ve 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 masternothing 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 masterSwitched 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