一、windows安装git
1、从Git官网直接下载安装程序,然后按默认选项安装即可,
2、因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。因此安装完成后,还需要最后一步设置,在命令行输入
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
3、创建git版本库,版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
a、首先在电脑D盘创建文件夹gitlearn,然后右键打开git bash here
b、创建对应目录
Administrator@PC201704271057 MINGW64 /d/gitlearn $ pwd /d/gitlearn Administrator@PC201704271057 MINGW64 /d/gitlearn $ mkdir learngit Administrator@PC201704271057 MINGW64 /d/gitlearn $ cd learngit/ Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit $ pwd /d/gitlearn/learngit
c、第二步,通过git init
命令把这个目录变成Git可以管理的仓库
Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit $ git init Initialized empty Git repository in D:/gitlearn/learngit/.git/ Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ ls Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ ls -la total 4 drwxr-xr-x 1 Administrator 197121 0 一月 9 14:25 ./ drwxr-xr-x 1 Administrator 197121 0 一月 9 14:20 ../ drwxr-xr-x 1 Administrator 197121 0 一月 9 14:25 .git/
d、版本控制系统是没法跟踪Word文件的改动的,前面我们举的例子只是为了演示,如果要真正使用版本控制系统,就要以纯文本方式编写文件。
4、开始测试
a、创建一个名字叫readme.txt文件,内容如下
Git is a version control system. Git is free software.
b、第一步,用命令git add
告诉Git,把文件添加到仓库
Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master)
$ git add readme.txt
c、第二步,用命令git commit
告诉Git,把文件提交到仓库
$ git commit -m "wrote a readme file" [master (root-commit) 7cf9237] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt
d、commit
可以一次提交很多文件,所以你可以多次add
不同的文件
$ git add file1.txt $ git add file2.txt file3.txt $ git commit -m "add 3 files."
二、git时光穿梭
1、git status,将一中readme.txt改成如下内容,然后运行git status查看结果
Git is a distributed version control system. Git is free software.
$ 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 status
命令可以让我们时刻掌握仓库当前的状态,上面的命令输出告诉我们,readme.txt
被修改过了,但还没有准备提交的修改。
2、git diff,
顾名思义就是查看difference,显示的格式正是Unix通用的diff格式,可以从下面的命令输出看到,我们在第一行添加了一个distributed
单词
$ git diff readme.txt warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory. 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.
3、将修改后的文件提交到仓库查看状态
Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git add readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git commit -m "add distributed" [master 49fd6ee] add distributed 1 file changed, 1 insertion(+), 1 deletion(-) Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git status On branch master nothing to commit, working tree clean
4、版本回退
a、git log查看git 操作,如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline
参数
Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git log commit 49fd6ee08bb40941995ef262d18bdb41cd096352 (HEAD -> master) Author: lipingchang <1209989516@qq.com> Date: Wed Jan 9 16:37:36 2019 +0800 add distributed commit 7cf92375c84913da3b8050080a2850730cc244a6 Author: lipingchang <1209989516@qq.com> Date: Wed Jan 9 16:16:59 2019 +0800 wrote a readme file Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git log --pretty=oneline 49fd6ee08bb40941995ef262d18bdb41cd096352 (HEAD -> master) add distributed 7cf92375c84913da3b8050080a2850730cc244a6 wrote a readme file
b、版本回退,在Git中,用HEAD
表示当前版本,也就是最新的提交49fd6...
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100
。
c、git reset,我们要把当前版本add distributed回退到上一个版本wrote a readme file,就可以使用git reset
$ git reset --hard HEAD^ HEAD is now at 7cf9237 wrote a readme file Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git log commit 7cf92375c84913da3b8050080a2850730cc244a6 (HEAD -> master) Author: lipingchang <1209989516@qq.com> Date: Wed Jan 9 16:16:59 2019 +0800 wrote a readme file Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a version control system. Git is free software.
会发现看不到最新的add distributed这个版本了,此时可以通过回退commit id 来实现版本切换,版本号没必要写全,前几位就可以了,Git会自动去找
$ git reset --hard 49fd6e HEAD is now at 49fd6ee add distributed Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git log commit 49fd6ee08bb40941995ef262d18bdb41cd096352 (HEAD -> master) Author: lipingchang <1209989516@qq.com> Date: Wed Jan 9 16:37:36 2019 +0800 add distributed commit 7cf92375c84913da3b8050080a2850730cc244a6 Author: lipingchang <1209989516@qq.com> Date: Wed Jan 9 16:16:59 2019 +0800 wrote a readme file Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a distributed version control system. Git is free software.
d、Git提供了一个命令git reflog
用来记录你的每一次命令,如果忘记版本commit id 就可以使用
$ git reflog 49fd6ee (HEAD -> master) HEAD@{0}: reset: moving to 49fd6e 7cf9237 HEAD@{1}: reset: moving to HEAD^ 49fd6ee (HEAD -> master) HEAD@{2}: commit: add distributed 7cf9237 HEAD@{3}: commit (initial): wrote a readme file
5、git工作区和暂存区,前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的,第一步是用git add
把文件添加进去,实际上就是把文件修改添加到暂存区,第二步是用git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。因此Git是这样跟踪修改的,每次修改,如果不用git add
到暂存区,那就不会加入到commit
中。
6、撤销修改
a、若已经在文件中添加了相应的内容还没有git add ,使用git checkout -- 文件名即可,git checkout -- file
命令中的--
很重要,没有--
,就变成了“切换到另一个分支”的命令
$ echo "bbb" >> readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a distributed version control system. Git is free software. aaa bbb Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git checkout -- readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a distributed version control system. Git is free software. aaa
b、若已经修改了相应内容并且git add了需要先执行 git reset HEAD 文件名,然后再执行git checkout -- 文件名即可
$ echo "bbb" >> readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git add readme.txt warning: LF will be replaced by CRLF in readme.txt. The file will have its original line endings in your working directory. Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a distributed version control system. Git is free software. aaa bbb Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git reset HEAD readme.txt Unstaged changes after reset: M readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (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: readme.txt no changes added to commit (use "git add" and/or "git commit -a") Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ git checkout -- readme.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit (master) $ cat readme.txt Git is a distributed version control system. Git is free software. aaa
7、删除文件
a、创建了一个文件test.txt 然后git add 了,此时要删除这个文件有如下方式。
1)、首先删除文件,rm test.txt ,然后在版本库中删除,git rm test.txt 并且 git commit -m "remove test.txt"
b、创建了一个文件test.txt然后git add 了,此时误删除了这个文件,要恢复的话只需要 git checkout -- 文件名 就行了。
三、远程仓库
1、要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git;关联后,使用命令git push -u origin master第一次推送master分支的所有内容;此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;
2、拉取远程仓库 git clone git@github.com:lwx516294/gitskills.git
四、分支管理
1、我们创建dev
分支,然后切换到dev
分支,git checkout
命令加上-b
参数表示创建并切换,git checkout -b dev
相当于以下两条命令
$ git branch dev $ git checkout dev Switched to branch 'dev'
2、git branch
命令会列出所有分支,当前分支前面会标一个*
号
$ git checkout -b dev Switched to a new branch 'dev' Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev) $ git branch * dev master Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev)
3、在dev 分支创建 aaa.txt文件并提交到dev分支然后合并到master分支上
$ echo "cccddd" >> dev.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev) $ ls dev.txt README.md Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev) $ git add dev.txt warning: LF will be replaced by CRLF in dev.txt. The file will have its original line endings in your working directory. Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev) $ git commit -m "dev.txt" [dev fa2c27e] dev.txt 1 file changed, 1 insertion(+) create mode 100644 dev.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (dev) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ ls README.md Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git merge dev Updating 1b4efe5..fa2c27e Fast-forward dev.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 dev.txt Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ ls dev.txt README.md
4、删除分支 git branch -d dev
$ git branch -d dev Deleted branch dev (was fa2c27e). Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git branch * master
4、解决冲突,若在dev 分支上修改了文件dev.txt 然后又切换到master 分支修改了dev.txt 文件然后要将dev 分支合并到master分支就会产生冲突。Git用<<<<<<<
,=======
,>>>>>>>
标记出不同分支的内容,我们修改如下后保存:
$ cat dev.txt cccddd <<<<<<< HEAD ddd ======= aaa >>>>>>> dev Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master|MERGING)
此时我们可以手动删除master上修改的内容然后提交,修改后变为
cccddd
aaa
用带参数的git log
也可以看到分支的合并情况:
$ git log --graph --pretty=oneline --abbrev-commit * ee1ac8c (HEAD -> master) dev merge | | * 9996444 (dev) aaa * | e3e8870 ddd |/ * fa2c27e dev.txt * 1b4efe5 (origin/master, origin/HEAD) Initial commit
5、分支管理策略,通常,合并分支时,如果可能,Git会用Fast forward
模式,但这种模式下,删除分支后,会丢掉分支信息,如果要强制禁用Fast forward
模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。
$ git merge --no-ff -m "merge with no-ff" dev Merge made by the 'recursive' strategy. dev.txt | 1 + 1 file changed, 1 insertion(+) Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git log commit ade6f2acb64e85d1a2db28b18415519365aaa524 (HEAD -> master) Merge: ee1ac8c ef25ce3 Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 11:30:07 2019 +0800 merge with no-ff commit ef25ce3bd6a29c1d15faf4b8894e10e8a81d30fc (dev) Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 11:29:16 2019 +0800 aaa commit ee1ac8c2d1c0e31d3bc0844b2933fda4530bde85 Merge: e3e8870 9996444 Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 11:10:20 2019 +0800 dev merge commit e3e8870d7d26636dfe2019964f653aa559ffba8a Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 10:55:27 2019 +0800 ddd commit 9996444daceee4e4d02ea0b5728caccd69e49844 Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 10:54:39 2019 +0800 aaa commit fa2c27ed7038c808e9a61e0d4bf2c1ac93a2298e Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 10:46:52 2019 +0800 dev.txt commit 1b4efe5d1b544343c0006e7b28331f2a8455b741 (origin/master, origin/HEAD) Author: Presley <35398877+lwx516294@users.noreply.github.com> Date: Thu Jan 10 10:30:51 2019 +0800 Initial commit
合并分支时,加上--no-ff
参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward
合并就看不出来曾经做过合并。
6、删除还没有合并的分支可以通过git branch -D 分支名
强行删除。
7、多人协作
a、查看远程库信息,使用git remote -v
;
$ git remote -v origin git@github.com:lwx516294/gitskills.git (fetch) origin git@github.com:lwx516294/gitskills.git (push)
上面显示了可以抓取和推送的origin
的地址。如果没有推送权限,就看不到push的地址。
b、本地新建的分支如果不推送到远程,对其他人就是不可见的;
c、从本地推送分支,使用git push origin branch-name
,如果推送失败,先用git pull
抓取远程的新提交;
d、在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name
,本地和远程分支的名称最好一致;
e、建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name
;
f、从远程抓取分支,使用git pull
,如果有冲突,要先处理冲突。
五、标签管理
1、给当前分支打tag,git tag <name>,查看所有的tag用git tag命令
$ git tag v1.0 $ git tag v1.0
2、给提交的commit id打tag
$ git log --pretty=oneline --abbrev-commit 392899f (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) pull cb3a4ee test 6ec19f6 xiugai ade6f2a merge with no-ff ef25ce3 (origin/dev, dev) aaa ee1ac8c dev merge e3e8870 ddd 9996444 aaa fa2c27e dev.txt 1b4efe5 Initial commit Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git tag v0.9 fa2c27e
3、注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show <tagname>
查看标签信息
$ git show v0.9 commit fa2c27ed7038c808e9a61e0d4bf2c1ac93a2298e (tag: v0.9) Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 10:46:52 2019 +0800 dev.txt diff --git a/dev.txt b/dev.txt new file mode 100644 index 0000000..af530c2 --- /dev/null +++ b/dev.txt @@ -0,0 +1 @@ +cccddd
4、还可以创建带有说明的标签,用-a
指定标签名,-m
指定说明文字:
$ git tag -a v0.1 -m "version 0.1 released" e3e8870 Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git tag v0.1 v0.9 v1.0 Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git show v0.1 tag v0.1 Tagger: lipingchang <1209989516@qq.com> Date: Thu Jan 10 13:49:56 2019 +0800 version 0.1 released commit e3e8870d7d26636dfe2019964f653aa559ffba8a (tag: v0.1) Author: lipingchang <1209989516@qq.com> Date: Thu Jan 10 10:55:27 2019 +0800 ddd diff --git a/dev.txt b/dev.txt index af530c2..c65caeb 100644 --- a/dev.txt +++ b/dev.txt @@ -1 +1,2 @@ cccddd +ddd
5、删除标签 git tag -d 标签名,因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。
$ git tag -d v0.1 Deleted tag 'v0.1' (was 15a0042)
6、要推送某个标签到远程,使用命令git push origin <tagname>
$ git push origin v1.0 Total 0 (delta 0), reused 0 (delta 0) To github.com:lwx516294/gitskills.git * [new tag] v1.0 -> v1.0
7、一次性推送全部尚未推送到远程的本地标签
$ git push origin --tags Total 0 (delta 0), reused 0 (delta 0) To github.com:michaelliao/learngit.git * [new tag] v0.9 -> v0.9
8、如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除,然后,从远程删除。删除命令也是push
$ git tag -d v1.0 Deleted tag 'v1.0' (was 392899f) Administrator@PC201704271057 MINGW64 /d/gitlearn/learngit/gitskills (master) $ git push origin :refs/tags/v1.0 To github.com:lwx516294/gitskills.git - [deleted] v1.0