Skip to content
pezy edited this page Jul 26, 2018 · 5 revisions

How to set proxy in windows git


use git config -l can show all the configuration of your git.

  • set
    git config --global http.proxy http://10.22.98.21:8080
  • remove
    git config --global (or --system or --local) --unset http.proxy
  • remove all
    git config --global (or --system or --local) --unset-all http.proxy

为 git 设置 Shadowsocks 代理

HTTP URL:

git config --global http.proxy socks5://127.0.0.1:1080

如果使用的是 git URL:

vim ~/.ssh/config
Host github.com
ProxyCommand connect -H 127.0.0.1:1080 %h %p

参考自: https://gist.github.com/laispace/666dd7b27e9116faece6

git如何回滚

刚才一次误操作仍然心有余悸,本想图省事直接在网页上为blog的post branch增加一个文件夹和一个markdown文档,结果不知怎么搞得合并到主gh-pages分支上去了。结果主分支一下子空了许多。立刻掏出命令行,找回之。

首先需要知道的是,你想回滚到哪一个提交版本? commits

如图所示,右边那串字符就是你的commits id了。复制你要回滚的版本id。 然后输入以下命令:

git reset --hard <tag/branch/commit id>

你会长舒一口气,至少硬盘上看,文件都回来了。 然后你将这些修改重新commit覆盖服务器端版本即可。

需要注意的是:本地branch为A,服务器端branch为B,请问如何正确的push?

git push origin A:B

这个冒号,可以理解为to的意思,from A to B.

Fork的repo拉取并合并主repo的步骤

首先要先确定一下是否建立了主repo的远程源:

git remote -v

如果里面只能看到你自己的两个源(fetch 和 push),那就需要添加主repo的源:

git remote add upstream URL
git remote -v

然后你就能看到upstream了。

如果想与主repo合并:

git fetch upstream
git merge upstream/master

如何引入别人的repo

Github 上常常有很多非常好的工具,如检测内存泄漏的,单元测试的。如何将这些工具引入到自己的 repo 目录里呢?

下面以 Catch(C++ 单元测试框架) 为例:

  • 添加submodule:
git submodule add git@github.com:philsquared/Catch.git
  • 更新submodule:
# should init
git submodule update --init
git submodule update
  • 查看状态:
git submodule status
  • 更改URL
# Modify your .gitmodule file to use the new URL
git submodule sync

发起一个pull request,我只想提交某一个commit怎么办?

情景浮现:Cpp-Primer之前的章节被发现有Bug,可是我正在进行第七章的撰写,我不希望把这个半成品发给 @Mooophy,只想pull request给他Bug的修改。

# 新建一个branch来缓存origin repo
git checkout -b upstream origin/master
# 只截取某一个commit,cherry-pick,真是个好名字
git cherry-pick <SHA hash of commit>
# 推送到origin repo就行了
git push origin upstream

当然,这几步其实都是在自己的fork内"演练"。你还需要在Github上点一下 pull request 按钮。然后万事大吉啦。

删除远程分支

git push origin :del_branch_name

该死的行尾空白,能否忽略?

由于经常在Windows和Mac上切换,两个系统的行尾空白符却不同意,造成很多不必要的diff,去掉这该死的差别:

git stripspace < README.md

完了,这个不该commit的,能取消吗?

有时候会出现不想commit的东西给commit了,尤其是喜欢用git commit -am的人。如何回退?

git reset --soft HEAD~1
# 可以重新调整
# 如果你想直接用上次的log,可以这样:
git commit -c ORIG_HEAD
# 如果你连log都要改,请使用常规的commit

修改远程地址

git remote set-url origin git://new.url.here

这个可能会有一个后遗症,就是修改后,再 pull 的时候可能会出现 fatal: refusing to merge unrelated histories 的错误。

此时应该加上 --allow-unrelated-histories,如下所示:

git pull origin master --allow-unrelated-histories

快速查看冲突列表

git diff --name-only --diff-filter=U

快速回滚到某历史版本

git reset --hard <old-commit-id>
git push -f origin branch

Using this is dangerous in a collaborative environment: you're rewriting history.

查看提交历史中的 diff

  • 看单个文件的全部 diff:
git log --full-diff -p your_file_path
  • 看最后两个历史 Commits 的 diff:
git log -p -2

see more at: http://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

如何处理 Windows 平台与 Unix/Linux 平台换行符的不一致带来的大量 diff

git config --global core.autocrlf input
# Configure Git on OS X or Linux to properly handle line endings

git config --global core.autocrlf true
# Configure Git on Windows to properly handle line endings

参考:https://help.github.com/articles/dealing-with-line-endings/#platform-all

如何将 .gitignore 里写过的后缀文件,强制 add

git add --force my/ignore/file.foo

远程有多个分支,我想将本地切换到远程的某一个分支该怎么做?

git checkout -b test origin/test

在 git server 上创建一个新的 repo

mkdir foo.git
cd foo.git
git init --bare
chown -R git:git .

Clone this wiki locally