Skip to content

克隆远程仓库 ​

Shell
git clone 项目的地址
git clone 项目的地址
Shell
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.

查看远程仓库 ​

如果想查看你已经配置的远程仓库服务器,可以运行 git remote 命令。 它会列出你指定的每一个远程服务器的简写。 如果你已经克隆了自己的仓库,那么至少应该能看到 origin ——这是 Git 给你克隆的仓库服务器的默认名字

Shell
git remote
git remote

你也可以指定选项 -v,会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL。

Shell
git remote -v
git remote -v
Shell
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)

如果你的远程仓库不止一个,该命令会将它们全部列出。 例如,与几个协作者合作的,拥有多个远程仓库的仓库看起来像下面这样:

JavaScript
bakkdoor  https://github.com/bakkdoor/grit (fetch)
bakkdoor  https://github.com/bakkdoor/grit (push)
cho45     https://github.com/cho45/grit (fetch)
cho45     https://github.com/cho45/grit (push)
defunkt   https://github.com/defunkt/grit (fetch)
defunkt   https://github.com/defunkt/grit (push)
koke      git://github.com/koke/grit.git (fetch)
koke      git://github.com/koke/grit.git (push)
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)
bakkdoor  https://github.com/bakkdoor/grit (fetch)
bakkdoor  https://github.com/bakkdoor/grit (push)
cho45     https://github.com/cho45/grit (fetch)
cho45     https://github.com/cho45/grit (push)
defunkt   https://github.com/defunkt/grit (fetch)
defunkt   https://github.com/defunkt/grit (push)
koke      git://github.com/koke/grit.git (fetch)
koke      git://github.com/koke/grit.git (push)
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)

添加远程仓库 ​

git clone 命令会自行添加远程仓库, 如何自己来添加它。运行 git remote add <shortname> <url>

添加一个新的远程 Git 仓库,同时指定一个方便使用的简写

Shell
git remote add pb https://github.com/paulboone/ticgit
git remote add pb https://github.com/paulboone/ticgit
Shell
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
pb  https://github.com/paulboone/ticgit (fetch)
pb  https://github.com/paulboone/ticgit (push)
$ git remote -v
origin  https://github.com/schacon/ticgit (fetch)
origin  https://github.com/schacon/ticgit (push)
pb  https://github.com/paulboone/ticgit (fetch)
pb  https://github.com/paulboone/ticgit (push)

现在你可以在命令行中使用字符串 pb 来代替整个 URL。 例如,如果你想拉取 Paul 的仓库中有但你没有的信息,可以运行 git fetch pb

Shell
git fetch pb
git fetch pb
Shell
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
 * [new branch]      master     -> pb/master
 * [new branch]      ticgit     -> pb/ticgit
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
 * [new branch]      master     -> pb/master
 * [new branch]      ticgit     -> pb/ticgit

推送到远程仓库 ​

git push <remote> <branch> 当你想要将 master 分支推送到 origin 服务器时(再次说明,克隆时通常会自动帮你设置好那两个名字), 那么运行这个命令就可以将你所做的备份到服务器

JavaScript
git push origin master
git push origin master

只有当你有所克隆服务器的写入权限,并且之前没有人推送过时,这条命令才能生效。 当你和其他人在同一时间克隆,他们先推送到上游然后你再推送到上游,你的推送就会毫无疑问地被拒绝。 你必须先抓取他们的工作并将其合并进你的工作后才能推送。 阅读 Git 分支 了解如何推送到远程仓库服务器的详细信息。

如果出现 443 报错则使用以下命令将代理关闭后再进行提交

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

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

git config --global --unset https.proxy

查看某个远程仓库 ​

如果想要查看某一个远程仓库的更多信息,可以使用 git remote show <remote> 命令。 如果想以一个特定的缩写名运行这个命令,例如 origin,会得到像下面类似的信息

Shell
git remote show origin
git remote show origin
Shell
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)
* remote origin
  Fetch URL: https://github.com/schacon/ticgit
  Push  URL: https://github.com/schacon/ticgit
  HEAD branch: master
  Remote branches:
    master                               tracked
    dev-branch                           tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

它同样会列出远程仓库的 URL 与跟踪分支的信息。 这些信息非常有用,它告诉你正处于 master 分支,并且如果运行 git pull, 就会抓取所有的远程引用,然后将远程 master 分支合并到本地 master 分支。 它也会列出拉取到的所有远程引用。

远程仓库的重命名与移除 ​

可以运行 git remote rename 来修改一个远程仓库的简写名。 例如,想要将 pb 重命名为 paul,可以用 git remote rename 这样做

Shell
git remote rename pb paul
git remote rename pb paul
Shell
$ git remote
origin
paul
$ git remote
origin
paul

值得注意的是这同样也会修改你所有远程跟踪的分支名字。 那些过去引用 pb/master 的现在会引用 paul/master。

如果因为一些原因想要移除一个远程仓库——你已经从服务器上搬走了或不再想使用某一个特定的镜像了, 又或者某一个贡献者不再贡献了——可以使用 git remote remove 或 git remote rm

Shell
git remote remove paul
git remote remove paul
Shell
$ git remote
origin
$ git remote
origin

一旦你使用这种方式删除了一个远程仓库,那么所有和这个远程仓库相关的远程跟踪分支以及配置信息也会一起被删除。

撤销提交 ​

如果你已经把提交推送到远程仓库(比如 GitHub),撤销上一次提交需要小心操作,尤其是如果你不是唯一一个在使用这个分支。


✅ 推荐方式:使用 git revert(不会改写历史)

这是最安全的做法,适用于公共分支(如 main、master)。

bash
git revert HEAD
git revert HEAD

这会创建一个新的提交,撤销上一次提交的更改,但不会修改历史记录。之后你可以正常推送到远程:

bash
git push origin 你的分支名
git push origin 你的分支名

⚠️ 高风险方式:使用 git reset(会改写历史)

如果你确定没有别人基于这个提交工作,或者你在自己的分支上,可以用 git reset 强制回退:

bash
# 回退到上一次提交之前,但保留更改在本地 
git reset --soft HEAD~1

# 或者彻底丢弃更改(慎用) 
git reset --hard HEAD~1
# 回退到上一次提交之前,但保留更改在本地 
git reset --soft HEAD~1

# 或者彻底丢弃更改(慎用) 
git reset --hard HEAD~1

然后强制推送到远程:

bash
git push origin 你的分支名 --force
git push origin 你的分支名 --force

⚠️ 注意:

  • 如果你用的是 共享分支(比如团队都在用的 main),不要用 --force,会覆盖别人的历史。
  • 如果你已经 reset --hard 并强制推送了,其他协作者需要重新拉取并处理冲突。

✅ 总结一句话:

  • 公共分支 → 用 git revert
  • 私人分支 → 可以用 git reset --hard + git push --force
AI助手