git notes
clone a remote repository
sshkey-gen -t rsa -C "email-addr"
, generate public and private keys in ~/.ssh/- copy public keys to github->settings->sshkey
- git clone XXX , clone repository to your local machine.
create and push your local branch to remote
- after you clone, enter the project and see which branch you are in, use
git branch
- to create a local branch and develop in the local branch,
git checkout -b localbranch
, this command will create a local branch named localbranch as switch to it.git branch
you will see you are in localbranch now. git branch
to see all the branches,git branch -a
orgit branch -r
will show all the remote branches.- to trace the remote branch,
git branch --set-upstream-to=origin/oracle
, this will create relations between localbranch and origin/oracle, andgit status
will show the trace. - now, if you
git add
andgit command
, andgit push
, you will push the changes to the remote branchorigin/oracle
. - git pull to fetch remote branch
ATTENTION: 只有本地分支跟踪远程分支之后,才能直接使用 git push 提交到远程分支,不然,需要制定当前要提交到的远程分支,比如 git push HEAD:oracle,HEAD 表示一个指向当前分支的指针
git pull origin/oracle to pull remote branch oracle.
- git log – 显示当前版本为止所有的提交日志,但是如果当版本回退的时候,git log 是查看不到回退之前的日志信息的,通过
git reflog
查看,能看到所有的 commit id - 回退版本
git reset --hard HEAD^
, 表示回退当前版本的前一个版本,HEAD 指向当前版本,即每次 git commit 提交后就会产生一个新的版本,此时HEAD指向的就是这个新的版本,HEAD^
表示前一个版本,HEAD^^
,表示当前版本往前追溯两个版本,也可以用HEAD~2
表示,这种表示方法更加简洁。 - 使用git init 之后,文件夹中会产生一个 .git 的隐藏文件夹,这个就是git 的暂存区,外面就是工作区,我们编辑文件就是在工作区。git add 的时候,就是将修改添加到暂存区,每一个新版本都是依赖于前一个版本的,每次记录的都是修改的内容。 git commit 就是把提交到暂存区的所有内容,创建一个版本记录,然后将HEAD 指向这个新的版本,也就是当前分支了。
- git add 添加, git commit 提交, git checkout 撤销, git rm 删除
- git reset 重置后取消暂存,然后git checkout 撤销修改
- git diff HEAD — code.txt,code.txt 已经经过了修改,但是还没有 git add/git commit 提交,这个命令就是比较当前分支 HEAD 中的code.txt 与当前经过修改的code.txt 进行对比。
- 设置全局变量 git config –global
git config --global user.name ""
git config --global user.email ""
git config --list # show all the configs
复制代码
保存用户名和密码,在使用 http 的时候可以使用。
git help credentials # show help info
git config credential.helper store # store password next time you pull
复制代码
查看远程分支
git branch -a
git branch -r
复制代码
都可以查看远程分支
git remote show origin
复制代码
可以查看remote地址,远程分支,还有本地分支与之相对应关系等信息
git remote prune origin
复制代码
可以删除那些远程不存在的分支(本地仍然存在)
修改注释
git log
复制代码
查看提交的日志信息
如果需要修改刚提交的日志,使用
git commit --amend
复制代码
通过 vim 直接修改,git log
查看修改后的日志
标签操作
add tags
git tag v1.0 # add light weight tag
git tag -a v1.0 -m "release 1.0" # add tag with comment
复制代码
show tags
git show v1.0
git tag -l
复制代码
share tag – push to remote repository
git push origin v1.0 # push one tag to remote
git push origin --tags # push multi tags
复制代码
delete tag
git tag -d v1.0 # delete local tag v1.0
复制代码
use git push <remote> :refs/tags/<tagname>
to delete remote tag, for example
git push origin :v1.0
复制代码
checkout tag
git checkout v1.0
复制代码
修改标签对应的内容
checkout 时,分离了HEAD指针,此时修改的内容不属于任何分支,不能直接提交。
1 先创建一个新分支,进行修改
git checkout -b newbranch v1.0
# modify
git add .
git commit -m "v1.1"
复制代码
2 删除远程tag
git push origin :v1.0
复制代码
3 在新分支创建新tag,然后push 到远程
git tag -a v1.1 -m "release-1.1"
git push origin v1.1
复制代码
git 回滚到某个 commit
git reset --hard HEAD^ 回退到上个版本
git reset --hard HEAD~3 回退到前3次提交之前,以此类推,回退到n次提交之前
git reset --hard commit_id 退到/进到 指定commit的sha码
复制代码
查看某一个文件的某一行代码
git blame filename # 查看某一个文件的每一行由谁修改
git blame -L n1,n2 filename # 查看文件从 n1 到 n2 行由谁修改
复制代码
子模块 submodules
子模块的添加
添加子模块非常简单,命令如下:
git submodule add <url> <path>
复制代码
其中,url为子模块的路径,path为该子模块存储的目录路径。
执行成功后,git status会看到项目中修改了.gitmodules
,并增加了一个新文件(为刚刚添加的路径)
git diff --cached
查看修改内容可以看到增加了子模块,并且新文件下为子模块的提交hash摘要
git commit
提交即完成子模块的添加
子模块的使用
克隆项目后,默认子模块目录下无任何内容。需要在项目根目录执行如下命令完成子模块的下载:
git submodule init
git submodule update
复制代码
或:
git submodule update --init --recursive
复制代码
执行后,子模块目录下就有了源码,再执行相应的makefile即可。
子模块的更新
子模块的维护者提交了更新后,使用子模块的项目必须手动更新才能包含最新的提交。
在项目中,进入到子模块目录下,执行 git pull更新,查看git log查看相应提交。
完成后返回到项目目录,可以看到子模块有待提交的更新,使用git add,提交即可。
删除子模块
有时子模块的项目维护地址发生了变化,或者需要替换子模块,就需要删除原有的子模块。
删除子模块较复杂,步骤如下:
rm -rf 子模块目录 删除子模块目录及源码
vi .gitmodules 删除项目目录下.gitmodules文件中子模块相关条目
vi .git/config 删除配置项中子模块相关条目
rm .git/module/* 删除模块下的子模块目录,每个子模块对应一个目录,注意只删除对应的子模块目录即可
复制代码
执行完成后,再执行添加子模块命令即可,如果仍然报错,执行如下:
git rm --cached 子模块名称
完成删除后,提交到仓库即可
错误信息
.gitmodules 中未找到子模组的url
no url found for submodule path
复制代码
可能是当前仓库的根目录中的 .gitsubmodules
文件出了问题,修改这个文件,比如
[submodule "parser"]
path = src/camphor/rule/parser
url = http://192.168.20.28/wuzhenyu/dbproxy-interface-api.git
复制代码
NOTE: 特别注意,path 路径为当前仓库根目录的相对路径,且路径最后不能以 /
结束
然后在执行
git submodule update --init --recursive
复制代码
即可更新子模块