瘦了就帅了 / Git 命令行

Created Mon, 16 May 2022 14:32:27 +0800 Modified Sat, 07 Mar 2026 13:25:09 +0000
1193 Words

0、基础设置

# 查看代理
git config --global --get http.proxy
git config --global --get https.proxy
# 设置代理
git config --global http.proxy http://[username:passwrod@]ip or URL:port 
git config --global https.proxy http://[username:passwrod@]ip or URL:port
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 只代理github.com
git config --global --get http.https://github.com.proxy
git config --global --get https.https://github.com.proxy
git config --global http.https://github.com.proxy http://[username:passwrod@]ip or URL:port
git config --global https.https://github.com.proxy http://[username:passwrod@]ip or URL:port
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
# push设置
git config --global push.default simple

1、仓库

1.1 克隆仓库

git clone https://[email protected]/xxxx/xxxx.git
# or 
git clone [email protected]:xxxxx/xxxxx.git
# 克隆指定分支,到指定目录
git clone -b branch_name repo path

git config user.name "Your Name"
git config user.email [email protected]

1.2 推送新内容到github

先在githubh上创建好项目

git init
git add .
git status -s
git config user.name "Your Name"
git config user.email [email protected]
git commit --amend --reset-author
git commit -m "first commit"
git remote add origin [email protected]:Youname/repo_name.git
git push -u origin master

1.3 导出仓库

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

1.4 修改仓库URL

# 查看当前的仓库URL
git remote -v

# 修改ssh url
git remote set-url origin [email protected]:用户名/仓库名.git

1.5 移动本地仓库路径

# 查看具体变化内容
# 如果输出 old mode 100644 → new mode 100755,表示权限变化
git diff
# 忽略文件权限差异
# 仅当前仓库生效
git config core.filemode false
# 或全局设置
git config --global core.filemode false
# 重置工作区
git reset --hard HEAD

2、分支

2.1 本地分支操作

# 拉取
git fetch origin branch_name
git pull origin branch_name

# 强制覆盖本地分支
git fetch --all
git reset --hard origin/branch_name
git pull

# 查看分支
git branch -a

# 基于现有分支创建新分支
# 查看当前分支
git status
# 基于现有分支创建一个分支,不切换到该分支
git branch new_branch_name
# 基于现有分支创建一个分支,并切换到该分支
git switch -c new_branch_name
git checkout -b new_branch_name

# 创建孤儿分支
# 创建并切换到孤儿分支
git checkout --orphan new_branch_name
# 清空暂存区和工作区
git rm -rf .
# 创建初始提交
git commit --alow-empty -m "init commit"
# 推送分支到运程
git push origin new_branch_name

# 切换分支
git checkout branch_name

# 删除分支
git branch -d branch_name

# 发布本地分支
git push 远程主机名 本地分支名:远程分支名

# 合并分支
# 将 a 分支合并到 b 分支
git checkout b
git merge a
git push

# 推送
# 查看本地项目状态
git status -s
# 添加文件 | 添加目录 | 添加所有内容
git add  file | dir | . 
# 删除 add 的文件
git rm [-r] --cached file | .
# 提交已修改的文件,但不提交未跟踪的文件
git commit -m "message" --untracked-files=no
# 提交已删除的文件
git commit -m "message" -a
# 推送到远程库
git push

2.2 远程分支操作

# 查看远程仓库地址
git remote -v

# 更新远程分支列表
# 如果你的 remote branch 不是在 origin 下,把 origin 换成你的名字
# --prune 删除远程已经删除的分支
git remote update origin --prune

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

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

# 1 查看本地分支是否发生变化
git branch -a

# 2 如本地库处于另一个分支中,需将本地分支重置回原分支
git checkout branch_name
git reset --hard

# 3 如本地分支没有变化,则强行 pull 覆盖本地文件
git fetch --all
git reset --hard origin/branch_name
git pull

2.4 在本地修改分支名称

# 查看本地分支
git branch -a
# 切换到要重命名的分支
git checkout branch_name
# 重命名分支
git branch -m new_branch_name
# 上传新分支
git push origin -u new_branch_name
# 删除原分支
git push origin --delete old_branch_name

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

# 拉取远程最新分支信息
git fetch origin
# 重命名本地分支
git branch -m old_branch_name new_branch_name
# 设置新的远程分支
git branch -u origin/new_branch_name
# or
git branch --set-upstream-to=origin/new_branch_name old_branch_name
# 验证配置结果
git branch -vv
# 删除旧的远程跟踪分支
git branch -dr origin/old_branch_name
```---

## 3、标签

```bash
# 查看现有的标签
git tag

# 给最新的提交打标签
git tag 1.0

# 推送所有标签
git push --tags
```---