git branch 不要分页 设置git branch不分页显示
1 git config --global pager.branch false
使用git合并特定的commit 比如要在master分支上合并其他分支的commit,commit id是37c89cc, 则命令是:
1 2 git checkout master git cherry-pick 37c89cc
本地同步远端已删除分支 参考: https://stackoverflow.com/questions/5751582/fetch-from-origin-with-deleted-remote-branches 使用git fetch -p来同步远端已经删除的分支。-p就是--prune修剪的意思。
Github中clone私有仓库 Github中,对于私有仓库如果直接git clone, 会提示fatal: repository 'https://github.com/xxxxxx/yyyyyy.git/' not found 有两个方法可以用于克隆Github上的私有仓库
使用ssh key方式
使用显式的用户名密码的https方式
Github中使用账号密码方式clone私有仓库 在git clone命令的url中,添加上github的用户名和密码即可。
1 git clone https://username:password@github.com/xxxxxx/yyyyyy.git
Github中使用ssh key方式clone私有仓库 在~/.ssh/config中配置github中使用的key, 类似如下
1 2 3 4 Host "github.com" HostName "github.com" User "username" IdentityFile ~/.ssh/id_rsa_for_github
接下来就可以clone代码了
1 git clone https://github.com/xxxxxx/yyyyyy.git
Reference
查看git中所有分支的日志 使用命令git log --all来查看所有分支的提交日志
在所有的branch中搜索 方法一 使用命令git grep "search-words" $(git rev-list --all) 来在所有的branch的commit中进行搜索。
该方法有一个问题,当git rev-list --all列出的commit很多时,可能会报-bash: /usr/bin/git: Argument list too long的错误
方法二 使用命令git rev-list --all | xargs git grep <expression> 来搜索,则不会有Argument list too long的问题
搜索限制在某个目录下的方法 以目录lib/util为例:
1 git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util
两个命令中都带了lib/util,因为rev-list只是将有lib/util变更的revisions全部找出来,所以还需要在git grep中添加-- lib/util将搜索限定在找出来的revisions中的路径lib/util下。
其他用法 还有一些其他的搜索方法,参考How to grep (search) committed code in the Git history
git diff 不要分页 使用命令git --no-pager diff设置git diff时不要分页。
查找git中文件是否被ignore以及是被哪个配置文件ignore的 使用git check-ignore *来查找目录下是否有被ignore的文件。 使用git check-ignore -v *来查找目录下ignore的文件,以及是被哪个配置文件ignore的
例子:
1 2 3 4 5 $ git check-ignore * node_modules $ git check-ignore -v * .gitignore:37:/node_modules node_modules $
例子中可以看到当前目录node_modules是被git ignore的,是被当前目录下的.gitignore文件的37行的配置/node_modules给ignore的。
如果要检查单个文件或者目录是否被ignore了,那么将上述命令的*换为对应的文件即可, 如:
1 2 3 $ git check-ignore -v node_modules/ Gemfile .gitignore:37:/node_modules node_modules/ $
可见Gemfile没有被ignore, node_modules/被文件.gitignore设置为ignore了。
参考:
git命令获取当前分支 使用git symbolic-ref --short -q HEAD来获取当前分支
参考:
获取git仓库中所有的branch 命令:
1 2 3 git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done git fetch --all git pull --all
一个完整的clone所有分支的例子
1 git clone ssh://user_name@git.xxxx.com/git/repository.git && cd repository && git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done && git fetch --all && git pull --all && cd ..
参考:
查找文件记录(包含已删除的文件) 如下命令可以用来查找文件的提交记录,即使文件已经被删除了。
指定具体文件路径查找,适用于还记得文件路径的情况
1 git log --all --full-history -- <path-to-file>
只显示最后一个和文件相关的commit
1 git log --all --full-history -1 -- <path-to-file>
模糊查找,适用于只记得部分文件名的情况
1 git log --all --full-history -- "**/thefile.*"
使用git log查找出文件所对应的commit后,使用git show显示具体commit情况
1 git show <SHA> -- <path-to-file>
如果想要具体的文件,那么git checkout <SHA>临时切到指定分支查看对应的文件,
1 git checkout <SHA> -- <path-to-file>
如果是查找删除的文件,那么<SHA>后面需要还有一个脱字符^
使用<SHA>^, 因为当前<SHA>中的文件已经被删除了, 需要切换到前一个版本
1 git checkout <SHA>^ -- <path-to-file>
Git从远端分支checkout 如果只有一个remote, 直接git checkout branch_name就行。
如果有多个remote,则需要显示指定remote
1 git checkout -b fix-failing-tests origin/fix-failing-tests
参考:
Macos下tree显示中文 1 2 3 4 5 # 安装tree brew install tree` # 中文不乱码加参数 -N tree -N
CentoOS 7 最小化安装后可能需要的一些命令 1 2 3 4 5 yum install wget curl yum install unzip yum install tree yum install net-tools yum install lszrz