32
Git Quick Tutorial Pikicast R&D Jack Pham

Git tutorial

Embed Size (px)

Citation preview

Page 1: Git tutorial

Git Quick TutorialPikicast R&DJack Pham

Page 2: Git tutorial

Git CommandsSetting up a repository init, clone, config

Saving changes add, commit

Inspecting a repository status, log

Navigate through commit and branch checkout

Undo changes checkout, revert, reset, clean

Rewriting history commit —ammend, rebase (rebase -i), reflog

Syncing remote, fetch, pull, push

Branching branch, checkout, merge

* Need to use with caution, should never be done on public(share) target

Page 3: Git tutorial

Setup a repository and start working

init

clone

Page 4: Git tutorial

git vs svn

Page 5: Git tutorial

Git init

git init

git init --bare

git clone

Page 6: Git tutorial

Saving changes

addcommit

Page 7: Git tutorial

Git repository local

Page 8: Git tutorial

Working Directory Stage Area

File status

git add

git add

git commit

git rm

Tracked

Page 9: Git tutorial

Common Git Routine (Local)- Edit-Stage-Commit

$vim hello.js

# Edit

$ git add hello.js

$ git commit

# Edit commit message

- Edit-Stage-Commit-Amend# Edit hello.js

$ git add hello.js

$ git commit --amend

# Edit commit message

** Don’t amend commit that you already push to public repository

caution !!

Page 10: Git tutorial

Viewing old commits

log

checkout

Page 11: Git tutorial

View log

$ git log

$ git log -n <limit>

$ git log --oneline

$ git log --stat

$ git log -p

$ git log --author=“<patterns>”

$ git log <file>

$ git log --graph --decorate --oneline

$ git log --grep=“<patterns>”

Page 12: Git tutorial

git checkout branch

git checkout master git checkout feature

Page 13: Git tutorial

git checkout commit

git checkout mastergit checkout 81abc12

** Don’t commit your work on a detach head

caution !!

Page 14: Git tutorial

Checkout options

git checkout <branch>

git checkout <commit>

git checkout <commit> <file>

Example:

git checkout master

git checkout a12ebd3

git checkout a12ebd3 hello.js

Page 15: Git tutorial

Undoing changes

checkout

revert

reset

Page 16: Git tutorial

git checkout to undo

git checkout a1e8fb5 # modify files

git add <file>

git commit

Page 17: Git tutorial

git revert <commit>

git revert abcd12ef

Before revert

After revert

abcd12ef

- The git revert command undoes a committed snapshot.- Undo the changes introduced by the commit- Appends a new commit with the resulting content. - git revert doesn't alter history

Page 18: Git tutorial

git reset

abcd12ef abcd12ef

Before resetAfter reset

- Variation:- git reset <file> : remove from staging area- git reset : reset branch to most recent commit (soft)- git reset ——hard- git reset <commit>- git reset ——hard <commit>

- git revert is “safe” way to undo- git reset “dangerous” to undo

** Don’t reset if you’ve already pushed to shared repository

caution !!

Page 19: Git tutorial

revert vs reset

- Don’t alter history- Safe way to undo but

generate more commit- For undo shared/public

commits

- Alter history- unsafe way to undo

shared/public commits- Cleaner history- For undo private (non-public)

commits

Page 20: Git tutorial

Rewriting History

commit --amend

reflog

rebase

Page 21: Git tutorial

git commit --amend

- Modify last commit- Replace last commit entirely

- Use to fix last commit (which hasn't pushed to share repo)

- Variation- git commit --amend - git commit --amend --no-edit

** Don’t amend commit that you already push to public repository

caution !!

Page 22: Git tutorial

git rebase <base>- Rebasing is the process of moving a

branch to a new base commit.- Maintain linear history

git rebase

git merge

recommend

!!

Page 23: Git tutorial

git reflog

- What to do if you accidentally - git reset --hard

- Reflog contain log of activity you perform

Page 24: Git tutorial

Remote synchronise and branching

remote

pull

fetch

pushbranch

merge

Page 25: Git tutorial

git remote

git remote

git remote -v

git remote add <name> <url>

git remote rm <name>

git remote rename <oldname> <new-name>

Page 26: Git tutorial

git fetch

- Download commits from remote but not merge to local

- Give you opportunity to review- Merge or rebase after review

git fetch <remote>

git fetch <remote> <branch>

Page 27: Git tutorial

git pull

git fetch

git merge

git pull = git fetch + git mergegit pull --rebase = git fetch + git rebase

- Download commits from remote and merge (or rebase) to local branch

Page 28: Git tutorial

git push

transfer commits from your local repository to a remote repo.

git push <remote>

git push <remote> —force

git push <remote> -all

git push <remote> --tags

** Do not use the --force flag unless you’re absolutely sure you know what you’re doing.

caution !!

Page 29: Git tutorial

git branchRepresented by a pointer (branch tip)

git branch

git branch <branch>

git branch -d <branch>

git branch -D <branch>

git branch -m <branch>

Page 30: Git tutorial

git mergePutting forked history back together

git branch -m <branch>

git branch --no-ff <branch>

fast-forward(git does this whenever

possible)

non-fast-forward (3-ways merge)

(probably after rebase)

Page 31: Git tutorial

Cleaner history

- Private branch: rebase & fast-forward merge

- Share: 3-way merge

- Atomic commit and meaningful message

- Good naming convention

recommend

!!

Page 32: Git tutorial

Staying out of troubles

- Don’t amend commit that you already shared repository

- Don’t commit your work on a detached head

- Don’t reset if you’ve already pushed to shared repository

- Don’t use the ‘push --force’ flag unless you’re absolutely sure you know what you’re doing.

caution !!