Github的高阶命令集合

来源:互联网 发布:php可以开发游戏吗 编辑:程序博客网 时间:2024/04/28 02:34

因为日常使用github经常会上stackoverflow搜索一些命令的使用,为了方便自己查询,在此总结出来。持续更新。。。

在commit之前查看修改的内容

来源:http://stackoverflow.com/questions/4456532/how-can-i-see-what-has-changed-in-a-file-before-committing-to-git

在commit之前,往往会想查看一下自己到底修改了哪些内容,避免垃圾信息过多。具体命令及解释为

# show differences between index and working tree# that is, changes you haven't staged to commitgit diff [filename]# show differences between current commit and index# that is, what you're about to commitgit diff --cached [filename]# show differences between current commit and working treegit diff HEAD [filename]

将其他分支的内容直接替换到主分支

来源:http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch

在创建一个新分支的时候,往往会造成新分支和主分支的conflict过多,难以合并。这个时候通常会希望用新分支替换主分支,同时保留commit记录。以下命令假设新分支名称为“new”:

git checkout newgit merge -s ours mastergit checkout mastergit merge new

如果不行的话,也可以尝试直接修改分支名称。但是这样会导致commit记录丢失:

git branch -m master old-mastergit branch -m new mastergit push -f origin master

修复commit记录

可能由于各种疏忽,导致git的commit记录中的邮箱和用户名错误,导致无法关联到用户。对于git打卡强迫症简直不能忍,还好可以修复。

具体看另一篇博文:git修复commit记录。

删除.DS_Store或其他垃圾文件

来源:http://stackoverflow.com/questions/107701/how-can-i-remove-ds-store-files-from-a-git-repository

Mac会给每个文件夹都添加一个.DS_Store文件,然而并不希望它们出现在git中。可以按如下操作:

  • 首先删除该文件:find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch --cached
  • 然后在.gitignore中添加.DS_Store

拉取远程分支

来源:http://stackoverflow.com/questions/9537392/git-fetch-remote-branch

下面的命令会创建一个本地的分支daves_branch,并将远程的分支daves_branch同步下来。当再次修改并push后,修改也会同步到远程的分支上去。

git checkout --track origin/daves_branch

--track其实就是git checkout -b [branch] [remotename]/[branch]的一种省略形式。因为通常情况下,本地分支和远程分支的名字会保持一致。

超大文件意外提交

来源:http://stackoverflow.com/questions/21168846/cant-remove-file-from-git-commit

在提交的时候,可能不小心在目录下新增了一个大于100MB的文件(数据库文件,打包后程序等)。这时,如果push到github的,会爆出remote: error: File XYZ is 226.32 MB; this exceeds GitHub's file size limit of 100.00 MB。不幸的是,尽管你尝试删除这个文件,修改.gitignore等等,都不能解决这个问题。这是因为,这个文件已经进入到commit记录中去了。要解决这个问题,就必须修改对应的commit记录,将文件从commit记录中删除。

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME"

这个命令,会遍历所有的commit记录,尝试删除对应的文件。删除的时候一定要小心,别把需要的文件给删除了,那就得不偿失了。

0 0
原创粉丝点击