2011
Dec
30

Git 是一个很好用的程式码版本管理工具,不管是前端,后端工程师,都应该要学会 Git 。

安装 Git

  • Ubuntu: sudo apt-get install git
  • CentOS, RedHat : sudo yum install git

GitHub设定

接著去 GitHub 申请帐号,并 create Repository ,然后按照网站给的教学来设定吧!!

  • git config --global user.name "Your Name"
  • git config --global user.email [email protected]
  • 建立 private/public key : 就是建立一对密码,把 private 存到自已电脑的 /home/name/.ssh/id_rsa , 另外一个 public key 存到 github。
    • 建立key方式 : ssh-keygen -t rsa -C "xxxm"
    • 测试进线 ssh -T [email protected] : 若收到讯息「Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.」这样就算成功罗。
  • 接著回到你要管理的程式目录
  • git init : 初始化 git,git 会自动建立资料夹 .git/ ,之后的档案是否有更新,都会从这边去比对。

再来还是跟著网站说明,上传一个 README 档案

  • touch README :建立 README
  • git add README : 将 README 增入至 git
  • git commit -m 'first commit' : commit 至 local git
  • git remote add origin [email protected]:xxxx/MyProgram.git 指定我要 commit 到 github server 路径,以及设定一个 Local branch -> origin
  • git push -u origin master : commit local branch origin 到远端的 master
  • 最后回到 GitHub 网站点一下 continue 就完工罗

clone 一份全新的 branch 回来

如何取消尚未 commit 的 code

在 GIT 中要取消修改,不像 svn 这么简单,以前用 svn 都会直接 rm 删除档案,然后再重新执行 svn update ,但是在 git 中,如果你直接用 rm 删除档案, git 会认定你不要这个档案了,然后就会出现 deleted file 的状态。

取消方式是,先用 git status 来检查有那个档案被修改过。

Example
  1. # On branch master
  2. # Changes not staged for commit:
  3. # (use "git add <file>..." to update what will be committed)
  4. # (use "git checkout -- <file>..." to discard changes in working directory)
  5. #
  6. # modified: xxx.php

再来使用 git checkout 取消这次的修改,注意指令中间要多加 -- 的符号,如果你要回复到特定的版号,那么就要补上 vesrion 。

git checkout -- xxx.php
git checkout (version) xxx.php

如果要取消全部的档案,可以使用下面这句语法,将全部的修改取消掉。

git checkout $(git ls-files --modified)

回复 commit 的 code

先输入 git log 看看目前 commit 的进度,然后选择你要回复的 log id ,最后使用 git revert 将程式回复。

  • git revert 508bbac0f9f92b971xxxf852c1bb7191d7a62722

如果你已经将 code , add 并 commit 到 local repository ,这时你可以用 git reset 的方式,将程式还原。

  • git reset --hard 3d5575f8e4c97ddab8ad5d540fee4664c04db75d

碰到 git auto merged files 有 conflict 时,如果不想处理 conflict,也可以直接用 reset 的方式还原,但要注意你刚刚所修改的程式就不见罗。

GIT merge error “commit is not possible because you have unmerged files”

Merge 其他人的 commit code 回来测试

Github 有一个不错的功能,就是每个人都能 Fork 一份最新的 code 回去修改,比如现在有 A ,B 两个人,各有一个 Code Branch , 当 A 修改了一部分 Code 之后, B 可以直接将 A 修改的部分拉回来自已的 Branch 做 Merge,然后等到 B 也测试完毕,再 commit 到 master branch, Merge 的方式是使用 Pull

其它指令

  • git remote update : 更新本地端的档案
  • git remote rm origin : 删除本地端 branch origin
  • git diff : 将新改的档案与本地端的 branch 做 diff
  • git diff origin/master: 与 github 上的 code 做 diff
  • git branch : 查看目前有多少个 branch

Git Sub Module

Git 还有一个 Sub Module 的功能,这是指一个 Git Project 中用到了第三方的 Git Source Code ,我们可不想把第三方的程式上传到我们的 Git Project,这时你可以使用,submodule 的方式,将第三方程式码下载回来。

增加一个 Sub Module


Git clone 时,连Sub Module 也一并下载

  • git clone --recursive git://github.com/user/myProject.git

更新 Sub Module

  • git submodule update --init --recursive

Git conflict

Please, commit your changes or stash them before you can merge. git
  • git stash
  • git pull
  • git stash pop

diff 本机已 commit 的程式和 git server 上的程式。

  • git diff HEAD origin/master
  • git log -p -3 (查询 commit log)

Pull Remote Branch

我 Local 端的 code = [email protected]:myname/source.git (master)

branch 操作

  • 删除远端 branch : git push origin --delete dev
  • 删除本地端 branch : git branch -d xxbranch
  • 切换 branch: git checkout xxxbranch

清除 git .log

当 git 越用越久,git log 的资料会越来越大,最后造成 git clone 的时间会很长,这时可以用以下指令清除没用的 log

Example
  1. git reflog expire --expire=now --all
  2. git gc --aggressive --prune=now
  3.  
  4. git push origin master --force

如何自已当一个 git server

git 的程式,本身就内建可以当一个 git server ,所以你不用额外安装其它的程式。

首先我们在 server 上先建立一个新的 repository。

  • cd /repository/
  • git init --bare new_repo

接著回到开发目录,随便写一个 README 档,然后用类似在 github 上的指令,将档案 push 到我们自已的伺服器。

  • cd /xxx/xx/new_repo
  • touch README
  • git init
  • git add README
  • git commit -m "initial commit"
  • git remote add origin account@host:/repository/new_repo
  • git push origin master

做完之后,再检查 /repository/new_repo 是不是多了一个 README 的档案呢。


回應 (Leave a comment)