1、官网安装 https://git-scm.com/downloads
2、创建本地版本库
Administrator@ThinkPadX1-Youga MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@ThinkPadX1-Youga MINGW64 ~
$ cd d:
Administrator@ThinkPadX1-Youga MINGW64 /d
$ pwd
/d
Administrator@ThinkPadX1-Youga MINGW64 /d
$ mkdir git_repository
Administrator@ThinkPadX1-Youga MINGW64 /d
$ cd git_repository
Administrator@ThinkPadX1-Youga MINGW64 /d/git_repository
$ pwd
/d/git_repository
3、设置登录用户与邮箱
Administrator@ThinkPadX1-Youga MINGW64 /d
$ git config --global user.name xxx -----登录用户
Administrator@ThinkPadX1-Youga MINGW64 /d
$ git config --global user.email xxx@139.com -----登录邮箱
Administrator@ThinkPadX1-Youga MINGW64 /d
$ cd TestSoftware/
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware
$ cd git_repository/
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository
$ pwd
/d/TestSoftware/git_repository
4、初始化版本库
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository
$ git init
Initialized empty Git repository in D:/TestSoftware/git_repository/.git/
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ pwd
/d/TestSoftware/git_repository
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ ls -ah
./ ../ .git/
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ pwd
/d/TestSoftware/git_repository
5、在版本库文件夹内创建readme.txt,并提交
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add readme.txt
fatal: pathspec 'readme.txt' did not match any files
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m "wrote a readme file2"
[master f94d02b] wrote a readme file2
1 file changed, 3 insertions(+)
6、更新多次文件内容
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m "append GPL"
[master 195f5c9] append GPL
1 file changed, 3 insertions(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m "append friend"
[master 77f50d5] append friend
1 file changed, 3 insertions(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log
commit 77f50d59e515a5b64e57f5fb66a7be9cf5bc1c93 (HEAD -> master)
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:36:37 2021 +0800
append friend
commit 195f5c95037a8259a5e1e2dea6f031d8418672be
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:35:07 2021 +0800
append GPL
commit f94d02b835bcc03025b1978fa62f8795d4d1e1a0
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:33:27 2021 +0800
wrote a readme file2
commit 9ae196987e826988ddc7ac65b3dc55cf3134644b
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:32:05 2021 +0800
wrote a readme file
7、回退版本和恢复版本
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log --pretty=oneline
77f50d59e515a5b64e57f5fb66a7be9cf5bc1c93 (HEAD -> master) append friend
195f5c95037a8259a5e1e2dea6f031d8418672be append GPL
f94d02b835bcc03025b1978fa62f8795d4d1e1a0 wrote a readme file2
9ae196987e826988ddc7ac65b3dc55cf3134644b wrote a readme file
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git reset --hard HEAD^
HEAD is now at 195f5c9 append GPL
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log
commit 195f5c95037a8259a5e1e2dea6f031d8418672be (HEAD -> master)
Author: xxx <xxx.com>
Date: Tue Jan 26 10:35:07 2021 +0800
append GPL
commit f94d02b835bcc03025b1978fa62f8795d4d1e1a0
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:33:27 2021 +0800
wrote a readme file2
commit 9ae196987e826988ddc7ac65b3dc55cf3134644b
Author: xxx <xxx@139.com>
Date: Tue Jan 26 10:32:05 2021 +0800
wrote a readme file
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git reset --hard 77f5
HEAD is now at 77f50d5 append friend
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cat readme.txt
111
2222
3333
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git is a friend
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$
8、删除文件
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add test1.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m 'add test'
[master dd7f462] add test
1 file changed, 1 insertion(+)
create mode 100644 test1.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ rm test.txt
rm: cannot remove 'test.txt': No such file or directory
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git status
On branch master
nothing to commit, working tree clean
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git rm test1.txt
rm 'test1.txt'
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m 'remove test1.txt'
[master 1cc8b9d] remove test1.txt
1 file changed, 1 deletion(-)
delete mode 100644 test1.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 176 Jan 26 11:33 readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$
9、git设置连接远程仓库
1、git创建repository,输入name,提交
2、获取key
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ ssh -T git@github.com
git@github.com: Permission denied (publickey).
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ ssh-keygen -t rsa -C "yourName"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
/c/Users/Administrator/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub # ---秘钥存放处
The key fingerprint is:
SHA256:c3a0gzpjLHqXZP4Rgct3urOfsnjJwAH4EO6OLik2Waw yourName
The key's randomart image is:
+---[RSA 3072]----+
| .o |
| .o . . |
| .o .. . . |
| . .... + . |
| . . .S.* = |
| = .=* = . |
| .= . .+*+o. |
|+E ..o++*o . |
|o.o .. ..o=*o |
+----[SHA256]-----+
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cat /c/Users/Administrator/.ssh/id_rsa.pub
3、设置key,输入key名字和生成秘钥
4、本地加载秘钥,
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ eval "$(ssh-agent -s)"
Agent pid 715
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ ssh-add
Identity added: /c/Users/Administrator/.ssh/id_rsa (CjwRiver)
5、重新推送
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git push -u origin master
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 4 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (25/25), 1.89 KiB | 483.00 KiB/s, done.
Total 25 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), done.
To github.com:xxx/learngit.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
10、git设置克隆远程仓库
$ git clone git@github.com:xxx/xxx.git -------远程仓库地址
Cloning into 'gitskills'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cd gitskills
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository/gitskills (main)
$ ls
README.md
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository/gitskills (main)
$ cat README.md
# gitskills
gitkills,learn down
11、分支管理
1、创建分支和合并
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master) ----master分支下有两个文件
$ ll
total 1
drwxr-xr-x 1 Administrator 197121 0 Jan 27 09:54 gitskills/
-rw-r--r-- 1 Administrator 197121 176 Jan 26 11:33 readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git checkout -b dev ------创建并切换到dev分支 -b是切换
Switched to a new branch 'dev'
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git branch ----查看当前分支
* dev
master
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git add readme.txt ---修改readme.txt ,新增【Creating a new branch is quick.】
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git commit -m 'branch test' --- dev分支上提交
[dev 3e866eb] branch test
1 file changed, 2 insertions(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git checkout master ---切换master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cat readme.txt ---查看readme.txt内容,没有变
111
2222
3333
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git is a friend
Git has a mutable index called stage.
444
555
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git merge dev ----合并分支。
Updating 1cc8b9d..3e866eb
Fast-forward ----这里指的是快速合并
readme.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cat readme.txt ----合并之后查看readme.txt内容
111
2222
3333
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git is a friend
Git has a mutable index called stage.
444
555
Creating a new branch is quick.
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git branch -d dev ----删除分支dev,
Deleted branch dev (was 3e866eb).
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git branch ----查看当前分支
* master
2、分支冲突解决
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git switch -c feature1 -----创建新的分支
Switched to a new branch 'feature1'
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (feature1)
$ git add readme.txt ----修改readme.txt文件,提交
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (feature1)
$ git commit -m 'and simple'
[feature1 b5b24d9] and simple
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (feature1)
$ git switch master ----切换master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git add readme.txt ---同样修改readme.txt文件后,再次提交
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git commit -m '& simple'
[master cde2079] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git merge feature1 ----合并看看,Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master|MERGING)
$ git status -----git status也可以告诉我们冲突的文件:
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
gitskills/
no changes added to commit (use "git add" and/or "git commit -a")
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master|MERGING)
$ cat readme.txt ---查看readme.txt的内容:
111
2222
3333
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git is a friend
Git has a mutable index called stage.
444
555
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master|MERGING)
$ git add readme.txt ---再次修改保存后提交
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master|MERGING)
$ git commit -m 'conflict fixed'
[master 3d5f936] conflict fixed
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log
commit 3d5f936a2fe2e440521d29346023fb078bee99b2 (HEAD -> master)
Merge: cde2079 b5b24d9
Author: cjw <18813048840@139.com>
Date: Wed Jan 27 11:34:44 2021 +0800
conflict fixed
commit cde20792faa9effeebcdd3292c17c39732f55429
Author: cjw <18813048840@139.com>
Date: Wed Jan 27 11:32:32 2021 +0800
& simple
commit b5b24d95cd22cc8d32dea7431fa90e0f72a52bbc (feature1)
Author: cjw <18813048840@139.com>
Date: Wed Jan 27 11:30:45 2021 +0800
and simple
commit 3e866ebbef42f9353276dc24be01ef1aa629cc42
Author: cjw <18813048840@139.com>
Date: Wed Jan 27 10:10:04 2021 +0800
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git branch -d feature1 ----删除分支
Deleted branch feature1 (was b5b24d9).
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log --graph --pretty=oneline --abbrev-commit ---带参数的git log也可以看到分支的合并
* 3d5f936 (HEAD -> master) conflict fixed
|
| * b5b24d9 and simple
* | cde2079 & simple
|/
* 3e866eb branch test
* 1cc8b9d (origin/master) remove test1.txt
* dd7f462 add test
* f020c25 status
* 1a58908 update 2
* ed66b14 index called
* 77f50d5 append friend
* 195f5c9 append GPL
* f94d02b wrote a readme file2
* 9ae1969 wrote a readme file
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ q
bash: q: command not found
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ cat readme.txt
111
2222
3333
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git is a friend
Git has a mutable index called stage.
444
555
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$
3、分支管理策略
合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。
如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。
下面我们实战一下--no-ff方式的git merge:
首先,仍然创建并切换dev分支:
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git switch -c dev
Switched to a new branch 'dev'
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git add readme.txt
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git commit -m 'add test1'
[dev 3c0c8ed] add test1
1 file changed, 2 insertions(+)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (dev)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
readme.txt | 2 ++
1 file changed, 2 insertions(+)
Administrator@ThinkPadX1-Youga MINGW64 /d/TestSoftware/git_repository (master)
$ git log --graph --pretty=oneline --abbrev-commit
* 6809576 (HEAD -> master) merge with no-ff
|
| * 3c0c8ed (dev) add test1
|/
* 3d5f936 conflict fixed
|
| * b5b24d9 and simple
* | cde2079 & simple
|/
* 3e866eb branch test
* 1cc8b9d (origin/master) remove test1.txt
* dd7f462 add test
* f020c25 status
* 1a58908 update 2
* ed66b14 index called
* 77f50d5 append friend
* 195f5c9 append GPL
* f94d02b wrote a readme file2
* 9ae1969 wrote a readme file
总结:合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。
4、多人协作
-
首先,可以试图用git push origin
推送自己的修改; -
如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
-
如果合并有冲突,则解决冲突,并在本地提交;
-
没有冲突或者解决掉冲突后,再用git push origin
推送就能成功! -
如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to
origin/ 。 -
这就是多人协作的工作模式,一旦熟悉了,就非常简单。
小结:
-
查看远程库信息,使用git remote -v;
-
本地新建的分支如果不推送到远程,对其他人就是不可见的;
-
从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
-
在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
-
建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;
-
从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。
以上资料来自廖雪峰git资料