Git
NOTE
[ ] means that it can be omitted.
Setup
git config --global user.name "my name"
git config --global user.email "my-address@goes.here"
git config --global core.editor "notepad++"
git config --list
git config --global -e # to use specific editor
cat ~/.gitconfig
4 Areas
- Working Directory
- Staging Area
- Repository (.git folder)
- Remote
Basics
Words
HEAD
means last commit.
Add
git add . # all files under current directory
git add -A # all files
git add -u # all files except new files (u = update)
git add . --dry-run # pre test
Commit
git commit -m "commit message"
git commit -am "commit message" # add & commit at same time
List tracked files
git ls-files
Unstage
git reset [HEAD]
git reset [HEAD] somefile.txt
Discard changes
git checkout [--] somefile.txt
Move files
git mv file1.txt file2.txt
the difference between mv
andgit move
is that git mv
automatically stages files.
Delete files
git rm file1.txt
Show logs
git log --all --oneline --graph --decorate
git log f8vk34...1ffai8
git log somefile.txt
git log --follow somefile.txt # follow rename
git show fi8vd2
Alias
git config --global alias.h "log --oneline"
git h # => equiv with "git log --oneline"
or edit .gitconfig directly.
Merge & Diff tools
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
git config --global diff.tool p4merge
git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
Compare
All followning diff
s can be replaced with difftool
HEAD vs Working directory
git diff
HEAD vs Staging area
git diff --staged
#or
git diff --cached
HEAD vs All changes(staged & unstaged)
git diff HEAD