Level 2: Staging & Remotes
Undo Commit Commands
1. git reset --soft HEAD^
Undo last commit (the changes will be keep in staging)2. git commit --amend -m "new message"
Change the last commit3. git reset --hard HEAD^
Undo last commit and all changes (hard reset will not keep the change)4. git reset --hard HEAD^^
Undo last 2 commits and all changesAdding a remote
git remote add origin https://github.com/name/git-real.git
new remote name of the remote address
kind of like bookmark, means 'origin' referring the above url address
git remote -v
Show all remote repositories
Change commit with one command
git commit --amend -m "change last commit with one command"
Check out file with current branch?
git checkout -- filename.fileext
=============================================================
Level 3: Cloning & Branching
Clone a repository
git clone
Clone a repository
git remote -v
list all the remotes ?
Checkout branch
1. git branch newBranch
create a new branch
2. git checkout newBranch
checkout the new branch
(same as)
1. git checkout -b newBranch
create a new branch and checkout
Merge branch
step 1: git checkout master
switch back to the main branch u want ur change to goes in
step 2: git merge branchToMerge
merge the temp branch to be merged
step 3: git branch -d branchToMerge
remove the temp branch
=============================================================
Level 4: Collaboration Basics
About pull
1. git pull
get latest code and update local code
(same as)
1. git fetch
get latest code from remote branch
2. git merge origin/master
merge remote master into local branch (update local code)
=============================================================
Level 5: Branching
Pulling New Branches
1. git branch -r
show all remote branches
2. git checkout remoteBranchName
checkout remote branch, already automatic link this local branch to the remote branch?
Removing a Branch
1. git push origin :remoteBranch
delete remote branch
2. git branch -d localBranch
delete local branch (if has uncommitted change, will get error)
3. git branch -D localBranch
force delete local branch
Manage remote branches
1. git remote show origin
show all remote and local branches, and their status (may have the ref to deleted remote branch)
2. git remote prune origin
to clean up deleted remote branches
Tagging (version)
1. git tag
list all tags
2. git tab -a v0.0.2 -m "version 0.0.2"
create a new tag
3. git push --tags
push new tags
4. git checkout v1.0.1
checkout or retrieve a previous version tag
=============================================================
Level 6: Rebase Belong to us
Rebase
1. git fetch
get(retrieve) remote changes
2. git rebase targetBranch
(get change from the current branch and put on top of the targetBranch?)
move all changes to master which are not in origin/master to a temp area
run all origin/master commits
run all commits in the temp area, one at a time (redo commits one by one)
2.1. git rebase --continue
if has conflict, fix the conflict then continue rebase
2.2. git rebase --abort
if want to stop rebase
3. git merge fromBranch
merge the fromBranch(just rebased) to targetBranch?
=============================================================
Level 7: History and Configuration
Config display
git config --global color.ui true
turn on colorizing the log
Output log with format
git log --pretty=oneline
display one commit in one line
git log --pretty=format:"%h %ad- %s [%an]"
%ad : author date
%an : author name
%h : SHA hash
%s : subject
%d : ref names
git log --oneline -p
patch the change with history
git log --oneline --stat
show how many changes (number)
git log --oneline --graph
show commits history with graph (simple)
git log --until=1.minute.ago
git log --until=1.day.ago
git log --until=1.hour.ago
git log --since=1.month.ago --until=2.weeks.ago
git log --since=2000-01-01 --until=2000-02-02
show log within date range
Show diff with format
git diff
(same as)
git diff HEAD
git diff HEAD^
git diff HEAD ^^
git diff HEAD~5
git diff HEAD^..HEAD
compare second most recent commit with most resent commit
git diff SHAShashNum1..SHAShashNum2
Show diff with format
git diff branch1 branch2
git diff --since=time1 --until=time2
Blame
git blame file.ext --date short
Untracking Files
git rm --cashed file.ext
use when u want to untracking the file without remove it
See all local config
git config --list
Setup Aliases
1.
git config --global alias.mylog \
"log --pretty=format:'%h %s [%an]'" --graph
2.
git mylog
pre-set log format for easy use later
1. git config --global alias.st status
2. git st
(same as)
git status
git config --global alias.mylog \
"log --pretty=format:'%h %s [%an]'" --graph
2.
git mylog
pre-set log format for easy use later
1. git config --global alias.st status
2. git st
(same as)
git status