Git 命令行

0、基础设置

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

1、项目

1.1 克隆项目

1git clone https://[email protected]/xxxx/xxxx.git
2# or 
3git clone [email protected]:xxxxx/xxxxx.git
4# 克隆指定分支,到指定目录
5git clone -b branch-name repo path
6
7git config user.name "Your Name"
8git config user.email [email protected]

1.2 推送新项目到github

先在githubh上创建好项目

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

1.3 导出

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

2、分支

2.1 本地分支操作

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

2.2 远程分支操作

1# 查看远程仓库地址
2git remote -v
3
4# 更新远程分支列表
5# 如果你的 remote branch 不是在 origin 下,把 origin 换成你的名字
6# --prune 删除远程已经删除的分支
7git remote update origin --prune

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

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

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

2.4 在本地修改分支名称

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

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

 1# 拉取远程最新分支信息
 2git fetch origin
 3# 重命名本地分支
 4git branch -m old-branch-name new-branch-name
 5# 设置新的远程分支
 6git branch -u origin/new-branch-name
 7# or
 8git branch --set-upstream-to=origin/new-branch-name old-branch-name
 9# 验证配置结果
10git branch -vv
11# 删除旧的远程跟踪分支
12git branch -dr origin/old-branch-name
13```---
14
15## 3、标签
16
17```bash
18# 查看现有的标签
19git tag
20
21# 给最新的提交打标签
22git tag 1.0
23
24# 推送所有标签
25git push --tags
26```---