欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Git教程(超级详细)

程序员文章站 2022-07-13 16:35:15
...

一、安装Git

Windows
直接在官网上下载

Linux
centOS:

$ yum install git

Ubuntu:

$ sudo apt-get install git

安装后需要设置下

$ git config --global user.name "Your Name"
$ git config --global user.email "aaa@qq.com"

二、创建仓库(repository)

创建目录

$ mkdir git_test
$ cd git_test/

初始化仓库

$ git init
Initialized empty Git repository in /home/dgut/git_test/.git/

这里我目录的位置是/home/dgut/git_test/

在git_test目录下编写readme.txt文件,尝试把文件放入到git仓库中

$ vim readme.txt 

文件内容为:

Git is a distributed version control system.
Git is free software.

第一步:把文件添加到暂存区中

$ git add readme.txt

第二步:把文件提交到仓库当前分支中

$ git commit -m "commit a readme.txt file"

-m 后面的内容为本次提交的说明

三、修改文件

(有时我们要修改已提交的文件)

修改完文件后用 git status 命令查看结果

$ git status
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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

还可以使用 git diff readme.txt 命令具体修改的内容

$ git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.

把修改后的文件再次提交

$ git add readme.txt
$ git commit -m "modified readme.txt"
[master e475afc] modified readme.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

四、版本查看和版本回退

刚刚我们修改了两次readme.txt,一次有两次版本
版本1:commit a file
版本2:modified readme.txt
使用 git log 命令可以查看我们各次提交的信息

$ git log
commit 82f64ce7fcc19b4768c827e15e76e4274b4c8f8b
Author: Ruifengs <aaa@qq.com>
Date:   Tue Jun 2 10:45:42 2020 +0800

    modified readme.txt

commit 8fe95a336daf3a2aa5fed444ce35c941045711e5
Author: Ruifengs <aaa@qq.com>
Date:   Tue Jun 2 10:24:13 2020 +0800

    commit a file

还有一个让输出更简洁的命令 git log --pretty=oneline

$ git log --pretty=oneline
82f64ce7fcc19b4768c827e15e76e4274b4c8f8b modified readme.txt
8fe95a336daf3a2aa5fed444ce35c941045711e5 commit a file

有时我们发现修改后的版本不满意,使用 git reset 命令可以回退到上一个版本 。
HEAD表示当前版本;上一个版本就是HEAD^;上上一个版本就是HEAD^^,版本过多的话,写很多个^有点不现实,比如往上50个版本的话,可以写成HEAD~50。

$ git reset --hard HEAD^
HEAD is now at 8fe95a3 commit a file

查看下现在仓库的状态,可以看到已经回到第一个版本

$ git log
commit 8fe95a336daf3a2aa5fed444ce35c941045711e5
Author: Ruifengs <aaa@qq.com>
Date:   Tue Jun 2 10:24:13 2020 +0800

    commit a file

git要回到某一版本还可以指定版本号恢复,版本号写前几位便可,现在我们试着回到第二个版本——modified readme.txt

$ git reset --hard 82f64ce
HEAD 现在位于 82f64ce modified readme.txt

git还提供了查看历史操作的命令

$ git reflog
82f64ce aaa@qq.com{0}: reset: moving to 82f64ce
8fe95a3 aaa@qq.com{1}: reset: moving to HEAD^
82f64ce aaa@qq.com{2}: commit: modified readme.txt
8fe95a3 aaa@qq.com{3}: commit (initial): commit a file

五、撤销修改

工作区撤销:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout – 下面试一下:

# 一开始readme.txt内容如下
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.

# 修改后的readme.txt文件内容
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.
Error

# 使用 git checkout -- readme.txt 撤回修改
$ git checkout -- readme.txt

# 再次查看readme.txt文件内容,可以发现已经撤回到原来的样子
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.

暂存区撤销:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD ,第二步用 git checkout –
例子如下:

# 原文件内容
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.

# 修改后内容如下,并提交到暂存区
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.
Error
$ git add readme.txt
# 查看下仓库状态
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)

	modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

# 当提交后你后悔了,想让内容恢复到原来的样子,需要输入以下命令
$ git reset HEAD readme.txt
Unstaged changes after reset:
M	readme.txt
$ git checkout -- readme.txt

# 查看结果,发现已经撤回到原来的样子
$ cat readme.txt
Git is a distributed version control system.
Git is free software.

版本库撤销:如果已经提交了不合适的修改到版本库时,想要撤销本次提交,可以参考第四节版本查看和版本回退

六、删除文件

# 提交一个测试文件
$ git add delete_test
$ git commit -m "delete"
[master 02d3fc2] delete
 1 file changed, 1 insertion(+)
 create mode 100644 delete_test
 
 # 第一步:本地手动删除文件
 $ rm -f delete_test
 
# 第二步:从版本库删除
$ git rm delete_test
rm 'delete_test'

# 第三步:提交到版本库
$  git commit -m "delete file"
[master 519fbc0] delete file
 1 file changed, 1 deletion(-)
 delete mode 100644 delete_test

七、添加远程库和克隆仓库

登录github,创建新的Repository
Git教程(超级详细)
在本地进入到目录下,然后运行命令 git remote add origin ‘你远程仓库的地址’ 表示连接到你的远程仓库(通过ssh连接加密),此处略写,可以参照:https://www.runoob.com/git/git-remote-repo.html

$ cd /home/badou/git_test/first_repository
$ git remote add origin aaa@qq.com:Ruifengs/create_test.git

然后通过 git push -u origin master 命令把本地库的所有内容推送到远程库上,-u参数的作用是关联本地的master分支和远程的master分支,这个参数只需在第一次推送时加,往后的push只需运行命令:git push origin master

$ git push -u origin master
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (20/20), 1.64 KiB | 560.00 KiB/s, done.
Total 20 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To github.com:michaelliao/learngit.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

git clone 要克隆的仓库地址 命令可以从远程仓库中克隆一个repository

$ git clone aaa@qq.com:Ruifengs/Spring-test.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (3/3), done.

八、分支管理

1.创建与分支管理

创建dev分支,然后切换到dev分支, -b参数表示创建并切换,有两种命令可以实现操作,建议使用第二种。

$ git checkout -b dev
Switched to a new branch 'dev'
$ git switch -c dev
Switched to a new branch 'dev'

git branch命令可以查看所有分支,*号表示分支。

$ git branch
* dev
  master

试着在dev分支上修改readme.txt文件并提交。

# 在readme.txt加上
I'm test modify

$ cat readme.txt 
Git is a distributed version control system.
Git is free software.
I'm test modify

# 提交
$ git add readme.txt
$ git commit -m "in dev modify"
[dev 73a458a] in dev modify
1 file changed, 1 insertion(+)

# 回到master分支上
$ git checkout master
Switched to branch 'master'

# 查看readme.txt会发现刚刚加上的句子消失了,原因就是刚刚是在dev分支上修改
# master分支此刻的提交点并没有变
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.

# 因此我们需要把dev分支合并到master分支
$ $ git merge dev
Updating d46f35e..b17d20e
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
 
 # 查看readme.txt,
$ cat readme.txt 
Git is a distributed version control system.
Git is free software.
I'm test modify

合并完分支后就可以把分支删除了,命令:git branch -d <name> 注意:如果要强行删除未合并的分支 命令为:git branch -D <name>

$ git branch -d dev
Deleted branch dev(was 73a458a)

切换分支有两种命令方式,建议使用第二种
git checkout <name>或者git switch <name>

2、Bug分支

应用场景:有时我们正在dev分支进行一个工作时,突然收到一个修复bug的紧急任务,而我们手上的工作又还没做完,还无法提交,这时候我们可以先把工作区的工作“保存起来”,新建一个分支来处理bug,之后再回到dev分支,恢复之前的工作现场.。

# git status 可以看到现在在dev分支
$ git status
# On branch dev
# 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

# git stash 命令把工作区的保存起来
$ git stash
Saved working directory and index state WIP on dev: 7e97564 modify
HEAD is now at 7e97564 modify

# git stash list 命令可以查看stash里被保存的分支,可以看到dev已经在里面
$ git stash list
aaa@qq.com{0}: WIP on dev: 7e97564 modify

# 工作区也已经没有任务
$ git status
On branch dev
nothing to commit, working directory clean
# 现在开始进行修复bug,假设要修复的bug是在readme.txt添加"This is a bug!"

# 先回到master主支
$ git checkout master
Switched to branch 'master'

# 创建bug分支并切换
$ git checkout -b bug
Switched to a new branch 'bug'

# 本地修改完readme.txt提交到版本库
$ git add readme.txt
$ git commit -m "bug over"
[bug cf35c20] bug over
 1 file changed, 1 insertion(+), 1 deletion(-)
 
# 提交后切换到master,把bug分支合并到master主支
$ git checkout master
Switched to branch 'master'
$ git merge bug
Updating caa245d..c               f35c20
Fast-forward
 readme.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

# 然后删掉bug分支
$ git branch -d bug
Deleted branch bug (was cf35c20).
# 回到dev分支,继续做我们的工作
$ git checkout dev
Switched to branch 'dev'

# 查看工作区,发现目前是空的
$ git status
# On branch dev
nothing to commit, working directory clean

# 现在运行 git stash apply 命令把stash的内容恢复到工作区
$ git stash apply
# On branch dev
# 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

# 恢复后需要把stash清理掉 运行 git stash drop 命令.
# 或者 直接运行 git stash pop 恢复内容的同时把stash内容也删了
$ git stash drop
Dropped refs/aaa@qq.com{0} (882631f33554c87b756faa489d05ee957330920a)

3、冲突解决

情景:有a、b同学,共同开发一个项目,a同学、b同学同时把项目克隆到本地,a同学在readme.txt文件添加了一句话"I’m a",提交到本地库后,push到远程仓库;另一边,b同学同样对readme.txt修改,添加了一句"I’m b’",然后提交,当要push到远程仓库后报错。这是因为a同学最新提交和b同学试图推送的提交有冲突,要先解决冲突才能push成功。例子如下:

# a 同学

# 修改后添加到暂存区,提交 
$ $ git add readme.txt
$ git commit -m "add a"
[dev 84d2900] add a
 1 file changed, 1 insertion(+)

# push到远程仓库
$ git push origin dev
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To aaa@qq.com:Ruifengs/create_test.git
   246bdf7..84d2900  dev -> dev      
# b同学

# 修改后添加到暂存区,提交 
$ git add readme.txt
$ git commit -m "add b"
[dev 84d2900] add b
 1 file changed, 1 insertion(+)
 
 # 在push的时候出出错了,提示我们需要先把远程库的内容pull下来,手动合并后再push
$ git push origin dev
To aaa@qq.com:Ruifengs/create_test.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'aaa@qq.com:Ruifengs/create_test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

# pull dev分支的内容到本地,但是最后提示merge失败,是因为冲突还没有解决。
$ git pull origin dev
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:Ruifengs/create_test
 * branch            dev        -> FETCH_HEAD
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

# 修改readme.txt手动合并内容
$ vim readme.txt

# 提交到版本库
$ git add readme.txt 
$ git commit -m "combine a and b"
[dev 6403c29] combine a and b

# 再次提交就成功啦
$ git push origin dev
Counting objects: 10, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 507 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To aaa@qq.com:Ruifengs/create_test.git
   84d2900..6403c29  dev -> dev

4、标签管理

# 创建标签,默认标签是打在最新提交的commit上,也就是HEAD
$ git tag v2.0

# 查看所有标签
$ git tag
v2.0

# git show <tagname>
$ git show v2.0
commit 69b25eb9ed9cb3feb530b8faa1eee0e00309ebfa
Author: Ruifengs <aaa@qq.com>
Date:   Thu Jun 4 10:06:18 2020 +0800

    add words

diff --git a/readme.txt b/readme.txt
index 9247db6..6b3d2c4 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 Git is a distributed version control system.
 Git is free software.
+Hello,I'm secone_test

# 还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字
$ git tag -a v0.1 -m "version 0.1 released" 519fbc0
$ git show v0.1
tag v0.1
Tagger: Ruifengs <aaa@qq.com>
Date:   Thu Jun 4 13:43:39 2020 +0800

version 0.1 released

commit 519fbc0d4fc428415622d8cff7a5bbf49d4cd006
Author: Ruifengs <aaa@qq.com>
Date:   Tue Jun 2 13:28:45 2020 +0800

    delete file

diff --git a/delete_test b/delete_test
deleted file mode 100644
index 47b0484..0000000
--- a/delete_test
+++ /dev/null
@@ -1 +0,0 @@
-wawa

# 删除本地标签
$ git tag -d v2.0
Deleted tag 'v2.0' (was 69b25eb)

# 如果要删除远程标签,先删除本地标签,再运行下面命令
$ git push origin :refs/tags/v9.0

# 推送某个本地标签到远程仓库
$ git push origin v9.0
Total 0 (delta 0), reused 0 (delta 0)
To aaa@qq.com:Ruifengs/create_test.git
 * [new tag]         v9.0 -> v9.0
 
# 一次性推送所有标签到远程库
$ git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 166 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To aaa@qq.com:Ruifengs/create_test.git
 * [new tag]         2.0 -> 2.0
 * [new tag]         v0.1 -> v0.1

referencehttps://www.liaoxuefeng.com/wiki/896043488029600

相关标签: github git

上一篇: Git最详细教程

下一篇: Git详细教程