0. 安裝 GIT

yum install git -y

 

0. 建立 GIT 身分

第一種方法,下指令

git config --global user.name <yourName>
git config --global user.email <yourEmail>
git config --global color.ui true

 
或是直接幹走我的設定檔
vim ~/.gitconfig
[code]
    editor = vim
    excludesfile = /home/rickymax/.gitignore_global
    #whitespace = trailing-space,space-before-tab
[user]
    name = rickymax
    email = rickylo@iii.org.tw
[color]
    ui = true
    diff = auto
    status = auto
    branch = auto
    log = auto
[apply]
    #whitespace = fix
 
[alias]
    co = checkout
    ci = commit
    di = diff
    st = status
    br = branch
    re = remote
    di = diff
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --

記得改名字學號, 不然會被助教抓, 然後被教授當掉

 

初始化 (optional)

如果你是從無到有,那你需要做這件事情
來幫助你再專案裡面生成 .git檔

cd /your/project
git init

 

GIT CLONE

從 github 之類的複製一份專案下來

git clone https://github.com/PROJECT/YOU-WANT.git

 

GIT BRANCH

拿到專案的第一件事,請確認版本是不是你要的 (查看local branch)

git branch

 
如果不是請跳到你想要的版本
# 查一下有哪些 branch (包含 remote branch)
git branch -a
# Jump~
git checkout -t origin/YOU-WANT
 
# 更新一下東東
git fetch
 
# 把程式拉下來吧
git pull origin <YOU-WANT>

 

當你改過 CODE 之後..

把你想要更新的檔案加到暫存區

git add <FILE>

 
確認一下那些檔案變動了
git status

 
如果沒問題, 就送進檔案庫
git commit -m "commit message"

請養成打註解的好習慣

 
這邊的 commit 只會送到 local 端
如果想要 push 到自己的(或是跟別人合作的) git web

git push origin <YOU-WANT>

 

其他常用指令

查看過去 commit 的歷史紀錄

git log

 
如果你有偷我的 .gitconfig
git lg

查看那些檔案被修改, 並且比較其內容

git diff

有 git add 就有 git rm
同理可證, 就是刪除檔案, 並且更新歷史紀錄

git rm <FILE>

 

修改/取消上一次的 commit

取消剛剛的 commit,但保留修改過的檔案。

git reset HEAD^ --soft

 
取消剛剛的 commit,回到再上一次 commit的 乾淨狀態。
git reset HEAD^ --hard

 
修改上一次的 commit 訊息
git commit --amend

 

結語

以上就是基本的 git 操作

等熟悉這些操作之後

有機會跟別人一起 coding 時

再去研究一下 新增/刪除 branch, git merge
 
 

git diff

設定:用了之後就可以 git 結合 diffmerge

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd 'diffmerge "$LOCAL" "$REMOTE"'
git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd 'diffmerge --merge --result="$MERGED" "$LOCAL" "$(if test -f "$BASE"; then echo "$BASE"; else echo "$LOCAL"; fi)" "$REMOTE"'
git config --global mergetool.diffmerge.trustExitCode true

只要設定一次就好了

 

# 換成使用 difftool
git difftool
# 換成使用 mergetool
git mergetool

 

 

 

除非特別註明,本頁內容採用以下授權方式: Creative Commons Attribution-ShareAlike 3.0 License