Git 命令行

0、基础设置

 1# 查看代理
 2$ git config --global --get http.proxy
 3$ git config --global --get https.proxy
 4# 设置代理
 5$ git config --global http.proxy http://[username:passwrod@]<ip or URL>:port 
 6$ git config --global https.proxy http://[username:passwrod@]<ip or URL>:port
 7# 取消代理
 8$ git config --global --unset http.proxy
 9$ git config --global --unset https.proxy
10
11# 只代理github.com
12$ git config --global --get http.https://github.com.proxy
13$ git config --global --get https.https://github.com.proxy
14$ git config --global http.https://github.com.proxy http://[username:passwrod@]<ip or URL>:port
15$ git config --global https.https://github.com.proxy http://[username:passwrod@]<ip or URL>:port
16$ git config --global --unset http.https://github.com.proxy
17$ git config --global --unset https.https://github.com.proxy
18# push设置
19$ git config --global push.default simple

1、项目

1.1 克隆项目

 1$ git clone https://xxxx@bitbucket.org/xxxx/xxxx.git
 2# or 
 3$ git clone git@github.com:xxxxx/xxxxx.git
 4# 克隆指定分支,到指定目录
 5$ git clone -b <branch> <repo> <path>
 6
 7$ git config user.name "Your Name"
 8$ git config user.email you@example.com
 9
10
11

1.2 推送新项目到github

先在githubh上创建好项目

1$ git init
2$ git add .
3$ git status -s
4$ git config user.name "Your Name"
5$ git config user.email you@example.com
6$ git commit --amend --reset-author
7$ git commit -m "first commit"
8$ git remote add origin git@github.com:Youname/repo_name.git
9$ git push -u origin master

1.3 导出

1$ git archive --format zip -0 \                         # 使用zip格式,不压缩
2              --output output.zip \                     # 输出的文件名
3              --remote git@github.com:Cuile/NMP.git \   # 远程项目地址
4              master \                                  # 分支名
5              ./                                        # 输出到当前目录

2、分支

2.1 本地分支操作

 1# 拉取
 2$ git pull
 3# 强制覆盖本地分支
 4$ git fetch --all
 5$ git reset --hard origin/<branch>
 6$ git pull
 7
 8# 推送
 9# 查看本地项目状态
10$ git status -s
11# 添加文件 | 添加目录 | 添加所有内容
12$ git add < file | dir | . >
13# 删除 add 的文件
14$ git rm [-r] --cached <file | .>
15# 提交已修改的文件,但不提交未跟踪的文件
16$ git commit -m <"message"> --untracked-files=no
17# 提交已删除的文件
18$ git commit -m <"message"> -a
19# 推送到远程库
20$ git push
21
22# 创建分支
23# 只创建一个分支
24$ git branch <branch-name>
25# 创建一个分支并切换到该分支
26$ git checkout -b <branch-name>
27
28# 查看分支
29$ git branch -a
30
31# 切换分支
32$ git checkout <branch name>
33
34# 删除分支
35$ git branch -d <branch name>
36
37# 发布本地分支
38$ git push <远程主机名> <本地分支名>:<远程分支名>
39
40# 合并分支
41# 将 a 分支合并到 b 分支
42$ git checkout b
43$ git merge a
44$ git push

2.2 远程分支操作

 1# 拉取
 2$ git fetch origin <branch>
 3$ git checkout -b <branch> origin/<branch>
 4$ git pull origin <branch>
 5
 6# 查看远程仓库地址
 7$ git remote -v
 8
 9# 更新远程分支列表
10# 如果你的 remote branch 不是在 origin 下,把 origin 换成你的名字
11# --prune 删除远程已经删除的分支
12$ git remote update origin --prune

2.3 删除本地文件后,从远端重新拉取最新版本

git提示: up-to-date. 但未从远端得到文件

 1# 1 查看本地分支是否发生变化
 2$ git branch -a
 3
 4# 2 如本地库处于另一个分支中,需将本地分支重置回原分支
 5$ git checkout <branch name>
 6$ git reset --hard
 7
 8# 3 如本地分支没有变化,则强行 pull 覆盖本地文件
 9$ git fetch --all
10$ git reset --hard origin/<branch name>
11$ git pull

2.4 重命名分支

 1# 查看本地分支
 2$ git branch -a
 3# 切换到要重命名的分支
 4$ git checkout <branch-name>
 5# 重命名分支
 6$ git branch -m <new branch-name>
 7# 上传新分支
 8$ git push origin -u <new branch-name>
 9# 删除原分支
10$ git push origin --delete <old branch-name>

2.5 更改本地分支对应的远程分支

1# dev为本地分支,origin/master为远程分支
2$ git branch --set-upstream-to=origin/master dev
3# or
4$ git branch --set-upstream dev origin/master 

3、标签

1# 查看现有的标签
2$ git tag
3
4# 给最新的提交打标签
5$ git tag 1.0
6
7# 推送所有标签
8$ git push --tags

相关专栏文章