GIT使用总结
GIT官网地址:http://git-scm.com/
PDF版本教程:https://progit2.s3.amazonaws.com/en/2015-01-02-24f7e/progit-en.261.pdf
从next分支创建远程分支osd-queue osd-queue/next
本机:
git remote add -t next osd-queuegit@192.168.1.244:/chinfotech/bsdocfs-server.git
git fetch osd-queue
git checkout -b osd-queue osd-queue/next
另外的机器:
git fetch
git checkout -b hj origin/osd-queue
git checkout next src/osdc/Objecter.*
当git push出错:
huangjun:/usr/src/bsdocfs-2.7# git pushorigin teammds
git@192.168.1.244's password:
error: src refspec teammds does not matchany.
error: failed to push some refs to'git@192.168.1.244:/chinfotech/bsdocfs-server.git'
huangjun:/usr/src/bsdocfs-2.7# git show-ref
最好的方法是:git push origin HEAD:teammds
git push的用法:
git push 远程名 本地分支:远程分支
如果出现以下错误:
By default……
you can set……
在gitserver端使用以下命令:
git config receive.denycurrentbranch ignore
配置该选项之后,就可以使用git push命令;
要想将一个分支推到远程,使用
git push 远程名 远程分支(若远程没有该分支,则会在远程新建该分支)
删除远程分支:
git push 【远程名】 :【远程分支】
将空的分支push到“远程分支”,等于是将远程分支删除;
git-tag -a v2.7-release 每次生成一个新的版本时,需要打上tag
在git-tag不能被git describe识别
git-describe v0.2.8
显示V0.2.7
应该按照git-tag –a version-name,git-commit 的顺序
git-tag -a v2.7-release
git commit -a -m "message"
删除某个tag:git tag –d tagname
上传tags
git push –tags
对于git中,在gitignore文件写入的untrack条件,并没有执行;
1)先创建git仓库
git init
2)然后再将当前目录下的所有文件都加入这个仓库
git add .
3)生成第一个提交版本
git commit -a -m "XX"
4)编写.gitignore文件
$vim .gitignore
*.pyc
5)提交更新
git commit -a -m "XX"
6)用gitk查看,这些文件仍然加入到git版本管理中;
搜寻所得方法:
1)删除那些被误跟踪的文件
git rm -r --cached *.pyc
在freenas下出现:
fatal: pathspec '.idea/' did not match any files
说明对该文件夹已经执行过该操作;
2)将当前的工作分支打包
git archive -o contentmanager.zip HEAD
将当前工作分支的文件都压缩到contentmanager.zip文件中;
3)git保留一个目录,但是不跟踪目录下的文件
touch uploads/.gitkeep
git add uploas/.gitkeep
4)修改最近提交的log
gitcommit --amend
5)若在git push时出现如下情况:
fatal: HTTP request failed
请尝试修改.git/config文件
修改[remote"otigin"]选项:
#url = https://github.com/hjwsm1989/learn
url = git@github.com:hjwsm1989/learn.git
原因:github目前只支持ssh方式来读写git仓库,虽然https方式标注为支持读写;
1) 查看所有分支的提交
git log –branches –p COMMIT
7)跟踪某些同类型的特殊文件
比如要过滤掉tar.gz后缀的文件(因为是压缩包)
但是某些项目中,需要预先下载安装包,可以节省时间(从网络下载耗费较长时间)
那么,可以在.gitignore文件中加入该文件要被trace
!pushy-0.5.1.tar.gz
若是整个文件夹,那么可以加入如下控制语句:
!/others/bsdocfs-deploy/packages/*
这样,整个packages下的文件都不会被忽略,之后,加入到git仓库:git add packages
7)git push出错
error: The requested URL returned error:403 Forbidden while accessing https://github.com/hjwsm1989/ceph/info/refs
fatal: HTTP request failed
解决方法:
vim .git/conf
#url = https://github.com/hjwsm1989/ceph
url= git@github.com:hjwsm1989/ceph.git
使用git
8) git submodule update --init
fatal: Needed a single revision
Unable to find current revision in submodule path 'ceph-object-corpus'
9)git rebase
合并不同分支的提交版本
git rebase test
先将test分支的代码checkout出来,作为工作目录
然后将master分支从test分支创建起的所有改变的补丁,依次打上。如果打补丁的过程没问题,rebase就搞定了
如果打补丁的时候出现了问题,就会提示你处理冲突。处理好了,可以运行git rebase –continue继续直到完成
如果你不想处理,你还是有两个选择,一个是放弃rebase过程(运行git rebase –abort),另一个是直接用test分支的取代当前分支的运行git rebase –skip
10)git flow
http://nvie.com/posts/a-successful-git-branching-model/
http://www.zavakid.com/2013/08/14/git_flow_2/
更加服务产品开发的概念,所有的操作步骤都是在产品开发的基础上抽象
新建一个git仓库
$ git flow init
按照提示,enter下去,全部使用默认值
开发一个新的特性
$ git flow feature start issue_1
………..(完成这个特性的代码编写过程)
$ git flow feature finish issue_1
会将feature/issue_1分支合并到develop分支,并切换到develop,删除feature/issue_1分支。
需要发布一个版本
$ git flow release start v0.9
开始一个新版本发布流程的代码编写工作
……….
$ git flow release finish v0.9
会将release/v0.9分支合并到master和develop分支上,并将release/v0.9打上一个tag
gitstash操作
使用git的时候,我们往往使用branch解决任务切换问题,例如,我们往往会建一个自己的分支去修改和调试代码, 如果别人或者自己发现原有的分支上有个不得不修改的bug,我们往往会把完成一半的代码 commit提交到本地仓库,然后切换分支去修改bug,改好之后再切换回来。这样的话往往log上会有大量不必要的记录。其实如果我们不想提交完成一半或者不完善的代码,但是却不得不去修改一个紧急Bug,那么使用'git stash'就可以将你当前未提交到本地(和服务器)的代码推入到Git的栈中,这时候你的工作区间和上一次提交的内容是完全一样的,所以你可以放心的修 Bug,等到修完Bug,提交到服务器上后,再使用'git stash apply'将以前一半的工作应用回来。也许有的人会说,那我可不可以多次将未提交的代码压入到栈中?答案是可以的。当你多次使用'git stash'命令后,你的栈里将充满了未提交的代码,这时候你会对将哪个版本应用回来有些困惑,'git stash list'命令可以将当前的Git栈信息打印出来,你只需要将找到对应的版本号,例如使用'git stash apply stash@{1}'就可以将你指定版本号为stash@{1}的工作取出来,当你将所有的栈都应用回来的时候,可以使用'git stash clear'来将栈清空。
在这里顺便提下git format-patch -n , n是具体某个数字, 例如 'git format-patch -1' 这时便会根据log生成一个对应的补丁,如果 'git format-patch -2' 那么便会生成2个补丁,当然前提是你的log上有至少有两个记录。
示例:
1) 从wip-wr分支切出wip-objecter分支,然后进行修改,但是目前还没有完成该分支(wip-objecter)的所有修改,但是wip-wr上有大量的修改,而且会涉及到wip-objecter分支,此时一般的操作:先将本分支的修改提交git commit, 然后使用git fetch拉取远程的更新,然后更新远程分支wip-wr到本地wip-wr分支(git merge origin/wip-wr),继续进行操作;
另外一种方法:将本地的修改保存在git协议栈.gitstash,然后git fetch, git merge origin/wip-wr;然后进行将上次保存到git协议栈的更新还原到当前的栈;
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear:清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。
GIT BRANCH更名
$ git branch –m old-branch-namenew-branch-name
配置win下的git:
- ls不能显示中文目录。解决办法:在git/etc/git-completion.bash中增加一行:
折叠复制代码
1. alias ls='ls –show-control-chars –color=auto'
- git commit不能提交中文注释。解决办法:修改git/etc/inputrc中对应的行:
折叠复制代码
1. set output-meta on set convert-meta off
- git log无法显示中文注释。解决办法:在git/etc/profile中增加一行:
折叠复制代码
1. export LESSCHARSET=iso8859
3.1 解决gitk显示中文乱码的问题
vim ~/.gitconfig
[gui]
encoding = utf-8
- centos 设置tab键的字符数
vim /etc/vimrc
set expandtab
set sw=4
set tabstop=4 " 怕檔案裡頭仍有 \t
set softtabstop=4
- 在vim中分隔多个窗口
:sp filepath
Ctrl+w j k表示切换窗口
Ctrl+w q表示退出窗口
- 设置文件格式,解决^M错误
set fileformat=unix
git config --global core.autocrlf input
# Set thissetting on OSX or Linux
git config--global core.autocrlf true
# Set thissetting on Windows
在pycharm中设置shell脚本的换行符,选择:“file”à
track文件权限
vim .git/config
filemode = false表示不track文件权限的改变
git 补丁
生成一个patch
git diff >patch
应用patch
git apply patch
git format-patch生成的git专用补丁
使用git am 0001-Fix1.patch来应用该补丁
合并指定分支的指定文件
如果要将A_branch分支的文件f.txt copy到B_branch分支上;
1 2 3 | git checkout B_branch cd path/to/f.txt git checkout A_bracn f.txt |
git add报错:
error: bad index file sha1 signature
git pull报错
fatal: cannot createdirectory at 'doc/bugs/3.20121112:_build_error_in_assistant': Invalid argument
git clean -df 可帮你搞定一切。
举例:
git clean -dn 这个命令可以看看有哪此文件和目录会被删
git clean -f 只会删文件,不会删目录
git clean -xf 可以删除在.gitignore文件中记录的一些文件
仍然出现错误:
error: bad index file sha1 signature
fatal: index file corrupt
fatal: git status --porcelain failed
解决方法:
查找当前的.git仓库,然后参数
find ./ -name “.git” -exec rm -rf {} \;
git status
git checkout
切换分支,
$ git checkout -- filename
用暂存区中filename文件来覆盖工作区中的filename文件。相当于取消自上次执行git add filename以来(如果执行过)的本地修改。
$ git checkoutbranch -- filename
维持HEAD的指向不变。用branch所指向的提交中filename替换暂存区和工作区中相应的文件。注意会将暂存区和工作区中的filename文件直接覆盖。
$ git checkout --. 或写作 git checkout .
注意git checkout 命令后的参数为一个点(“.”)。这条命令最危险!会取消所有本地的 修改(相对于暂存区)。相当于用暂存区的所有文件直接覆盖本地文件,不给用户任何确认的机会
Git子模块git submodule:
http://www.kafeitu.me/git/2012/03/27/git-submodule.html
一个工程中包含一个子工程,这个工程是一个目录,也是通过git进行管理的,
类似SGMag和monitor的关系,如何设置?
根据.gitmodules文件来识别、配置子模块
.gitmodules示例内容:
[submodule "ceph-object-corpus"]
path = ceph-object-corpus
url = git://github.com/ceph/ceph-object-corpus.git
[submodule "src/libs3"]
path = src/libs3
url = git://github.com/ceph/libs3.git
[submodule "src/civetweb"]
path = src/civetweb
url = git://github.com/ceph/civetweb
[submodule"src/erasure-code/jerasure/jerasure"]
path = src/erasure-code/jerasure/jerasure
url = https://github.com/ceph/jerasure.git
branch = v2-ceph
[submodule"src/erasure-code/jerasure/gf-complete"]
path = src/erasure-code/jerasure/gf-complete
url = https://github.com/ceph/gf-complete.git
branch = v2-ceph
[submodule "src/rocksdb"]
path = src/rocksdb
url = git://github.com/ceph/rocksdb
ignore = dirty
[submodule"ceph-erasure-code-corpus"]
path = ceph-erasure-code-corpus
url = https://github.com/ceph/ceph-erasure-code-corpus.git
操作步骤:
为learn工程加入lottery submodule
[submodule “python/lottery”]
path= python/lottery
url= git@github.com:hjwsm1989/Lottery.git
执行git submodule addgit@github.com:hjwsm1989/Lottery.git python/lottery
执行git commit -a -m "add submodules[Lottery] to learn"
在另外一台主机上使用learn工程
[root@oracle-vm home]# git clone git@github.com:hjwsm1989/learn.git
查看submodule
[root@oracle-vm learn]# git submodule
-bc1885de22565f586524b6392cfdaa73b25a4d8dpython/lottery
初始化子项目
[root@oracle-vm learn]# git submodule init
Submodule 'python/lottery'(git@github.com:hjwsm1989/Lottery.git) registered for path 'python/lottery'
查看工程的配置文件
[root@oracle-vm learn]# cat .git/config
[core]
repositoryformatversion= 0
filemode= true
bare= false
logallrefupdates= true
[remote "origin"]
fetch= +refs/heads/*:refs/remotes/origin/*
url= git@github.com:hjwsm1989/learn.git
[branch "master"]
remote= origin
merge= refs/heads/master
[submodule "python/lottery"]
url =git@github.com:hjwsm1989/Lottery.git
更新本地工程的子项目
[root@oracle-vm learn]# git submodule update
A、在子项目上修改,并提交
更新本地的project
[root@oracle-vm learn]# git submodule foreach git pull origin master
Entering 'python/lottery'
Warning: Permanently added the RSA host key forIP address '192.30.252.130' to the list of known hosts.
From github.com:hjwsm1989/Lottery
*branch master -> FETCH_HEAD
Updating bc1885d..6a7c9f4
Fast-forward
my_Lottery.py | 20 +++++++++++++++++++-
1 fileschanged, 19 insertions(+), 1 deletions(-)
createmode 100644 README
B、在project上修改子项目,并提交
重命名my_lottery.py文件为MyLottery.py
[root@oracle-vm lottery]# mv my_\Lottery.py MyLottery.py
切换到master分支
[root@oracle-vm lottery]# git checkout master
提交修改
[root@oracle-vm lottery]# git commit –a
更新子项目
[root@oracle-vm lottery]# git push
退出子项目所在目录,返回上一级
[root@oracle-vm lottery]# cd ..
[root@oracle-vm python]# git diff
diff --git a/python/lottery b/python/lottery
index bc1885d..d3726e8 160000
--- a/python/lottery
+++ b/python/lottery
@@ -1 +1 @@
-Subproject commitbc1885de22565f586524b6392cfdaa73b25a4d8d
+Subproject commitd3726e8ae718aae6f0606d0ad7aaf4c68e26d6b5
提交父项目
[root@oracle-vm python]# git commit -a
[master ef12680] update submodule lotterySigned-off-by: huangjunhjwsm1989@gmail.com
更新git server上的父项目
[root@oracle-vm python]# git push
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 359 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@github.com:hjwsm1989/learn.git
ab05bf2..ef12680 master ->master
在另外一台主机acer-pc上更新learn项目
从git server更新
git pull
更新子项目
git submodule update
或者
git submodule foreach git pull origin master