Git Basics 1 Carenza

26
Git Basics I Credit to Git (http://git-scm.com/book)

description

Beginning git

Transcript of Git Basics 1 Carenza

Git Basics ICredit to Git (http://git-scm.com/book)

Why Git?

● Most Operations are local● High integrity - use of SHA-1 hash● Most of the time, git adds data

○ Hard to mess up○ You can recover quite a bit○ All is not lost!

United (actually 3) States of Git

Git Configs

user.email user.namecore.editor merge.toolcolor.ui color.diffformat.signoff … and many, many more!See git help config

Initializing and Adding to Repos

● git init○ creates .git directory○ nothing is initially tracked

● git add○ Adds files/paths to a git○ can also use wildcards

● git clone○ git clone url [-b branch] [target]

The File Status Lifecycle

Checking the file status

● git status● .gitignore● git diff● Exercise: demonstrating the lifecycle, pt 1

○ Creating○ Tracking○ Staging○ Committing

More life cycle commands you may not be aware of!

● git diff revisited● git rm● git mv

Viewing past changes

● git log○ -p switch - view code diffs for each commit○ -[#] (ex. -2) - view the past # commits○ --stat - view stats for each commit○ --pretty - format log

■ oneline■ format

○ --graph○ --name-only

Limiting log output

● --since● --until● --author● --grep● --committer● --no-merges

Log --pretty=format: options%H,%h Commit hash

%T,%t Tree hash

%P,%p Parent hashes

%an Author name

%ae Author e-mail

%ad Author date (format respects the --date= option)

%ar Author date, relative

%cn Committer name

%ce Committer email

%cd Committer date

%cr Committer date, relative

%s Subject

Git to the rescue! Preventing derps

● git commit --amend● git reset HEAD <filename>● git checkout -- <filename>

Hint: Look at the git status command for clueson what to do next!

Using remote repositories - git remote● By itself - lists the remotes by short name

○ “origin” when cloned● -v option shows expanded URL● git remote add <shortname> <url-to-repo>● git remote set-url <shortname> <url>● git remote show <shortname>● git remote rm <shortname>● git remote rename <shortname>

The fetch/push cycle

● git fetch <shortname>○ Fetches all the branches/commits from the remote

● git pull <shortname> <branch>○ Shorthand merge (more on this later)

● git push <shortname> <branch>○ Pushes local commits to the remote

Cool git shortcut!

● aliases using git config○ git config --global alias.<shortname> <command>○ Ex: git config --global alias.co checkout○ git config --global alias.unstage ‘reset HEAD --’○ git config --global alias.last ‘log -1 HEAD’

Creating Branches

● git branch <branchname> - create branch● git checkout <branchname> - switch to

existing branch● git checkout -b <branchname> -

combination of git branch and git checkout

Visualizing branches (Exercise)

git branch iss53

git checkout iss53; git commit -a

git checkout -b hotfix; git commit -a

git checkout master; git merge hotfix

git branch -d hotfix; git checkout iss53; git commit -a

git merge iss53

Resolving merge conflicts

● Choose one section or another or groups of either/both depending on what conflicts

● Requires a little knowledge & intuition● Git status tells all!<<<<<<< HEAD<div id="footer">contact : [email protected]</div>=======<div id="footer"> please contact us at [email protected]</div>>>>>>>> iss53

Branch management

● git branch○ git branch -v see the last commit on each branch○ git branch --merged find out which branches have

merged○ git branch --no-merged○ git branch -d / git branch -D delete branch

Remote branches

● <remote>/<branch>● to synchronize git fetch <remote>● merging with remote

git merge <remote>/<branch>● git push <remote> <branch>

○ git push origin test○ git push origin test:dummy○ git push origin :dummy (removes dummy)