Git 入门指令

107 阅读2分钟

配置全局commit的昵称和Email

git config --global user.name "your name"
git config --global user.email "your email"

查看配置:git config -l

SSH 公钥

git --version
ssh-keygen -t rsa

在当前用户目录下的.ssh文件中生成两个文件:id_rsa和id_rsa.pub,分别代表私钥和公钥.
注意:如果已有私钥,则覆盖,另外该目录下的knownhost文件会在首次访问仓库时提示生成。

远程操作

一系列与 Git 远程仓库操作相关的命令:

mkdir spflow
cd spflow
git init 
touch README.md
git add README.md
git add . // 所有文件添加到版本库
git commit -m "first commit"
// 本地文件与远程仓库关联
git remote add origin https://gitee.com/snackpub/spflow.git
git push -u origin "master"

切换为远程分支地址

git remote set-url origin git@codeup.aliyun.com:snack/a1.git
git remote -v

指定分支clone代码

git clone -b  xxx集团/4358-contract-supplement --depth 1 git@codeup.aliyun.com:xxxx/project.git


# 切换到指定的分支
git checkout <branch-name> 
# 例如: git checkout develop

####  **创建并切换到新分支**
git checkout -b <new-branch-name> 
# 等同于以下两条命令: 
git branch <new-branch-name>
git checkout <new-branch-name>
git checkout -b 华西第四医院/task/fixApibug origin/华西第四医院/task/fixApibug

git reset

  • git reset --soft HEAD~1:仅移动 HEAD 指针,不改变工作目录和暂存区的状态。
  • reset current branch to here:相当于 git reset --hard,会同时移动 HEAD 指针并将工作目录和暂存区的内容重置为指定提交的状态。

此时远程并没有更改,如果需要远程代码跟分支一致,需要强制push上去。

应用场景

当你需要撤销最近的一次提交,但想保留工作目录和暂存区中的更改时,可以使用 git reset --soft HEAD~1。

当你需要彻底撤销最近的更改,并希望工作目录和暂存区恢复到某个特定提交的状态时,可以使用 reset current branch to here 或者 git reset --hard。