Git 记录
历史关系
查看日志
显示文件名和更改数量
git log --stat
显示文件名和更改类型
git log --name-status
图形显示
git log --graph --oneline
从某个分支提取到主分支的日志
git log from-branch..master --name-status
提取排除了合并的修订提交的列表。
git log from..to --no-merges --oneline
查看更改的差异
git diff --name-status
忽略缩进的差异
git diff --ignore-space-change
在特定文件的过去提交时点上查看内容
git show HEAD^:path/to/file
git show badcafe:path/to/file
将特定的文件恢复到过去的提交点。
git checkout HEAD^ path/to/file
git checkout badcafe path/to/file
远程对分支的操作
确认远程分支。
git branch -r
远程分支的检出
git checkout -b hogehoge origin/hogehoge
将除了master之外的分支推送到远程仓库
# ブランチ変更
git checkout hogehoge
# リモートのリポジトリにブランチを push
git push -u origin hogehoge
# リモートのリポジトリに別名(fugafugaという名前)でブランチを push
git push -u origin hogehoge:fugafuga
设置当前分支跟踪远程分支。
$ git branch --set-upstream origin/(ブランチ名)
删除远程分支(标签)。
我想要删除远程的hoge分支(或hoge标签)。
git push --delete origin hoge
设置git push的默认远程分支
git branch --set-upstream master remote/master
其他工作 (qí tā de zuò yè)
确认已合并的分支
git branch --merged
重新贴标签
调整提交的顺序
git rebase -i HEAD~2
导出
git checkout-index -a -f --prefix=../export_20121220/
准备个人使用的 .gitignore
执行以下操作后,~/.gitignore 将作为个人的 .gitignore 在任何仓库中被引用。
git config --global core.excludesfile ~/.gitignore
准备好 » 忽略文件
再创建一个工作目录
git-new-workdir . ../foobar
参考资料:git-new-workdir的用途方便 – #生存战略,这就是subtech。
在Topic分支中工作期间,通过rebase将发生在master分支上的更改合并。
$ git commit -v -a #トピックブランチの変更をとりあえずコミット(あとで取り消す)
$ git checkout master
$ git pull
$ git checkout topic-branch
$ git rebase master
$ git reset HEAD^ # 先にとりあえずコミットした修正を取り消す
参考:Git rebase 非常方便对吧?- Seasons.NET
如果发生冲突,请参考以下方法处理。
When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".
批量协同-连携
在bash提示符中显示分支名称,并通过使用”*”来识别是否已更改。
# for git
#
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
function parse_git_dirty {
[[ ! $(git status 2> /dev/null | tail -n1) =~ ^nothing\ to\ commit ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
export PS1='\h \[\033[1;33m\]\W \[\033[0m\]$(parse_git_branch)$ '
GitHub 接合
准备使用GitHub
设置用户名
git config --global user.name "Your name"
电子邮件
git config --global user.email "your_email@example.com"
将现有的本地存储库推送到 GitHub 存储库中。
-
- 公開鍵(SSH KEY)をアカウントに登録する
-
- git remote add (任意の名前) https://github.com/(Githubアカウント)/(リポジトリ).git
例) git remote add origin https://github.com/hoge-user/hoge-repos.git
git push -u origin master
Git的行为设置
参考:Git开始使用时要做的一些方便的设置:亚洲部落格
上色
git config --global color.ui auto
设置别名
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch
用户名和电子邮件地址
git config --global user.name "hoge"
git config --global user.email "hoge@example.com"
全球忽略设置
$ git config --global core.excludesfile ~/.gitignore_global
请将其配置为“~/.gitignore_global”。
参考文献:在全球范围内使用.gitignore的方法 – Qiita
更改推动时的行为
为了防止意外事故,只将当前正在进行的分支推送。
git config --global push.default simple
修改拉动时的行为
将–rebase设置为默认操作。
$ git config --global pull.rebase true
设定制表符控制字符的缩进宽度。
git config --global core.pager 'less -x4'
参考:为git-diff命令设置制表位宽度 | The Coding Journal ツ
在提交代码时使用vim编辑器
git config --global core.editor 'vim'
合并时不要快进。
git config --global --add merge.ff false
git config --global --add pull.ff only
参考资料:推荐使用git的merge –no-ff功能 – Qiita
请参考
- Set Up Git · github:help