因为Git是分布式版本控制系统,所以,每个机器都必须自报家门
注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
或者使用ssh-keygen -t rsa -C "我的SSH密钥"
创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录:
$ mkdir learngit
$ cd learngit
$ pwd
通过git init命令把这个目录变成Git可以管理的仓库:
瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了
文件必须位于git的目录下
向本地版本库添加文件: git add file
提交修改: git commit -m "comment"
查看当前分支状态: git status
版本回退;
- HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。
- 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本,git log --graph/git log --graph --pretty=oneline 查看版本历史
- 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。
- git reset --hard HEAD^ 会退到上一个版本
工作区:本地init的文件夹
版本库:隐藏的.git文件夹,包括暂存区stage,以及git为我们创建的master分支,master指针HEAD
撤销修改:
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file。
场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。
场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。
命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
删除文件
阅读: 558479
在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交:
$ git add test.txt
$ git commit -m "add test.txt"
[master b84166e] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:
$ rm test.txt
这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
现在,文件就从版本库中被删除了。
小提示:先手动删除文件,然后使用git rm <file>和git add<file>效果是一样的。
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
$ git checkout -- test.txt
添加远程仓库:
1.在远程创建仓库
2.在工作区运行命令:git remote add origin https://github.com/TonyLeeOne/RepositoryForConfigurations.git
或者git remote add origin [email protected]:TonyLeeOne/RepositoryForConfigurations.git
3.git status ,若有修改要提交,使用git commit -m "comment"或者添加文件git add file在提交 git commit -m "comment"
4.运行git push -u origin master 提交本地文件到远程仓库
5.从现在起,在提交到远程只需要使用: git push origin master
从远程库克隆
要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。
Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。
$ git clone [email protected]:michaelliao/gitskills.git
Cloning into 'gitskills'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Receiving objects: 100% (3/3), done.
创建与合并分支
git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
$ git branch dev
$ git checkout dev
用git branch命令查看当前分支
git checkout branch 切换到指定分支
Git鼓励大量使用分支:
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
解决冲突
当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。
用git log --graph命令可以看到分支合并图。
分支管理策略
首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;
那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;
你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。
Git分支十分强大,在团队开发中应该充分应用。
合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并
Bug分支
修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。
Feature分支
开发一个新feature,最好新建一个分支;
如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除。
多人协作
- 查看远程库信息,使用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,如果有冲突,要先处理冲突。
Rebase
- rebase操作可以把本地未push的分叉提交历史整理成直线;
- rebase的目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比。
$ git rebase
First, rewinding head to replay your work on top of it...
Applying: add comment
Using index info to reconstruct a base tree...
M hello.py
Falling back to patching base and 3-way merge...
Auto-merging hello.py
Applying: add author
Using index info to reconstruct a base tree...
M hello.py
Falling back to patching base and 3-way merge...
Auto-merging hello.py
特点:把分叉的提交历史“整理”成一条直线,看上去更直观。缺点是本地的分叉提交已经被修改过了
创建标签
git tag tagname
查看所有标签
git tag
- 命令git tag <tagname>用于新建一个标签,默认为HEAD,也可以指定一个commit id;
- 命令git tag -a <tagname> -m "blablabla..."可以指定标签信息;
- 命令git tag可以查看所有标签。
git show tag 显示标签描述
对以提交的内容打标签;
git log --pretty=oneline --abbrev-commit
9ce7f5a (HEAD -> master, tag: v1.o, tag: v1.1, origin/master) sda
89882e3 hello
git tag v0.9 89882e3
操作标签
删除标签: git tag -d v0.9
如果要推送某个标签到远程,使用命令git push origin <tagname>:
Tony:repository tony$ git push origin v1.1
Counting objects: 1, done.
Writing objects: 100% (1/1), 159 bytes | 159.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/TonyLeeOne/RepositoryForConfigurations.git
* [new tag] v1.1 -> v1.1
一次性推送全部尚未推送到远程的本地标签:
Tony:repository tony$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/TonyLeeOne/RepositoryForConfigurations.git
* [new tag] v1.o -> v1.o
Tony:repository tony$
如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:
$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)
然后,从远程删除
Tony:repository tony$ git push origin :refs/tags/v1.o
To https://github.com/TonyLeeOne/RepositoryForConfigurations.git
- [deleted] v1.o
- 命令git push origin <tagname>可以推送一个本地标签;
- 命令git push origin --tags可以推送全部未推送过的本地标签;
- 命令git tag -d <tagname>可以删除一个本地标签;
- 命令git push origin :refs/tags/<tagname>可以删除一个远程标签。
配置别名
git config --global alias.st status
git st就表示git status
git branch -a 查看所有分支,包括本地和远程
git clone -b develop [email protected]:SGM_INT_B2C/MS_Gateway.git
clone某个分支下的内容
git push origin master -f 强制将本地推送到远程