Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 126 章 Git - Fast Version Control System

distributed revision control system

目录

126.1. Repositories 仓库管理
126.1.1. initial setup
126.1.2. 克隆代码
126.1.3. 切换分支
126.1.4. git-add - Add file contents to the index
126.1.5. Creating and Commiting
126.1.6. Status
126.1.7. Diff
126.1.8. Push
126.1.9. Pull
126.1.10. fetch
126.1.11. Creating a Patch
126.1.12. reset
126.2. 分支管理
126.2.1. 查看本地分支
126.2.2. 创建分支
126.2.3. 删除分支
126.2.4. 切换分支
126.2.5. 重命名分支
126.2.6. git-show-branch - Show branches and their commits
126.3. git log
126.3.1. hash-object
126.3.2. 一行显示 --oneline
126.3.3. 查看文件历史记录
126.3.4. 格式化
126.4. reflog
126.5. 远程仓库
126.5.1. 查看远程地址
126.5.2. 添加远程仓库
126.5.3. 修改 origin
126.5.4. 删除 origin
126.5.5. 仓库共享
126.6. git show - Show various types of objects
126.6.1. 查看指定版本的文件内容
126.7. 合并分支
126.7.1. 合并分支
126.7.2. rebase
126.7.3. 合并分支解决冲突
126.7.4. 终止合并
126.7.5. 合并单个文件
126.7.6. Git 合并特定 commits 到另一个分支
126.8. 比较文件
126.8.1. 比较 SHA
126.8.2. 分支比较
126.9. Submodule 子模块
126.9.1. 添加模块
126.9.2. checkout 子模块
126.9.3. 删除子模块
126.10. Git Large File Storage
126.10.1. 安装 LFS 支持
126.10.2. LFS lock
126.11. git config
126.11.1. git config
126.11.2. 查看配置
126.11.3. 编辑配置
126.11.4. 替换配置项
126.11.5. 配置默认分之
126.11.6. GPG签名
126.11.7. core.sshCommand
126.11.8. fatal: The remote end hung up unexpectedly
126.11.9. 忽略 SSL 检查
126.11.10. 配置忽略合并文件
126.11.11. .gitignore
126.11.12. .gitattributes
126.11.13. 配置模版目录
126.12. git-rev-parse - Pick out and massage parameters
126.12.1. 获得当前提交ID
126.13. git-daemon 服务器
126.13.1. git-daemon - A really simple server for git repositories
126.13.2. git-daemon-sysvinit
126.13.3. inet.conf / xinetd 方式启动
126.13.4. git-daemon-run
126.13.5. Testing
126.14. git-svn - Bidirectional operation between a single Subversion branch and git
126.15. Web Tools
126.15.1. viewgit
126.16. gitolite - SSH-based gatekeeper for git repositories
126.16.1. gitolite-admin
126.17. FAQ
126.17.1. 导出最后一次修改过的文件
126.17.2. 导出指定版本区间修改过的文件
126.17.3. 撤销当前修改,恢复到远程最后一次提交
126.17.4. 回撤提交
126.17.5. 撤回单个文件提交
126.17.6. 合并分支中的单个
126.17.7. 每个项目一个证书
126.17.8. fatal: Not possible to fast-forward, aborting.
126.17.9. receive.denyCurrentBranch
126.17.10. 更新所有项目以及分支
126.17.11. 找回丢失的分支

homepage: http://git.or.cz/index.html

过程 126.1. Git

  1. install

    sudo apt-get install git-core
    			
  2. config

    			
    $ git-config --global user.name neo
    $ git-config --global user.email openunix@163.com
    			
    			
  3. 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
    			

126.1. Repositories 仓库管理

126.1.1. initial setup

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"
		

126.1.2. 克隆代码

克隆到指定目录

			
➜  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'

		
		

126.1.2.1. 恢复文件

				
[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)
				
				
			

126.1.3. 切换分支

git-checkout - Checkout and switch to a branch

126.1.3.1. checkout master

$ git checkout master
Switched to branch "master"
			

126.1.3.2. checkout 分支

$ git branch
* master
  mybranch

$ git checkout mybranch
Switched to branch "mybranch"

$ git branch
  master
* mybranch
			

126.1.3.3. 通过 checkout 找回丢失的文件

setup.py 不经意间被删除,找到丢失那一刻的提交是 fda886b0ae1526020c366cea2b747b3ceda18ff6,通过 checkout 检出该文件

				
git checkout fda886b0ae1526020c366cea2b747b3ceda18ff6 -- setup.py
				
			

重新添加到版本库中

				
git add setup.py
git commit -a -m '还原丢失文件'
git push
				
			

126.1.3.4. checkout 所有远程分支

				
for branch in $(git branch -r | grep -v HEAD) ; do
# git checkout -b ${branch#*/} $branch;
git checkout ${branch#*/};
git pull;
done				
				
			

126.1.3.5. 使用 ours 与 theirs 解决冲突

发生冲突是文件内会出现

				 
<<<<<<<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	
				
			

126.1.3.6. 使用远程分支强行覆盖本地分支

方法一

				 
neo@MacBook-Pro-M2 ~/w/netkiller (master) > git fetch --all
neo@MacBook-Pro-M2 ~/w/netkiller (master) > git reset --hard origin/master				
				
			

方法二

				 
git pull --force <远程主机> <远程分支>:<本地分支>				
				
			
				 
neo@MacBook-Pro-M2 ~/w/netkiller (master) > git pull --force origin master:master
				
			

126.1.4. git-add - Add file contents to the index

		
$ echo 'hello world!!!'> newfile
$ git-add newfile
		
		

126.1.5. Creating and Commiting

$ cd (project-directory)
$ git init
$ (add some files)
$ git add .
$ git commit -m 'Initial commit'
		

126.1.5.1. git-commit - Record changes to the repository

$ git-commit -m 'add a new file' newfile
Created initial commit f6fda79: add a new file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 newfile
		

126.1.6. Status

		
$ 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")
		
		

126.1.6.1. git-status - Show the working tree status

		
$ git-status newfile
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file: newfile
#		
			

126.1.7. Diff

		
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
		

126.1.7.1. --name-only 仅显示文件名

git diff --name-only
			

126.1.8. Push

		
$ 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
		
		

126.1.9. Pull

$ 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(-)
		

126.1.10. fetch

$ 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
		

126.1.11. Creating a Patch

		
$ 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
		
		

126.1.12. reset

重置到上一个版本

git log
git reset --hard HEAD^
git log
git push -f
		

126.1.12.1. 还原文件

				
$ git checkout <commit> --filename
$ git reset filename