Git基本操作

目录

Git基本操作

查看配置

查看全局配置

1
git config --global --list

查看当前配置

1
git config --list

配置个人的用户名称和电子邮件地址

1
2
git config --global user.name "iflyelf"
git config --global user.email iflyelf@gmail.com

生成、添加 SSH 公钥

生成 SSH 公钥

1
2
3
4
ssh-keygen -t ed25519 -C '邮箱' -f ~/.ssh/gitee_id_rsa
-t key 类型
-C 注释
ssh-keygen -t ed25519 -C 'iflyelf@gmail.com' -f ~/.ssh/gitee_id_rsa

创建Git仓库

创建仓库目录文件并进入目录

1
2
mkdir -pv git-me
cd git-me

初始化Git仓库

1
git init

创建README文件并编辑

1
2
touch README.md
echo "学习git" >> README.md

增加文件到缓存区

增加所有修改的文件到缓存区(不包括删除的文件到缓存区)

1
git add .

增加所有修改的文件包含删除的文件到缓存区

1
git add -A

将暂存区内容添加到仓库中

1
2
3
git commit -am "创建README.md文件"
-a 类似 git add . 但不包含新增加的文件
-m 注释内容

增加远程仓库

1
2
3
4
git remote add <remote_name> <remote_url>
remote_name 远程仓库的名称
remote_url 远程仓库的 URL
git remote add origin https://gitee.com/iflyelf/git-me.git

将本地仓库的代码上传至远程仓库

1
2
git push <远程主机名> <本地分支名>:<远程分支名>
git push -u origin "master"

从远程获取代码并合并本地的版本

1
2
git pull <远程主机名> <远程分支名>:<本地分支名>
git pull origin master

Git 分支管理

创建分支

1
2
git branch 分支名称
git branch dev

切换分支

1
2
git checkout 分支名称
git checkout dev

创建分支并切换至新分支

1
2
3
4
5
git checkout -b 分支名称
git checkout -b test
or
git switch -c 分支名称
git switch -c test

查看分支

查看本地仓库所有分支

1
git branch

查看远程仓库所有分支

1
git branch -r

查看本地和远程仓库所有分支

1
git branch -a

推送本地分支到远程仓库分支

1
2
git push origin 本地分支名称
git push origin test

获取远程仓库代码到本地仓库分支

1
git fetch -p

合并分支

1
git merge 分支名称

删除分支

删除分支

1
git branch -d 分支名称

强制删除分支

1
git branch -D 分支名称

删除远程分支

1
git push origin --delete test

Git 标签

创建标签(git commit之后操作)

1
2
git tag -a 版本号 -m "版本描述"
git tag -a v1.0 -m "v1.0"

查看标签

1
git tag

查看该版本所修改的内容

1
2
git show 版本号
git show v1.1

删除标签

1
git tag -d 版本号

Git 版本管理

查看当前仓库状态

1
2
3
4
5
6
7
git status -s
M 被修改
A 增加
D 删除
R 重命名
C 被拷贝
U 被更新但是未合并

查看代码提交记录

查看历史提交记录

1
git log --oneline

以列表形式查看指定文件的历史修改记录

1
git blame 文件名

对比文件内容差异

1
git diff 文件名

恢复或撤销文件的更改

撤销单个文件的修改(丢弃所有未提交的更改)

1
2
git restore 文件名称
git checkout -- 文件名称

还原文件到暂存区的状态

1
git restore --staged 文件名称

还原所有未提交的更改(包括工作目录和暂存区的更改)

1
git restore .

还原文件到指定提交的状态

1
git restore --source=<commit> <file>

回退版本

回退所有内容到上一个版本

1
git reset HEAD^

回退到某个版本

1
git reset --soft HEAD

回退到某个版本回退点之前的所有信息

1
2
git reset --hard 版本号
git reset --hard v1.0

将本地的状态回退到和远程的一样

1
git reset --hard origin/master

相关内容

发现新版本

当前站点有新版本可用。