Git初学入门指令

git基本指令

  1. 初始化
bash 复制代码
git init
  1. 查看状态
bash 复制代码
git status
  1. 新建文件
bash 复制代码
touch <filename>
  1. 加入暂存区
bash 复制代码
git add .   或者 git add -A  表示全部加入暂存区    git add <filename>单个文件加入暂存区
  1. 加入仓库
bash 复制代码
git commit -m "注释内容,加入这次提交的解释说明"
  1. 查看提交纪录
bash 复制代码
git log  

回退到指定提交版本:如果你知道要回退到的提交版本的哈希值(commit hash),可以使用以下命令回退到该提交的状态:请将 替换为你要回退到的提交版本的真实哈希值。

bash 复制代码
git reset --hard <commit hash>

回退到前一个提交版本:如果你只想回退到前一个提交版本,可以使用以下命令,这将把代码状态重置到前一个提交的状态:

bash 复制代码
git reset --hard HEAD^

回退到某个提交之前的版本:如果你想回退到指定提交之前的版本,可以使用以下命令,将 替换为你要回退到的提交之前的提交版本的真实哈希值:

bash 复制代码
git reset --hard <commit hash>^

这将把代码状态重置到指定提交之前的版本。

分支

  1. 以当前分支为基础新建分支并展开
bash 复制代码
git checkout -b <branchname>
  1. 列举所有的分支
bash 复制代码
git branch
  1. 单纯地切换到某个分支
bash 复制代码
git checkout <branchname>
  1. 删掉特定的分支
bash 复制代码
git branch -D <branchname>
  1. 合并分支
bash 复制代码
git merge <branchname>
  1. 若分支出现冲突,可用以下命令退出当前的合并
bash 复制代码
git merge --abort

push本地仓库

create a new repository on the command line

bash 复制代码
echo "# gogocode-copy" >> README.md
  git init
  git add README.md
  git commit -m "first commit"
  git branch -M main
  git remote add origin https://github.com/ZhiYao223/gogocode-copy.git
  git push -u origin main

push an existing repository from the command line

bash 复制代码
git remote add origin https://github.com/ZhiYao223/gogocode-copy.git
  git branch -M main
  git push -u origin main

要查看您当前仓库已经添加的远程仓库名称,您可以使用以下命令:

bash 复制代码
$ git remote

这个命令将显示所有已经添加的远程仓库的名称。每个名称占一行,并按照添加的顺序排列。

如果您使用该命令后没有看到任何输出,说明当前仓库尚未添加任何远程仓库。

如果您需要查看某个远程仓库的详细信息,可以使用以下命令:

bash 复制代码
$ git remote show <remote-name>

其中, 是所需远程仓库的名称。这个命令将显示有关该远程仓库的更详细的信息,包括 URL 和分支信息。例如:

bash 复制代码
$ git remote show origin

通常情况下,一个本地 Git 仓库只能有一个名为 "origin" 的远程仓库。如果您想更改远程仓库的 URL,可以使用以下命令:

bash 复制代码
$ git remote set-url origin https://github.com/ZhiYao223/gogocode-copy.git

这样会覆盖掉原有的远程仓库 URL。如果您希望删除已存在的名为 "origin" 的远程仓库,可以使用以下命令:

bash 复制代码
$ git remote remove origin

然后再执行添加远程仓库的操作:

bash 复制代码
$ git remote add origin https://github.com/ZhiYao223/gogocode-copy.git

这样就可以成功添加新的远程仓库了。

您可以使用以下命令查看当前仓库已经添加的远程仓库的地址:

bash 复制代码
$ git remote -v

这个命令会显示已经添加的所有远程仓库的名称和 URL。在显示的输出中,"origin" 是默认的远程仓库名称,而 "fetch" 和 "push" 分别表示对应的读取和写入地址。该命令的输出类似于:

bash 复制代码
origin  https://github.com/ZhiYao223/gogocode-copy.git (fetch)
origin  https://github.com/ZhiYao223/gogocode-copy.git (push)

这意味着当前仓库的"origin"远程仓库的读取和写入 URL 都是 https://github.com/ZhiYao223/gogocode-copy.git。

如果您想查看某个具体远程仓库的 URL,可以使用以下命令:

bash 复制代码
$ git remote get-url <remote-name>

其中, 是所需远程仓库的名称

相关推荐
课堂随想2 小时前
【git】通过配置 `init.defaultBranch`,自定义 Git 初始化时的默认分支名称,避免使用 `master` 并消除相关的警告提示
git
粥里有勺糖5 小时前
视野修炼-技术周刊第104期 | 下一代 JavaScript 工具链
前端·javascript·github
qq_377572775 小时前
github 远程仓库删除 本地重新上传
git
缘友一世6 小时前
macos安装git并连接gitCode远程仓库
git·macos·gitcode
熬夜学编程的小王9 小时前
C++类与对象深度解析(一):从抽象到实践的全面入门指南
c++·git·算法
课堂随想10 小时前
SHA-1 是一种不可逆的、固定长度的哈希函数,在 Git 等场景用于生成唯一的标识符来管理对象和数据完整性
git·哈希算法
易雪寒13 小时前
IDEA在git提交时添加忽略文件
java·git·intellij-idea
徒步僧14 小时前
mac中文件夹怎么显示.git隐藏文件
git·macos
纪伊路上盛名在15 小时前
如何初步部署自己的服务器,达到生信分析的及格线
linux·运维·服务器·python·学习·r语言·github