「版本控制」 git基础命令

Posted by Dawn-K's Blog on June 30, 2020

git

[toc]

参考资料

介绍

git基本命令

配置公私钥

  1. 本地生成公私钥
1
2
3
4
5
6
7
8
9
git config --global user.name  "xxx"
# 这个要和账号的邮箱一样
git config --global user.email "xxx@xxx"

# 这个如果设置完git的初始路径,就不一定是~了,本质上是移动到C:/Windows/User/用户名/.ssh
cd ~/.ssh
ssh-key -t ras -C "xxx@xxx"

然后就会有 id_rsa 和 id_rsa.pub 
  1. 公钥放入远程的代码仓库

gitee: 设置->SSH公钥

github: setting->SSH and GPG keys

初始化

创建一个git仓库, 创建之后就会在当前目录生成一个.git的文件

1
2
git init

git add: 把文件添加到缓冲区

1
2
3
4
5
6
# 添加单个文件
git add filename
# 添加所有文件到缓冲区
git add .
# 加all可以添加被手动删除的文件, 而加"."不行
git add --all

git rm: 删除文件

1
git rm filename

git commit: 提交

提交缓冲区的所有修改到仓库(注意: 如果修改了文件但是没有add到缓冲区, 也是不会被提交的)

1
2
git commit -m "提交的说明"

commit可以一次提交缓冲区的所有文件

git status: 查看git库的状态

未提交的文件, 分为两种, add过已经在缓冲区的, 未add过的

1
git status

git diff: 比较

如果文件修改了, 还没有提交, 就可以比较文件修改前后的差异

1
2
git diff filename

git log: 查看日志

1
git log

git reset: 版本回退

可以将当前仓库回退到历史的某个版本

1
2
3
4
5
6
7
8
9
10
git reset

# 第一种用法 回退到上一个版本(HEAD代表当前版本, 有一个^代表上一个版本, 以此类推)

git reset --hard HEAD^

# 第二种用法 回退到指定版本(其中d7b5是想回退的指定版本号的前几位)

git reset --hard d7b5

git reflog: 查看命令历史

查看仓库的操作历史

1
git reflog

git branch: 分支管理

查看分支

1
2
3
4
5
# 查看分支的情况, 前面带*号的就是当前分支

git branch

创建分支

1
2
3
4
5
6
7
8
# 基于当前commit创建分支 
git branch 分支名

# 切换当前分支到指定分支
git checkout 分支名

# 创建分支并切换到创建的分支
git checkout  -b 分支名

合并

合并某分支的内容到当前分支

git merge 分支名

git常见问题

配置多个远程仓库

# 查看当前配置的远程仓库
git remote -v

# 添加仓库
git remote add repo1 ${your_remote_repo_address}
git remote add repo2 ${your_another_remote_repo_address}

# Push
git push repo1
git push repo2

要求输入账号密码

这个往往是远程仓库采用了http的方式导致的. 使用git remote修改为ssh方式的地址即可.

git status中文乱码

git config --global core.quotepath false

修改上一条commit

git commit –amend

# 如果有提交遗漏的文件,此时可以add上,然后在编辑器中关闭(让vscode的git正确显示)
git add your_file
# 然后修改上次的命令,这一步主要是修改上一步的提交信息的,刚才add的文件无需在此操作
git commit --amend

git commit –amend 后出现 Waiting for your editor

原文:

hint: Waiting for your editor to close the file… “C:\Soft\Microsoft VS Code\Code.exe” –wait: C:\Soft\Microsoft VS Code\Code.exe: No such file or directory error: There was a problem with the editor ‘“C:\Soft\Microsoft VS Code\Code.exe” –wait’.

配置一下 vscode 的打开方式即可

git config --global core.editor 'code --wait'