git基本操作

git强制覆盖

1
2
3
git fetch --all
 git reset --hard origin/master
git pull

git强制覆盖本地命令(单条执行)

1
git fetch --all && git reset --hard origin/master && git pull

fork分支更新

1
2
3
4
5
git remote -v 
git remote add upstream git@github.com:xxx/xxx.git
git fetch upstream
git merge upstream/master
git push

git查看某个文件的提交历史

1
2
3
4
git log --pretty=oneline 文件名

# 接下来使用git show显示具体的某次的改动
git show <git提交版本号> <文件名>

记住密码

如果你用git从远程pull拉取代码,每次都要输入密码,那么执行下面命令即可

1
git config --global credential.helper store

这个命令则是在你的本地生成一个账号密码的本子似的东东,这样就不用每次都输入了(但是还得输入一次)

删除缓存区

1
2
3
4
5
6
7
8
9
10
11
1 git rm --cached +文件名 ->这个命令不会删除物理文件,只是将已经add

进缓存的文件删除。

2 git rm --f +文件路名 ->这个命令不仅将文件从缓存中删除,还会将

物理文件删除,所以使用这个命令要谨慎。

3 若删除已经添加缓存的某一个目录下所有文件的话需要添加一个参数 -r

git rm -r --cached 文件名

撤销commit

写完代码后,我们一般这样

1
2
3
git add . //添加所有文件

git commit -m "本功能全部完成"

执行完commit后,想撤回commit,怎么办?

这样凉拌:

1
git reset --soft HEAD^

这样就成功的撤销了你的commit

注意,仅仅是撤回commit操作,您写的代码仍然保留。

说一下个人理解:

HEAD^的意思是上一个版本,也可以写成HEAD~1

如果你进行了2次commit,想都撤回,可以使用HEAD~2

至于这几个参数:

–mixed

意思是:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作
这个为默认参数,git reset –mixed HEAD^ 和 git reset HEAD^ 效果是一样的。

–soft

不删除工作空间改动代码,撤销commit,不撤销git add .

–hard

删除工作空间改动代码,撤销commit,撤销git add .

注意完成这个操作后,就恢复到了上一次的commit状态。

顺便说一下,如果commit注释写错了,只是想改一下注释,只需要:

1
git commit --amend

此时会进入默认vim编辑器,修改注释完毕后保存就好了。

代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

# http协议,1081端口修改成自己的本地代理端口
git config --global http.proxy http://127.0.0.1:1081
git config --global https.proxy https://127.0.0.1:1081

#
下面是仅仅针对github进行配置,让github走本地代理,其他的保持不变;
# socks5协议,1080端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

# http协议,1081端口修改成自己的本地代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:1081
git config --global https.https://github.com.proxy https://127.0.0.1:1081

其他几个相关命令:
# 查看所有配置
git config -l

# reset 代理设置
git config --global --unset http.proxy
git config --global --unset https.proxy

win git识别linux软链

1
git clone -c core.symlinks=true git@xxxxxx.git

或者打开conf

1
core.symlinks=true

说明: https://github.com/git-for-windows/git/wiki/Symbolic-Links

win git 大小写

1
git config core.ignorecase false