配置用户名邮箱
签名设置一般有2种设置,第一种是在某个文件夹下生效,另一类是在此电脑生效。
1.在某个具体文件夹下可以用
git config user.name August
git config user.email youxiaong@qq.com
可以在cat .git/config
中看到用户名和邮箱
2.此电脑可用
git config --global user.name August
git config --global user.email youxiaong@qq.com
可以在首先执行cd ~
然后执行cat .gitconfig
可以看到用户名和密码
git init
初始化本地仓库,初始化本地服务后后出现一个.get的文件夹。代表本地仓库初始化成功。
这种代表暂存区是没有要提交的文件,工作区也没有什么文件。
状态查看命令git status
主要查看工作区,暂存区的状态
$ git status
On branch master
nothing to commit, working tree clean
这种代表暂存区是有一个demo.txt文件。
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
demo.txt
nothing added to commit but untracked files present (use "git add" to track)
nothing added to commit but untracked files present (use "git add" to track)
这句话的意思是使用git add命令可以把一个未追踪的文件追追踪。
当把文件conmmit到本地仓库后改动文件,使用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: demo.txt
no changes added to commit (use "git add" and/or "git commit -a")
no changes added to commit (use "git add" and/or "git commit -a")
这句话的意思是先add在commit或者直接commit。
这种情况是吧改变的文件通过git add指令移至暂存区
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: demo.txt
git add demo.txt
添加改变的文件到暂存区
添加操作 git add
将工作区的“新建/修改”添加到暂存区
git add demo.txt
将文件添加至暂存区
$ git add demo.tx
warning: LF will be replaced by CRLF in demo.txt.
The file will have its original line endings in your working directory.
git rm --cached demo.txt
暂存区中的某个文件移除
$ git rm --cached demo.txt
fatal: pathspec 'demo.txt' did not match any files
提交操作 git commit -m "提交信息说明"
将暂存区的内容提交到本地库
git commit -m "提交信息"
将暂存区提交到本地仓库
$ git commit -m "this is first commit"
[master (root-commit) 2a394c0] this is first commit
1 file changed, 2 insertions(+)
create mode 100644 demo.txt
提交成功后会出现如下信息2a394c0
代表本次提交版本号,后面显示的this is first commit
也就是我们的提交说明,1 file changed, 2 insertions(+)
代表一个文件被修改了,改了2行create mode 100644 demo.txt
代表提交的是一个新建的文件。
如图所示:注:图片来源于网络
查看历史记录
git log
$ git log
commit eea7acfe618c8a23e01fe2d0edcaccbae1d76065
Author: August <1337713882@qq.com>
Date: Tue Mar 17 15:31:40 2020 +0800
this is add context
commit c249175c2f26dae3b5adcac12bfb5effb24ee7db
Author: August <1337713882@qq.com>
Date: Tue Mar 17 15:06:36 2020 +0800
this is second
commit 2a394c0cbc66ad62bf0435f6ee74daf0e83247d2
Author: August <1337713882@qq.com>
Date: Tue Mar 17 14:38:32 2020 +0800
this is first commit
commit c249175c2f26dae3b5adcac12bfb5effb24ee7db
commit 后面的字符串代表的是本次提交的内容的有个索引。Author代表后面是之前配置的作者<>中的是签名时配置的邮箱。Date Tue Mar 17 15:06:36 2020 +0800
代表的是提交日期this is second
代表的是提交说明。
git log --pretty=oneline
$ git log --pretty=oneline
eea7acfe618c8a23e01fe2d0edcaccbae1d76065 this is add context
c249175c2f26dae3b5adcac12bfb5effb24ee7db this is second
2a394c0cbc66ad62bf0435f6ee74daf0e83247d2 this is first commit
git log --oneline
$ git log --oneline
eea7acf this is add context
c249175 this is second
2a394c0 this is first commit
git reflog
$ git reflog
eea7acf HEAD@{0}: commit: this is add context
c249175 HEAD@{1}: commit: this is second
2a394c0 HEAD@{2}: commit (initial): this is first commit
版本回退方式1 git reset --hard 索引
先执行git reflog获取要回退的版本信息然后执行git reset
$ git reset --hard c249175
HEAD is now at c249175 this is second
然后我们使用git reflog可以看到当前的版本为c249175,就是我们刚刚回退的版本。
$ git reflog
c249175 HEAD@{0}: reset: moving to c249175
eea7acf HEAD@{1}: commit: this is add context
c249175 HEAD@{2}: commit: this is second
2a394c0 HEAD@{3}: commit (initial): this is first commit
版本前置也是先通过git reflog获取版本号然后使用git reset --hard索引,可以到制定的版本
版本回退方式2 git reset --hard HEAD^
git reset --hard HEAD^往后退一步
$ git reset --hard HEAD^
HEAD is now at c249175 this is second
git reset --hard HEAD^^^往后退三步
多个^表示后退多步
版本回退方式3 git reset --hard HEAD~数字
git reset --hard HEAD~2往后退两步
~后面的数字代表后退的步数
恢复删除的文件
$ rm hello.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: hello.txt
$ git add hello.txt
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: hello.txt
$ git commit -m"second commit"
[master 6046d16] second commit
1 file changed, 7 deletions(-)
delete mode 100644 hello.txt
$ git reset --hard 4925922
HEAD is now at 4925922 new text hello
$ ls
demo.txt hello.txt
这时就可以看到被删除的文件。
文件在暂存区没有在本地库,被删除使用git reset --hard HEAD
指令进行恢复。
git reset --hard HEAD 暂存区和工作区都会被刷新根据Head刷新。
文件比较 git diff 文件名
git diff是和暂存区的文件进行比较
$ git diff two.txt
diff --git a/two.txt b/two.txt
index f20996b..0191346 100644
--- a/two.txt
+++ b/two.txt
@@ -1,8 +1,8 @@
a
b
-c
-dddddddddddddd
-eee
+cc
+ddddddddddd
+ee
f
g
h
warning: LF will be replaced by CRLF in two.txt.
The file will have its original line endings in your working directory.
$ git diff HEAD 表示和本地库的文件进行比较
$ git diff HEAD two.txt
diff --git a/two.txt b/two.txt
index f20996b..6f07b37 100644
--- a/two.txt
+++ b/two.txt
@@ -1,7 +1,7 @@
a
b
-c
-dddddddddddddd
+ccccc
+dddddddddd
eee
f
g
warning: LF will be replaced by CRLF in two.txt.
The file will have its original line endings in your working directory.
分枝管理
创建新分枝git branch 分枝名
$ git branch sit
$ git branch -v 查看当前所有分枝
$ git branch -v
* master cd18f18 two.txt
sit cd18f18 two.txt
$ git checkout 分枝名称 切换分枝
$ git checkout sit
M two.txt
Switched to branch 'sit'
将sit分枝代码合并到master分枝上
1.切换到被合并的分枝的分枝上面。
$ git checkout master
Switched to branch 'master'
2.然后执行git merge操作
$ git merge sit
Updating cd18f18..0b4cc69
Fast-forward
one.txt | 11 +++++++++++
two.txt | 4 ++--
2 files changed, 13 insertions(+), 2 deletions(-)
create mode 100644 one.txt
解决冲突
git merge的时候冲突了
$ git merge sit
Auto-merging two.txt
CONFLICT (content): Merge conflict in two.txt
Automatic merge failed; fix conflicts and then commit the result.
然后我们可以查看冲突的内容,改好后使用git add指令在使用git commit指令进行提交就行了。
给远程地址起别名git remote add origin
git remote add origin https://~~~~~~~~
$ git remote -v 查看远程地址
$ git push将本地仓库提交到远程仓库
$ git push origin master
Counting objects: 28, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (28/28), 2.08 KiB | 0 bytes/s, done.
Total 28 (delta 7), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To https://gitee.com/ypjmy/gitdemo.git
* [new branch] master -> master
$ git clone 命令可以从远程下载代码到本地
$ git clone https://~~~~~
Cloning into 'prd'...
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 28 (delta 7), reused 0 (delta 0)
Unpacking objects: 100% (28/28), done.