知乎专栏 |
目录
homepage: http://git.or.cz/index.html
过程 125.1. Git
install
sudo apt-get install git-core
config
$ git-config --global user.name neo $ git-config --global user.email openunix@163.com
Initializ
$ mkdir repository $ cd repository/ /repository$ git-init-db Initialized empty Git repository in .git/
to check .gitconfig file
$ cat ~/.gitconfig [user] name = chen email = openunix@163.com
Tell git who you are: $ git config user.name "FirstName LastName" $ git config user.email "user@example.com" If you have many git repositories under your current user, you can set this for all of them $ git config --global user.name "FirstName LastName" $ git config --global user.email "user@example.com" If you want pretty colors, you can setup the following for branch, status, and diff commands: $ git config --global color.branch "auto" $ git config --global color.status "auto" $ git config --global color.diff "auto" Or, to turn all color options on (with git 1.5.5+), use: $ git config --global color.ui "auto" To enable aut-detection for number of threads to use (good for multi-CPU or multi-core computers) for packing repositories, use: $ git config --global pack.threads "0" To disable the rename detection limit (which is set "pretty low" according to Linus, "just to not cause problems for people who have less memory in their machines than kernel developers tend to have"), use: $ git config --global diff.renamelimit "0"
克隆到指定目录
➜ workspace git clone http://neo@192.168.30.5/netkiller.cn/api.netkiller.cn.git tmp.netkiller.cn
克隆单分支(非 master)
git clone -b 分支名 https://xxx.git git clone --branch <branchname> <remote-repo-url>
$ git clone git://github.com/git/hello-world.git $ cd hello-world $ (edit files) $ git add (files) $ git commit -m 'Explain what I changed'
[root@grey lua]# git status On branch grey Your branch is up to date with 'origin/grey'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: grey.lua Untracked files: (use "git add <file>..." to include in what will be committed) cache.lua flush.lua no changes added to commit (use "git add" and/or "git commit -a") [root@grey lua]# git restore grey.lua [root@grey lua]# git status On branch grey Your branch is up to date with 'origin/grey'. Untracked files: (use "git add <file>..." to include in what will be committed) cache.lua flush.lua nothing added to commit but untracked files present (use "git add" to track)
$ git branch * master mybranch $ git checkout mybranch Switched to branch "mybranch" $ git branch master * mybranch
setup.py 不经意间被删除,找到丢失那一刻的提交是 fda886b0ae1526020c366cea2b747b3ceda18ff6,通过 checkout 检出该文件
git checkout fda886b0ae1526020c366cea2b747b3ceda18ff6 -- setup.py
重新添加到版本库中
git add setup.py git commit -a -m '还原丢失文件' git push
for branch in $(git branch -r | grep -v HEAD) ; do # git checkout -b ${branch#*/} $branch; git checkout ${branch#*/}; git pull; done
发生冲突是文件内会出现
<<<<<<<HEAD <ours contents> ======= <theirs contents> >>>>>>>
使用 --ours 或 --theirs 来选择保留那一方的文件,例如:
git checkout --theirs <fileName>
冲突解决步骤
$ git checkout --ours <fileName> $ git add <fileName> //标记该冲突已解决 $ git rebase --continue $ git status $ git commit -a -m '冲突已经处理' $ git push
$ echo 'hello world!!!'> newfile $ git-add newfile
$ cd (project-directory) $ git init $ (add some files) $ git add . $ git commit -m 'Initial commit'
$ git clone git://10.10.0.5/example.git Cloning into example... remote: Counting objects: 5, done. remote: Compressing objects: 100% (4/4), done. remote: Total 5 (delta 1), reused 0 (delta 0) Receiving objects: 100% (5/5), done. Resolving deltas: 100% (1/1), done. neo@neo-OptiPlex-380:~/tmp$ cd example/ neo@neo-OptiPlex-380:~/tmp/example$ git status # On branch master nothing to commit (working directory clean) neo@neo-OptiPlex-380:~/tmp/example$ ls test1 test2 test3 test4 neo@neo-OptiPlex-380:~/tmp/example$ echo hello > test1 neo@neo-OptiPlex-380:~/tmp/example$ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: test1 # no changes added to commit (use "git add" and/or "git commit -a")
neo@neo-OptiPlex-380:~/tmp/example$ git diff diff --git a/test1 b/test1 index e69de29..ce01362 100644 --- a/test1 +++ b/test1 @@ -0,0 +1 @@ +hello
比较 nqp-cc/src/QASTCompilerMAST.nqp 文件 当前版本与 211ab0b19f25b8c81685a97540f4b1491eb17504 版本的区别
git diff 211ab0b19f25b8c81685a97540f4b1491eb17504 -- nqp-cc/src/QASTCompilerMAST.nqp
$ git clone git://10.10.0.5/example.git $ cd example $ (edit files) $ git add (files) $ git commit -m 'Explain what I changed' $ git push origin master Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 278 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To git://10.10.0.5/example.git 27f8417..b088cc3 master -> master
$ git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git://10.10.0.5/example 27f8417..b088cc3 master -> origin/master Updating 27f8417..b088cc3 Fast-forward test1 | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
$ git fetch remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 2 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (2/2), done. From git://10.10.0.5/example b088cc3..7e8c17d master -> origin/master
$ git clone git://github.com/git/hello-world.git $ cd hello-world $ (edit files) $ git add (files) $ git commit -m 'Explain what I changed' $ git format-patch origin/master