git使用
基本概念:
- 工作区:项目所在操作目录,实际操作项目的区域;
- 暂存区:用于记录工作区的工作,(修改)内容
- 仓库区:用于备份工作区的内容
- 远程仓库:远程主机上的GIT仓库
安装:
[root@gly-tencient ~]# yum install git
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
.......
Installed:
git.x86_64 0:1.8.3.1-23.el7_8
Dependency Installed:
perl-Git.noarch 0:1.8.3.1-23.el7_8
Complete!
[root@gly-tencient ~]#
初始配置:
配置命令:git config
- 配置所有用户:git config –system [选项]
- 配置文件位置:/etc/gitconfig
- 配置当前用户:git config –global [选项]
- 配置文件位置:~/.gitconfig
- 配置当前项目:git config [选项]
- 配置文件位置:project/.git/config
- 示例:
[root@gly-tencient ~]# git config --system user.name yuyuyuliang00 # 配置用户名
[root@gly-tencient ~]# git config --global user.name yuyuyuliang00@163.com # 配置用户邮箱
[root@gly-tencient ~]# cat /etc/gitconfig
[user]
name = yuyuyuliang00
[root@gly-tencient ~]# cat .gitconfig
[user]
name = yuyuyuliang00@163.com
[root@gly-tencient ~]# mkdir gitproject
[root@gly-tencient ~]# cd gitproject/
[root@gly-tencient gitproject]# git config --list # 查看配置信息
user.name=yuyuyuliang00
user.name=yuyuyuliang00@163.com
基本命令:
- 初始化仓库:git init # 意义:将某个项目目录变为git操作目录,生产git本地仓库。即该项目目录可以使用git管理。
- 查看本地仓库状态:git status # 说明:初始化仓库后默认工作在master分支,当工作区与仓库区不一致时会有提示。
- 将工作内容记录到暂存区:git add [files …]
例如1:将file.txt记录到暂存区:git add file.txt
例如2:将所有文件(不包含隐藏文件)记录到暂存区:git add *
[root@gly-tencient gitproject]# git init
Initialized empty Git repository in /root/gitproject/.git/
[root@gly-tencient gitproject]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@gly-tencient gitproject]# echo "hello world" > file.txt
[root@gly-tencient gitproject]# git status # 查看本地仓库状态
[root@gly-tencient gitproject]# echo "file2" > file2.txt
[root@gly-tencient gitproject]# echo "file3" > file3.txt
[root@gly-tencient gitproject]# git add * # 将所有文件(不包含隐藏文件)记录到暂存区
[root@gly-tencient gitproject]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file.txt
# new file: file2.txt
# new file: file3.txt
#
4. 取消文件暂存记录:git rm --cached [file]
[root@gly-tencient gitproject]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file.txt
# new file: file2.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file3.txt
5. 将文件同步到本地仓库:git commit [file] -m [message]
说明:-m表示添加一些同步信息,表达同步内容
[root@gly-tencient gitproject]# git commit -m "add some files" # 将暂存区所记录同步到仓库区
[master (root-commit) a80b1b6] add some files
2 files changed, 2 insertions(+)
create mode 100644 file.txt
create mode 100644 file2.txt
6. 查看commit日志记录:
git log
git log --pretty=oneline
[root@gly-tencient gitproject]# git log
commit a80b1b673da0115a45c90c27489d58356beba8a0
Author: guyueliang@foxmail.com <guyueliang@foxmail.com>
Date: Wed Apr 6 19:35:33 2022 +0800
add some files
[root@gly-tencient gitproject]# git log --pretty=oneline
a80b1b673da0115a45c90c27489d58356beba8a0 add some files
7. 查看工作区与仓库不同: git diff 文件
[root@gly-tencient gitproject]# cat file.txt # 编辑file.txt,在末尾添加一行
hello world
add a new line
[root@gly-tencient gitproject]# git diff file.txt # 查看工作区与仓库不同
diff --git a/file.txt b/file.txt
index 3b18e51..0854b27 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
hello world
+add a new line
8. 从仓库取回文件:git checkout [commit] -- [file]
[root@gly-tencient gitproject]# ls
file2.txt file3.txt file.txt
[root@gly-tencient gitproject]# rm file2.txt # 删除工作区中的文件
rm: remove regular file ‘file2.txt’? y
[root@gly-tencient gitproject]# git checkout -- file2.txt # 从本地仓库取回文件
[root@gly-tencient gitproject]# ls
file2.txt file3.txt file.txt
9. 移动或删除文件:
git mv [file] [path]
git rm [files]
注意:这两个操作会修改工作区内容,同时将操作记录提交到暂存区。
[root@gly-tencient gitproject]# mkdir fold
[root@gly-tencient gitproject]# git add file3.txt
[root@gly-tencient gitproject]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: file3.txt
# renamed: file.txt -> fold/file.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: fold/file.txt
#
[root@gly-tencient gitproject]# ls
file2.txt file3.txt fold
[root@gly-tencient gitproject]# git rm -f file3.txt
rm 'file3.txt'
[root@gly-tencient gitproject]# ls
file2.txt fold
[root@gly-tencient gitproject]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: file.txt -> fold/file.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: fold/file.txt
#
版本控制:
退回到上一个commit节点
git reset --hard HEAD^
注意:一个^表示回退1个版本,依次类推。当版本回退之后工作区会自动和当前coommit版本保持一致。
[root@gly-tencient gitproject]# echo "hello world" > file.txt
[root@gly-tencient gitproject]# git add file.txt
[root@gly-tencient gitproject]# git commit -m 'add file.txt'
[master (root-commit) 3cbe0f8] add file.txt
1 file changed, 1 insertion(+)
create mode 100644 file.txt
[root@gly-tencient gitproject]# echo "add a new line" >> file.txt
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
[root@gly-tencient gitproject]# git commit -m 'modify file.txt'
[master f6ca77c] modify file.txt
[root@gly-tencient gitproject]# vim file.txt
[root@gly-tencient gitproject]# git add file.txt
[root@gly-tencient gitproject]# git commit -m "modify file.txt secondly"
[master 3aeace1] modify file.txt secondly
1 file changed, 1 insertion(+)
[root@gly-tencient gitproject]# git status
# On branch master
nothing to commit, working directory clean
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
add a second line
[root@gly-tencient gitproject]# git reset --hard HEAD^ # 回退到上一次提交
HEAD is now at f6ca77c modify file.txt
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
查看所有操作记录:git reflog
[root@gly-tencient gitproject]# git reflog
f6ca77c HEAD@{0}: reset: moving to HEAD^
3aeace1 HEAD@{1}: commit: modify file.txt secondly
f6ca77c HEAD@{2}: commit: modify file.txt
3cbe0f8 HEAD@{3}: commit (initial): add file.txt
回退到指定的commit_id节点:git reset --hard [commit_id]
注意:最上面为最新记录,可以利用commit_id去往任何操作位置。
[root@gly-tencient gitproject]# git reset --hard 3cbe0f8 # 回退到第一次提交
HEAD is now at 3cbe0f8 add file.txt
[root@gly-tencient gitproject]# cat file.txt
hello world
标签:
标签:在项目的重要commit位置添加快照,保存当前的工作状态,一般用于版本迭代。
git tag [tag_name] [commit_id] -m [message]
说明:commit_id可以不写则默认标签标识最新的commit位置,message也可以不写,但是最好添加。
[root@gly-tencient gitproject]# git tag tag_latest -m 'version 1' # 创建本地标签
[root@gly-tencient gitproject]# git tag # 查看本地所有标签
tag_latest
[root@gly-tencient gitproject]# git show tag_latest # 查看本地某个 tag 的详细信息
tag tag_latest
Tagger: guyueliang@foxmail.com <guyueliang@foxmail.com>
Date: Wed Apr 6 22:24:14 2022 +0800
version 1
commit 3cbe0f8868af2bbb3c32d890af3f131b9c224f9c
Author: guyueliang@foxmail.com <guyueliang@foxmail.com>
Date: Wed Apr 6 20:09:34 2022 +0800
add file.txt
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..3b18e51
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+hello world
[root@gly-tencient gitproject]# git tag -d tag_latest # 删除本地的指定标签
Deleted tag 'tag_latest' (was 21c3379)
[root@gly-tencient gitproject]# git tag
保存工作区
- 保存工作区内容:git stash save [message] # 说明:将你工作去未提交的修改封存,让工作区回到修改前的状态
- 查看工作区列表:git stash list # 说明:最新保存的工作区在最上面
- 应用某个工作区:git stash apply [stash@{n}]
- 删除工作区:
- git stash drop [stash@{n}] # 删除一个工作区
- git stash clear # 删除所有保存的工作区
[root@gly-tencient gitproject]# git reset --hard 3aeace1 # 返回到最后一次commit
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
add a second line
[root@gly-tencient gitproject]# echo "add a third line" >> file.txt # 在文件末尾添加一行
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
add a second line
add a third line
[root@gly-tencient gitproject]# git stash save 'last modified' # 保存工作区内容,返回修改前的状态
Saved working directory and index state On master: last modified
HEAD is now at 3aeace1 modify file.txt secondly
[root@gly-tencient gitproject]# cat file.txt # 查看文件内容
hello world
add a new line
add a second line
[root@gly-tencient gitproject]# git stash list # 查看工作区列表
stash@{0}: On master: last modified
[root@gly-tencient gitproject]# git stash apply stash@{0} # 应用指定工作区
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: file.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gly-tencient gitproject]# cat file.txt # 检查文件内容
hello world
add a new line
add a second line
add a third line
[root@gly-tencient gitproject]# git stash clear # 删除所有保存的工作区
[root@gly-tencient gitproject]# git stash list
分支管理
定义:分支即每个人在原有代码(分支)的基础上建立自己的工作环境,单独开发,互不干扰。完成开发工作后再进行分支统一合并。
- 查看分支情况:git branch # 说明:前面带*的分支表示当前工作分支
- 创建分支:git branch [branch_name] # 说明:基于a分支创建b分支,此时b分支会拥有a分支全部内容,在创建b分支时最好保持a分支”干净”状态。
- 切换分支:git checkout 分支名
- git checkout -b asyn # 创建并且切换到这个分支
- 合并分支:git merge [分支名] # 把分支内容合并到主分支
冲突问题时合并分支过程中最为棘手的问题。
当分支合并时,原分支和以前发生了变化就会产生冲突,当合并分支时添加新的模块(文件),这种冲突可以自动解决,只需要自己决定commit操作即可。
当合并分支时两个分支修改了同一个文件,则需要手动解决冲突。
- 删除分支:
- git branch -d [branch] # 删除分支
- git branch -D [branch] # 删除没有被合并的分支
[root@gly-tencient gitproject]# git branch branch_new # 创建新的分支
[root@gly-tencient gitproject]# git branch # 查看分支情况
branch_new
* master
[root@gly-tencient gitproject]# git checkout branch_new # 切换到指定分支
Switched to branch 'branch_new'
[root@gly-tencient gitproject]# git branch
* branch_new
master
[root@gly-tencient gitproject]# vim file.txt # 在branch_new分支中,更改file.txt文件,在末尾添加一行add a fourth line
[root@gly-tencient gitproject]# cat file.txt
hello world
add a new line
add a second line
add a third line
add a fourth line
[root@gly-tencient gitproject]# git merge branch_new # 合并指定分支
Already up-to-date.
[root@gly-tencient gitproject]# git checkout master
M file.txt
Switched to branch 'master'
[root@gly-tencient gitproject]# cat file.txt # 检查合并后master分支中文件的内容
hello world
add a new line
add a second line
add a third line
add a fourth line
[root@gly-tencient gitproject]# git branch -d branch_new # 删除指定分支
Deleted branch branch_new (was d29655b).
[root@gly-tencient gitproject]# git branch
* master
远程仓库操作命令
所有操作在本地git仓库下进行
- 添加远程仓库:git remote add origin https://github.com/you_namef/python.git
- 删除远程主机:git remote rm
- 查看连接的主机:git remote # 注意:一个git项目连接的远程主机名不会重复
- 将本地分支推送给远程仓库,第一次推送分支使用-u表示远程对应分支建立关联
- git push origin master
- 删除远程分支
- git branch -a # 查看所有分支
- git push origin [:branch] # 删除远程分支
- git push origin --delete branch
- 其它推送方法:
- git push --force origin # 用于本地版本比远程版本旧时强行推送本地版本。
- git push origin [tag] # 推送本地标签到远程
- git push origin --tags # 推送本地所有标签到远程
- git push origin --delete tag v1.0 # 删除远程标签
- 从远程获取代码:
git pull # 从远程获取代码
git fetch origin master:tmp # 将远程分支master拉取到本地,作为tmp分支
区别:pull将远程内容直接拉取到本地,并和对应分支内容进行合并,fetch将远程分支内容拉取到本地,但是不会和本地对应分支合并,可以自己判断后再使用merge合并。
准备:
1、生成ssh密钥对
[root@gly-tencient gitproject]# ssh-keygen -t rsa -b 4096 -C "your_email_for_github"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:CbLKOG9MAMfB4pGO27TF4gAZ0yE2os7dvI49645dQuA your_email_for_github
The key's randomart image is:
+---[RSA 4096]----+
|=O+o |
|O=* |
|Bo.o. . |
|*o= *o . . |
| X E.+ S |
|.o=.. . |
|ooo o . |
| oo *.o |
| ..ooBo |
+----[SHA256]-----+
2、在github上建立一个repository,并且选择settings->Deploy keys->Add depoly keys,并且把 /root/.ssh/id_rsa.pub的内容复制到key文本框中,点击Add key:
3、连接测试:
[root@gly-tencient gitproject]# ssh -T git@github.com
Hi your_register_name/gittest! You've successfully authenticated, but GitHub does not provide shell access.
远程仓库测试
[root@gly-tencient gitproject]# git remote add orgin git@github.com:your_name/gittest.git # 添加远程仓库
[root@gly-tencient gitproject]# git remote # 查看远程仓库
orgin
[root@gly-tencient gitproject]# git push orgin master # 把本地分支推送到远程仓库
Counting objects: 12, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (12/12), 960 bytes | 0 bytes/s, done.
Total 12 (delta 0), reused 0 (delta 0)
To git@github.com:guyueliangssrf/gittest.git
* [new branch] master -> master
[root@gly-tencient gitproject]# git branch -a # 查看所有分支
* master
remotes/orgin/master
在github上查看推送结果:
拉取远程仓库到本地:
[root@gly-tencient gitproject]# git fetch orgin master:tmp # 拉取远程仓库master分支到本地本地作为tmp分支
From git@github.com:your_name/gittest
* [new branch] master -> tmp
[root@gly-tencient gitproject]# git branch -a # 查看所有分支
* master
tmp
remotes/orgin/master
[root@gly-tencient gitproject]# git checkout tmp # 切换到tmp分支
M file.txt
Switched to branch 'tmp'
[root@gly-tencient gitproject]# cat file.txt # 查看文件内容
hello world
add a new line
add a second line
add a third line
add a fourth line