Git命令

[toc]

clone

从git远端仓库下载到本地

git clone https://github.com/YuAlfred/Typora.git

pull

从远程获取代码并合并本地的版本。

# 将远程主机 origin 的 master 分支拉取过来,与本地的 brantest 分支合并。
git pull origin master:brantest
# 如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
git pull origin master

查看config

git config --list

查看整个仓库的状态

git status

扩大Buffer

# 表示设置为5G
sudo git config --global http.postBuffer 5242880000
sudo git config --global https.postBuffer 5242880000
sudo git config --global ssh.postbuffer 5242880000

设置代理

全局http,https代理

git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy https://127.0.0.1:7890

取消http,hppts代理

有时候git Push报错 是因为想走ssh但是设置了http代理,此时关闭http代理即可

git config --global --unset http.proxy

只对GitHub代理

git config --global http.https://github.com.proxy https://127.0.0.1:7890
git config --global https.https://github.com.proxy https://127.0.0.1:7890

Sockets代理(只对GitHub)

git config --global http.https://github.com.proxy socks5://127.0.0.1:7890
git config --global https.https://github.com.proxy socks5://127.0.0.1:7890

取消代理

git config --global --unset http.proxy
git config --global --unset https.proxy

创建本地仓库并提交

创建本地仓库(在仓库目录下)

git init

将目录下所有文件添加到本地仓库

git add .

提交本地仓库,添加注释

git commit -m 'commit'

添加远程仓库地址(需要现在GitHub上建好),命名为origin

git remote add origin [仓库地址.git]

推传到远程仓库

git push -u origin master

提交修改并推送

回退版本 git-revert

git-revert会回退制定提交并生成新的记录,看起来就是一次正常代码修改后的提交,所以不需要强制push,不会对别人造成影响。

用法

回退最近一次提交

git revert HEAD

回退上上次提交

git revert HEAD^

不生成提交记录

也可通过--no-commit配置该命令不生成提交记录,无痕回退:

git revert HEAD --no-commit

回退多个提交

指定提交范围可以回退多个提交

git revert <first_bad_commit>...<last_bad_commit>

克隆指定目录

$ git init
$ git remote add origin  远程仓库地址
$ git config core.sparsecheckout true
$ echo "子目录名称" >> .git/info/sparse-checkout
$ git pull origin master

忽略某些文件

简单的忽略文件可以直接添加到.gitignore里面去

全局忽略

可以新建一个存储全局忽略的文件,并将其设置入Git全局设置中

# 创建.gitignore_global存储规则
touch ~/.gitignore_global
# 将该文件中的规则设置为全局规则
git config --global core.excludesfile ~/.gitignore_global

删除已上传需要忽略的文件

如果有的文件已经被误上传 可以用如下方法直接删除

# 从cached中删掉这个文件
git rm --cached [filename]
# 然后commit就行啦
git commit -a -m "remove file don't need"