一、git使用前准备
git本地仓库与github远程仓库连接时要有认证,是通过SSH的公钥进行认证的。创建SSH公钥并将添加到github中
在用户目录下查看.ssh文件夹
如果没有这个文件夹或者文件,需要手动创建
在Program Files文件夹下找到Git/bin目录,双击sh.exe
输入以下命令,一路回车会创建.ssh文件夹和其中的密钥
$ ssh-keygen -t rsa -C "xxxx@xx.xx"
用记事本打开id_rsa.pub文件,将其中的公钥复制。
打开https://github.com,打开设置页
将前面复制的公钥粘贴到文本框中
现在就建立了本地仓库与远程仓库之前的认证。验证
二、github远程仓库创建
本地仓库与远程仓库的连接有三种方式:
- 初始化本地仓库,然后将本地仓库推送到远程仓库;
- 将一个已有的本地仓库推送到远程仓库;
- clone一个远程仓库到本地。
三、git基本操作
初始化:git init
新建一个文件夹gitdemo并初始化
$ cd g:
Administrator@User-14 MINGW64 /g
$ mkdir gitdemo
Administrator@User-14 MINGW64 /g (master)
$ cd gitdemo
Administrator@User-14 MINGW64 /g/gitdemo
$ git init
Initialized empty Git repository in G:/gitdemo/.git/
Administrator@User-14 MINGW64 /g/gitdemo (master)
$
初始化会在当前文件夹下创建一个.git的文件夹,此文件夹存储着管理当前目录内容所需的仓库数据。
查看本地仓库状态: git status
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git status
On branch master //当前在master分支
No commits yet //没有过提交记录
nothing to commit (create/copy files and use "git add" to track) //暂存区没有要提交的文件
在当前文件夹下创建一个文件后再次查看仓库状态
$ git status
On branch master
No commits yet //没有过提交记录
Untracked files: //存在一个没有被git跟踪的文件
(use "git add <file>..." to include in what will be committed)
test.txt
//没有需要提交的,但是存在没有被git跟踪的文件
nothing added to commit but untracked files present (use "git add" to track)
将文件添加到暂存区:git add 文件名/git add .
git add .代表的是添加所有的文件到暂存区
上面创建的test.txt文件,由于没有git操作因此是不会被git跟踪的,git向仓库中添加要先将工作区文件添加到暂存区后才能提交到仓库。
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git add test.txt //将文件添加到暂存区
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git status
On branch master
No commits yet
Changes to be committed: //暂存区中没有提交的文件
(use "git rm --cached <file>..." to unstage)
new file: test.txt
当test.txt文件内容更改时,再次查看状态
$ git status
On branch master
No commits yet
Changes to be committed: //暂存区中存在没有被提交的文件
(use "git rm --cached <file>..." to unstage)
new file: test.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: test.txt
提交暂存区文件到本地仓库:git commit -m “description”
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git commit -m "first" //提交暂存区文件
[master (root-commit) dc1a2af] first
1 file changed, 1 insertion(+)
create mode 100644 test.txt
========text.txt文件改变了===========
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
=================添加改动的文件到暂存区后再提交========================
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git add .
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git commit -m "second"
g[master 3e6b279] second
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git status
On branch master
nothing to commit, working tree clean
查看提交日志:git log /git log –pretty=short /git log test.txt /git log -p /git log -p test.txt
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git log
commit 3e6b279f80f8496bcc7ffff73ab3d6e9727aeed3 (HEAD -> master)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:31:35 2021 +0800
second
commit dc1a2afcb88674bd7ff34afe5d5ef9d93d2253b4
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:27:10 2021 +0800
first
=================显示信息简写===============
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git log --pretty=short
commit 3e6b279f80f8496bcc7ffff73ab3d6e9727aeed3 (HEAD -> master)
Author: Administrator <woodlum@vip.qq.com>
second
commit dc1a2afcb88674bd7ff34afe5d5ef9d93d2253b4
Author: Administrator <woodlum@vip.qq.com>
first
==============查看指定文件的提交日志=============
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git log test.txt
commit 3e6b279f80f8496bcc7ffff73ab3d6e9727aeed3 (HEAD -> master)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:31:35 2021 +0800
second
commit dc1a2afcb88674bd7ff34afe5d5ef9d93d2253b4
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:27:10 2021 +0800
first
==============查看文件的详细改动-p=============
$ git log -p //或者git log -p test.txt
commit 3e6b279f80f8496bcc7ffff73ab3d6e9727aeed3 (HEAD -> master)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:31:35 2021 +0800
second
diff --git a/test.txt b/test.txt
index e4a7dd9..f6ce845 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-aaaaa //改动之前内容
No newline at end of file
+aaaaabb //此处能看到内容变化
No newline at end of file
commit dc1a2afcb88674bd7ff34afe5d5ef9d93d2253b4
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 11:27:10 2021 +0800
first
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..e4a7dd9
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+aaaaa //此处能看到内容变化
No newline at end of file
查看操作日志:git reflog
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git reflog
d76d79a (HEAD -> master, A) HEAD@{0}: reset: moving to d76d79a
d76d79a (HEAD -> master, A) HEAD@{1}: reset: moving to d76d79a
31a3585 HEAD@{2}: reset: moving to 31a3585
d76d79a (HEAD -> master, A) HEAD@{3}: merge A: Fast-forward
31a3585 HEAD@{4}: checkout: moving from A to master
d76d79a (HEAD -> master, A) HEAD@{5}: commit: a1
31a3585 HEAD@{6}: checkout: moving from master to A
31a3585 HEAD@{7}: commit (initial): m1
查看工作区和暂存区差异:git diff
//先修改test.txt文件内容
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git diff
diff --git a/test.txt b/test.txt
index f6ce845..4e7ce06 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-aaaaabb
No newline at end of file
+aaaaabbccc //文件内容改变之后的内容
No newline at end of file
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git add . //添加到暂存区后,diff就没有差异了因此不会显示内容
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git diff
查看工作区与最新提交差异:git diff HEAD
//先修改test.txt文件内容
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git diff //工作区与暂存区差异
diff --git a/test.txt b/test.txt
index 4e7ce06..9f67a57 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-aaaaabbccc
No newline at end of file
+aaaaabbcccd
No newline at end of file
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git add .
//添加到暂存区后无差异
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git diff
//工作区与最新提交的差异,HEAD指向最新提交的记录
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git diff HEAD
diff --git a/test.txt b/test.txt
index 4e7ce06..9f67a57 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-aaaaabbccc
No newline at end of file
+aaaaabbcccd
No newline at end of file
四、分支操作
git默认分支为master,以此基础上创建不同的分支,由不可的开发人员进行修改,然后再进行合并到master分支。分支有利于版本控制和团队协作。
查看分支列表:git branch
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git branch
* master //分支名前带有*号表示当前所在的分支
分支的创建与切换
- 分支创建git branch XX
- 分支切换git checkout XX
- 分支创建并切换git checkout -b XX
- 切换上一个分支git checkout –
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git branch A //1创建分支A
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git branch //分支列表,当前分支还是在主分支上
A
* master
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git checkout A //2切换当前分支为A
Switched to branch 'A'
M test.txt
Administrator@User-14 MINGW64 /g/gitdemo (A)
$ git branch //分支列表显示当前分支为A
* A
master
Administrator@User-14 MINGW64 /g/gitdemo (A)
$ git checkout master //切换到主分支上
Switched to branch 'master'
M test.txt
====================
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git checkout -b B //创建分支B并切换到B
Switched to a new branch 'B'
M test.txt
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git branch //分支列表显示当前为B分支
A
* B
master
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git checkout - //切换上一个分支master
Switched to branch 'master'
M test.txt
番外:
- git checkout master^ 使HEAD指向当前的上一记录。
- git checkout master~1 等同上行。
- git checkout master~n 使HEAD指向当前的上n条记录处。
分支的删除:git branch -D fname
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git branch -D B
Deleted branch B (was 670ec48).
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git branch
* master
git branch -f bugFix master 强制bugFix分支指向master
分支的合并:git merge fname
//先要切换到合并后的分支
Administrator@User-14 MINGW64 /g/gitdemo (A)
$ git branch master
fatal: A branch named 'master' already exists.
Administrator@User-14 MINGW64 /g/gitdemo (A)
$ git merge A
Already up to date.
图形查看日志:git log –graph
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git log --graph
* commit d76d79a34d1153c619297dd8fc3013c00cc8f9e8 (HEAD -> master, A)
| Author: Administrator <woodlum@vip.qq.com>
| Date: Wed Jan 6 15:20:27 2021 +0800
|
| a1
|
* commit 31a3585df1ebb5b744689ba1b7530c525c4c4271
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 15:19:25 2021 +0800
m1
五、更改提交的操作
回溯历史版本:git reset
//查看操作日志
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git reflog
d76d79a (HEAD -> master, A) HEAD@{0}: reset: moving to d76d79a
d76d79a (HEAD -> master, A) HEAD@{1}: reset: moving to d76d79a
31a3585 HEAD@{2}: reset: moving to 31a3585
d76d79a (HEAD -> master, A) HEAD@{3}: merge A: Fast-forward
31a3585 HEAD@{4}: checkout: moving from A to master
d76d79a (HEAD -> master, A) HEAD@{5}: commit: a1
31a3585 HEAD@{6}: checkout: moving from master to A
31a3585 HEAD@{7}: commit (initial): m1
//回溯到A分支创建之前
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git reset --hard 31a3585
HEAD is now at 31a3585 m1
//创建B分支
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git checkout -b B
Switched to a new branch 'B'
//修改文件内容并提交
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git add .
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git commit -m "b1"
[B 69eb4b2] b1
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git reflog
69eb4b2 (HEAD -> B) HEAD@{0}: commit: b1 //提交B分支的修改
31a3585 (master) HEAD@{1}: checkout: moving from master to B //创建B分支并切换
31a3585 (master) HEAD@{2}: reset: moving to 31a3585 //回溯到A分支之前
d76d79a (A) HEAD@{3}: reset: moving to d76d79a
d76d79a (A) HEAD@{4}: reset: moving to d76d79a
31a3585 (master) HEAD@{5}: reset: moving to 31a3585
d76d79a (A) HEAD@{6}: merge A: Fast-forward
31a3585 (master) HEAD@{7}: checkout: moving from A to master
d76d79a (A) HEAD@{8}: commit: a1
31a3585 (master) HEAD@{9}: checkout: moving from master to A
31a3585 (master) HEAD@{10}: commit (initial): m1
//推进到分支A合并到master位置
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git reset --hard d76d79a
HEAD is now at d76d79a a1
冲突消除
//前面已经合并了A分支,现在合并B分支,会发现更改部分冲突
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git merge B
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@User-14 MINGW64 /g/gitdemo (master|MERGING)
$
将test文件修改为想要提交的内容,然后add和commit即可
Administrator@User-14 MINGW64 /g/gitdemo (master|MERGING)
$ git add .
Administrator@User-14 MINGW64 /g/gitdemo (master|MERGING)
$ git commit -m "消除冲突"
[master 1759d05] 消除冲突
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git log
commit 1759d05a5dc0d1eaf402fae97f21aa3c014e31d5 (HEAD -> master)
Merge: d76d79a 69eb4b2
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 16:14:38 2021 +0800
消除冲突
commit 69eb4b22a38b70a3d054534fdfce8a24fba1a15d (B)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 15:33:42 2021 +0800
b1
commit d76d79a34d1153c619297dd8fc3013c00cc8f9e8 (A)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 15:20:27 2021 +0800
a1
commit 31a3585df1ebb5b744689ba1b7530c525c4c4271
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 15:19:25 2021 +0800
m1
修改commit信息:git commit –amend
将上面的commit信息“消除冲突”修改为“merge-B”
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git commit --amend
[master f4d38a7] merge-B
Date: Wed Jan 6 16:14:38 2021 +0800
这时会打开编程器
按i键或者Insert键进行编程模式
按ESC键进入命令模式,输入 :wq保存或者:q!不保存退出,查看reflog日志
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git reflog
f4d38a7 HEAD@{0}: commit (amend): merge-B //已经修改了
1759d05 HEAD@{1}: commit (merge): 消除冲突
d76d79a (A) HEAD@{3}: checkout: moving from A to master
压缩记录:git rebase -i
git rebase 分支名:将当前分支的记录合并到其他分支中,使记录成线性化。
当我们在合并分支之前发现有拼写错误,这时我们再次提交会出现新的记录,但是拼写错误这条记录我们不希望出现在历史记录中,我们需要将这条记录和前一记录合并。
创建分支C,将test内容改为masterCc,其中c小写是错误的,我们要求大写,不过之前没有发现,现在已经提交了一次,如果修改后再提交就会有两条记录。
//git commit -am add与commit的合写
Administrator@User-14 MINGW64 /g/gitdemo (C)
$ git commit -am "c1"
[C 5b8616b] c1
1 file changed, 1 insertion(+), 1 deletion(-)
====修正错误====
Administrator@User-14 MINGW64 /g/gitdemo (C)
$ git commit -am "修改拼写错误"
[C d6d54d6] 修改拼写错误
1 file changed, 1 insertion(+), 1 deletion(-)
//现在可以看到有两条记录,我们现在只需要保留c1这条记录,因此需要将记录压缩在一块
Administrator@User-14 MINGW64 /g/gitdemo (C)
$ git log
commit d6d54d60acededdc2994d9e245942afc1f464655 (HEAD -> C)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 16:42:27 2021 +0800
修改拼写错误
commit 5b8616b2f04ffe0473859862efc7f167b3e26835
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 16:34:17 2021 +0800
c1
//压缩最新的两条记录,会打开编程器,~2表示合并最新的两条记录
$ git rebase -i HEAD~2
删除第二行,:wq保存并退出
//现在看就没有那个修改拼写错误的记录了
Administrator@User-14 MINGW64 /g/gitdemo (C)
$ git log
commit a2f721c9f2c215bf245aa842128bc5094d2c0042 (HEAD -> C)
Author: Administrator <woodlum@vip.qq.com>
Date: Wed Jan 6 17:02:06 2021 +0800
c1
六、向远程仓库GitHub推送
前面我们在github上已经建立了一个同名的gitdemo远程仓库,本地仓库与远程仓库名称尽量保持一致。
添加远程仓库:git remote add 标识符 远程仓库地址
此命令相当于给远程仓库设置一个别名(标识符),以后的操作直接在这个别名上操作。
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git remote add origin git@github.com:woodlum2017/gitdemo.git
将本地仓库中的内容推送到远程仓库:git push -u 标识符 远程分支名
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git push -u origin master
Enumerating objects: 18, done.
Counting objects: 100% (18/18), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (18/18), 1.26 KiB | 215.00 KiB/s, done.
Total 18 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:woodlum2017/gitdemo.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
-u参数:在推送的同时,将origin仓库的master分支设置为本地仓库当前分支的upstream(上游)当前分支与远程分支建立了对应关系,同时如果远程仓库没有master分支同时会创建。
a.在本地master分支中运行git pull命令从远程仓库的master分支获取内容;
b.在本地master分支中支行git push直接推送。
如果在其他分支中操作会产生错误,需要手动指定pull和push的仓库和分支。
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git pull
Already up to date.
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git push
Everything up-to-date
==============手动推送=================
Administrator@User-14 MINGW64 /g/gitdemo (master)
$ git checkout B
Switched to branch 'B'
Administrator@User-14 MINGW64 /g/gitdemo (B)
$ git push origin B
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'B' on GitHub by visiting:
remote: https://github.com/woodlum2017/gitdemo/pull/new/B
remote:
To github.com:woodlum2017/gitde
七、从远程仓库GitHub中获取
获取远程仓库:git clone 远程仓库地址
执行git clone命令后我们会默认处于master分支下,同时系统会自动将origin设置成该远程仓库的标识符。
git branch -a //-a会显示本地和远程仓库的信息
新建一个文件夹gitdemo2
Administrator@User-14 MINGW64 /g/gitdemo2 (master)
$ git clone git@github.com:woodlum2017/gitdemo.git
Cloning into 'gitdemo'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 21 (delta 1), reused 18 (delta 1), pack-reused 0
Receiving objects: 100% (21/21), done.
Resolving deltas: 100% (1/1), done.
Administrator@User-14 MINGW64 /g/gitdemo2 (master)
$ cd gitdemo
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (master)
$ git branch -a //-a会显示本地和远程仓库的信息
* master
remotes/origin/B
remotes/origin/HEAD -> origin/master
remotes/origin/master
获取远程仓库中的其他分支
git checkout -b B origin/B
创建一个新的本地分支B并切换,并获取远程仓库中的B分支数据
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (master)
$ git checkout -b B origin/B
Switched to a new branch 'B'
Branch 'B' set up to track remote branch 'B' from 'origin'.
======修改数据后提交并推送=====
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (B)
$ git commit -am "D1"
[B 7c90c0d] D1
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (B)
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 250 bytes | 250.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:woodlum2017/gitdemo.git
ff94ca5..7c90c0d B -> B
获取最新的远程分支git pull
假设当前分支为B,而远程上最新的分支是master,我们需要分支B中的数据是最新的。
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (master)
$ git checkout B
Switched to branch 'B'
Administrator@User-14 MINGW64 /g/gitdemo2/gitdemo (B)
$ git pull origin master
From github.com:woodlum2017/gitdemo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
test.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
关于多人协作,多人在同一个分支中操作,commit后push给远程仓库分支,如果多个人对同一个文件修改在push时会提示冲突,因此我们需要更加频繁的pull/push操作。
尽量不要多人修改同一个文件,每次分支修改前先pull仓库中的数据,修改后再push推送。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。