gittest
gittest
初始化版本,配置签名
wm@xiaowan MINGW64 /d/git/gittest
$ git init
Initialized empty Git repository in D:/git/gittest/.git/
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git config user.name "wanmin"
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git config user.email "1914977093@qq.com"
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git config --global user.name "wanmin1"
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git config --global user.email "18090099827@163.com"
D:\git\gittest\.git\config
C:\Users\wm\.gitconfig
提交文件
git status
git add src/Test.java
git commit -m "java"
查看文件提交记录
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git log
commit 506fca290713401118f1a1bd8f897c0795bf94bf (HEAD -> master)
Author: wanmin <1914977093@qq.com>
Date: Fri Feb 17 12:25:51 2023 +0800
02 a
commit ba1753123691ba57cdb9828286ccf0dca61670ec
Author: wanmin <1914977093@qq.com>
Date: Fri Feb 17 12:17:07 2023 +0800
java
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git log --pretty=oneline
506fca290713401118f1a1bd8f897c0795bf94bf (HEAD -> master) 02 a
ba1753123691ba57cdb9828286ccf0dca61670ec java
回退历史
git reset --hard HEAD^ #回退一步
git reset --hard HEAD~N #回退n次操作
版本穿越
git reflog #查看版本号
git reset --hard 506fca2 #穿越到该版本
还原文件
#如果文件修改,还未进行git add的情况下,可以通过git checkout还原回到修改之前的状态. 如果已经提交了,只能使用git reset命令回退.
git checkout
删除文件
#如果进行了一些文件的误提交,先将本地文件删除,再执行git add 提交.
#实际上,只要提交后的文件,在git中都有记录,每个用户可以通过git reflog查看到对应的版本号,还可以再回退到对应的版本.
wm@xiaowan MINGW64 /d/git/gittest (master)
$ touch src/girl.jpg
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git add src
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git commit -m "new file"
[master 7c6bbeb] new file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 src/girl.jpg
wm@xiaowan MINGW64 /d/git/gittest (master)
$ rm -rf src/girl.jpg
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: src/girl.jpg
no changes added to commit (use "git add" and/or "git commit -a")
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git add src/girl.jpg
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git commit -m "fix bug"
[master e9221b4] fix bug
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 src/girl.jpg
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git status
On branch master
nothing to commit, working tree clean
wm@xiaowan MINGW64 /d/git/gittest (master)
三个区
工作区:项目对应的本地磁盘目录
暂存区:一般存放在.git目录下的index文件.
本地库:项目对应的本地磁盘目录中的.git目录
Git 分支
分支原理
分支实战
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git branch -v #查看分支
* master e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git branch dev #创建分支
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git branch -v #查看分支
dev e9221b4 fix bug
* master e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git checkout dev #切换分支
Switched to branch 'dev'
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git branch -v #查看分支
* dev e9221b4 fix bug
master e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git checkout -b test #创建分支并切换分支
Switched to a new branch 'test'
wm@xiaowan MINGW64 /d/git/gittest (test)
$ git branch -v #查看分支
dev e9221b4 fix bug
master e9221b4 fix bug
* test e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (test)
$ git checkout dev #切换分支
Switched to branch 'dev'
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git branch -v #查看分支
* dev e9221b4 fix bug
master e9221b4 fix bug
test e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ touch src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git add src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git commit -m "add hello1.java"
[dev 026bb7f] add hello1.java
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git branch -v #查看分支
* dev 026bb7f add hello1.java
master e9221b4 fix bug
test e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (dev)
$ git checkout master #切换分支
Switched to branch 'master'
wm@xiaowan MINGW64 /d/git/gittest (master)
$ ll src
total 1
-rw-r--r-- 1 wm 197609 26 Feb 17 12:43 Test.java
wm@xiaowan MINGW64 /d/git/gittest (master)
$ #分支之间互相不干涉
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git branch -v #查看分支
dev 026bb7f add hello1.java
* master e9221b4 fix bug
test e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git merge dev #合并dev分支
Updating e9221b4..026bb7f
Fast-forward
src/hello1.java | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (master)
$ ll /src/
ls: cannot access '/src/': No such file or directory
wm@xiaowan MINGW64 /d/git/gittest (master)
$ ll /src
ls: cannot access '/src': No such file or directory
wm@xiaowan MINGW64 /d/git/gittest (master)
$ ll
total 0
drwxr-xr-x 1 wm 197609 0 Feb 17 13:22 src/
wm@xiaowan MINGW64 /d/git/gittest (master)
$ cd src
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ ll
total 1
-rw-r--r-- 1 wm 197609 26 Feb 17 12:43 Test.java
-rw-r--r-- 1 wm 197609 0 Feb 17 13:22 hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ #dev已经合并到master
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git branch -v #查看分支
dev 026bb7f add hello1.java
* master 026bb7f add hello1.java
test e9221b4 fix bug
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git branch -D test #删除分支test
Deleted branch test (was e9221b4).
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git branch -v #查看分支
dev 026bb7f add hello1.java
* master 026bb7f add hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (master)
冲突
1) *如果两个分支同时修改一个文件,再进行合并分支时,就会出现冲突M**ERGING**.*
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git branch -v #查看分支
dev 026bb7f add hello1.java
* master 026bb7f add hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git checkout dev #切换分支
Switched to branch 'dev'
wm@xiaowan MINGW64 /d/git/gittest/src (dev)
$ ll
total 1
-rw-r--r-- 1 wm 197609 26 Feb 17 12:43 Test.java
-rw-r--r-- 1 wm 197609 0 Feb 17 13:22 hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (dev)
$ vim hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (dev)
$ git add hello1.java
warning: in the working copy of 'src/hello1.java', LF will be replaced by CRLF the next time Git touches it
wm@xiaowan MINGW64 /d/git/gittest/src (dev)
$ git commit -m "add hello1"
[dev 26f4c4d] add hello1
1 file changed, 1 insertion(+)
wm@xiaowan MINGW64 /d/git/gittest/src (dev)
$ git checkout master #切换分支
Switched to branch 'master'
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ vim hello1.java
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git add hello1.java
warning: in the working copy of 'src/hello1.java', LF will be replaced by CRLF the next time Git touches it
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git commit -m "add hellomaster"
[master f51fece] add hellomaster
1 file changed, 1 insertion(+)
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git branch -v
dev 26f4c4d add hello1
* master f51fece add hellomaster
wm@xiaowan MINGW64 /d/git/gittest/src (master)
$ git merge dev
Auto-merging src/hello1.java
CONFLICT (content): Merge conflict in src/hello1.java
Automatic merge failed; fix conflicts and then commit the result.
wm@xiaowan MINGW64 /d/git/gittest/src (master|MERGING)
*2)定位冲突*
wm@xiaowan MINGW64 /d/git/gittest (master|MERGING)
$ git diff
diff --cc src/hello1.java
index 42ed88a,15b8f2a..0000000
--- a/src/hello1.java
+++ b/src/hello1.java
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
+hellomaster
++=======
+ hello1
++>>>>>>> dev
*3)解决冲突*
#进入到冲突的文件,进行内容的修改.然后重新git add 与 git commit
wm@xiaowan MINGW64 /d/git/gittest (master|MERGING)
$ vim src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (master|MERGING)
$ git add src/hello1.java
wm@xiaowan MINGW64 /d/git/gittest (master|MERGING)
$ git commit -m "fix merging"
[master 1ed4fd2] fix merging
wm@xiaowan MINGW64 /d/git/gittest (master)
$ git branch -v
dev 26f4c4d add hello1
* master 1ed4fd2 fix merging
github与gitee
创建本地项目
wm@xiaowan MINGW64 /d/git
$ pwd
/d/git
wm@xiaowan MINGW64 /d/git
$ mkdir -p xiaowan/jianfa
wm@xiaowan MINGW64 /d/git
$ cd xiaowan/jianfa/
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa
$ git init
Initialized empty Git repository in D:/git/xiaowan/jianfa/.git/
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git config user.name "xiaowan"
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git config user.email "xiaowan@sina.com"
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ mkdir src
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ cd src
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa/src (master)
$ touch jianfa.java
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa/src (master)
$ vim jianfa.java
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa/src (master)
$ cd ..
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/
nothing added to commit but untracked files present (use "git add" to track)
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git add src/
warning: in the working copy of 'src/jianfa.java', LF will be replaced by CRLF the next time Git touches it
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: src/jianfa.java
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git commit -m "jianfa init"
[master (root-commit) 9b8166a] jianfa init
1 file changed, 3 insertions(+)
create mode 100644 src/jianfa.java
在Gitee中创建Project
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git remote add origin https://gitee.com/wan-min1/jianfa.git #增加远端地址
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git push origin master #推送到远端库
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (4/4), 264 bytes | 264.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/wan-min1/jianfa.git
* [new branch] master -> master
*在Gitee中查看*
从GitHub上克隆一个项目[令狐冲]
wm@xiaowan MINGW64 /d/git
$ pwd
/d/git
wm@xiaowan MINGW64 /d/git
$ mkdir linghuchong
wm@xiaowan MINGW64 /d/git
$ cd linghuchong/
wm@xiaowan MINGW64 /d/git/linghuchong
$ git clone https://gitee.com/wan-min1/jianfa.git
Cloning into 'jianfa'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
wm@xiaowan MINGW64 /d/git/linghuchong
$ git config user.name "linghuchong"
fatal: not in a git directory
wm@xiaowan MINGW64 /d/git/linghuchong
$ git config user.name "xw"
fatal: not in a git directory
wm@xiaowan MINGW64 /d/git/linghuchong
$ ll
total 0
drwxr-xr-x 1 wm 197609 0 Feb 17 14:06 jianfa/
wm@xiaowan MINGW64 /d/git/linghuchong
$ git init
Initialized empty Git repository in D:/git/linghuchong/.git/
wm@xiaowan MINGW64 /d/git/linghuchong (master)
$ git config user.name "xw"
wm@xiaowan MINGW64 /d/git/linghuchong (master)
$ git config user.eamil "xw@163.com"
wm@xiaowan MINGW64 /d/git/linghuchong (master)
$ cd src/
bash: cd: src/: No such file or directory
wm@xiaowan MINGW64 /d/git/linghuchong (master)
$ ll
total 0
drwxr-xr-x 1 wm 197609 0 Feb 17 14:06 jianfa/
wm@xiaowan MINGW64 /d/git/linghuchong (master)
$ cd jianfa/
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa (master)
$ ll
total 0
drwxr-xr-x 1 wm 197609 0 Feb 17 14:06 src/
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa (master)
$ cd src/
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa/src (master)
$ ll
total 1
-rw-r--r-- 1 wm 197609 20 Feb 17 14:06 jianfa.java
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa/src (master)
$ vim jianfa.java
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa/src (master)
$ git add jianfa.java
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa/src (master)
$ git commit -m "add aaaa"
[master 9d714e0] add aaaa
1 file changed, 1 insertion(+), 1 deletion(-)
wm@xiaowan MINGW64 /d/git/linghuchong/jianfa/src (master)
$ git push origin master #将新的东西提交到gitee
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Writing objects: 100% (4/4), 296 bytes | 296.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/wan-min1/jianfa.git
9b8166a..9d714e0 master -> master
从Gitee中更新项目到本地库
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ git pull origin master
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), 1.24 KiB | 28.00 KiB/s, done.
From https://gitee.com/wan-min1/jianfa
* branch master -> FETCH_HEAD
9b8166a..4f09938 master -> origin/master
Updating 9b8166a..4f09938
Fast-forward
src/jianfa.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
wm@xiaowan MINGW64 /d/git/xiaowan/jianfa (master)
$ cat src/jianfa.java
public class{
aaaaa`
bbb
}