标签:local idt 本地存储 打开 tar address 研究 三次 read

在团队做过软件开发的,版本控制必是不可或缺的一项。目前,版本控制主要分为集中式版本控制系统和分布式版本控制系统 ,即大家熟知的SVN和Git。Git是当下最流行的分布式版本控制系统,故,今天,我们就来研究一下Git的神奇之处。
svn propget svn:mergeinfo指令;而在Git中,可以快速在几个分支中切换和快速合并;有两种Git的使用方法,一种是通过终端命令控制,另一种是使用Git的图形管理工具——SourceTree。具体两者哪一种更好用,更有效率,那还得因人而异。笔者下面将会分别介绍这两种方法的基本操作,希望读者能边读边动手操作一下,受益匪浅。
Mac-Pro:~ kys-1$ mkdir gitTest Mac-Pro:~ kys-1$ cd gitTest
git init命令,如下:
Mac-Pro:~ kys-1$ cd /Users/kys-1/Desktop/gitTest Mac-Pro:gitTest kys-1$ git init Initialized empty Git repository in /Users/kys-1/Desktop/gitTest/.git/
.git目录,若是没有看到,只需要使用ls -ah命令即可:
Mac-Pro:gitTest kys-1$ ls -ah . .. .DS_Store .git
GitTest.md文件,内容如下:
git is a strong software Let‘s start with a good mood!
gitTest所在的目录下;git add命令添加文件,如下:
Mac-Pro:gitTest kys-1$ git add GitTest.md
git commit将文件提交到仓库,如下:
Mac-Pro:gitTest kys-1$ git commit -m "wrote a README"
[master (root-commit) dfaeb43] wrote a README
1 file changed, 1 insertion(+)
create mode 100644 GitTest.md
-m后面输入的是本次提交的详细信息,比如,完成了哪个功能或者修复了哪个bug。首先,先对提交的GitTest.md中的信息进行修改,然后运行git status,效果如下:
Mac-Pro:gitTest kys-1$ 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: GitTest.md
no changes added to commit (use "git add" and/or "git commit -a")
git diff可以查看具体修改了哪些内容,如下:
Mac-Pro:gitTest kys-1$ git diff diff --git a/GitTest.md b/GitTest.md index 00ad777..64590e1 100644 --- a/GitTest.md +++ b/GitTest.md @@ -1 +1,2 @@ -git is a strong software \ No newline at end of file +git is a strong software +Let‘s start with a good mood! \ No newline at end of file
首先,使用git log命令查看历史记录,如下:
Mac-Pro:gitTest kys-1$ git log
//版本一
commit 988f92f1f5cf959d491ad63462e0c90372bb4b0b
Author: XiaolinSun <401788217@qq.com>
Date: Fri Apr 8 11:15:08 2016 +0800
add new
//版本二
commit dfaeb438504942d09e7f4282bd93b560d2ee68e2
Author: XiaolinSun <401788217@qq.com>
Date: Fri Apr 8 11:12:02 2016 +0800
wrote a README
Mac-Pro:gitTest kys-1$ git reset --hard HEAD^
HEAD is now at dfaeb43 wrote a README
Mac-Pro:gitTest kys-1$ git log
//版本三
commit dfaeb438504942d09e7f4282bd93b560d2ee68e2
Author: XiaolinSun <401788217@qq.com>
Date: Fri Apr 8 11:12:02 2016 +0800
wrote a README
很清晰,共有三次提交历史。其中,988f92f1f5cf959d491ad63462e0c90372bb4b0b这样一串字符表示提交的版本号commit id;
使用git reset命令可以回退到上一个版本,输入git reset --hard commit id,如下:
Mac-Pro:gitTest kys-1$ git reset --hard 988f92f HEAD is now at 988f92f add new
如果开发过程中,一不小心,回退错了地方,可以使用git reflog命令查看命令历史,如下:
Mac-Pro:gitTest kys-1$ git reflog
dfaeb43 HEAD@{0}: reset: moving to HEAD^
988f92f HEAD@{1}: commit: add new
dfaeb43 HEAD@{2}: commit (initial): wrote a README
从而,可以选择要回到的那个版本即可。

.git;git add把文件添加进去,实际上是把文件修改添加到暂存区;然后,用git commit提交更改,是把暂存区的内容提交到当前分支;最后,每次文件修改后,如果不add到暂存区,那是不会加入到commit。当你修改文件GitTest.md时,在你准备提交前,忽然发现一个错误,如图:
git is a strong software Let‘s start with a good mood! Start up now! Fight for future! //新添加的内容
这时候,必须要查看一下状态:输入git status,如下:
Mac-Pro:gitTest kys-1$ 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: GitTest.md
no changes added to commit (use "git add" and/or "git commit -a")
git checkout -- file命令就会丢弃工作区的修改,如下:
Mac-Pro:gitTest kys-1$ git checkout -- GitTest.md
git is a strong software Let‘s start with a good mood! Start up now!
git rm和`git commit命令就可以从版本库删除相应的文件,例如:我们新建一个newFile.md文件,然后将其加入工作区,如下所示:
Mac-Pro:gitTest kys-1$ git add newFile.md Mac-Pro:gitTest kys-1$ git commit -m "add newFile" [master e278be1] add newFile 1 file changed, 1 insertion(+) create mode 100644 newFile.md Mac-Pro:gitTest kys-1$ git rm newFile.md rm ‘newFile.md‘ Mac-Pro:gitTest kys-1$ git commit -m "delete newFile" [master 9c28795] delete newFile 1 file changed, 1 deletion(-) delete mode 100644 newFile.md
以上操作都是在本地仓库进行的一些操作,如果感觉上面讲的有些许啰嗦,那么只需记住下面几个命令即可:
1.git init -------创建空的版本库; 2.git add -------添加文件到暂存区; 3.git commit -------提交文件到版本库(仓库); 4.git status -------查看相关状态; 5.git diff -------查看具体修改的内容; 6.git log -------查看提交历史记录; 7.git reset -------回到上一版本; 8.git reflog -------查看命令历史记录; 9.git checkout -------丢弃工作区的修改; 10.git rm -------删除文件;

本地按钮,后点击新仓库,选择创建本地仓库,填入自己的地址,点击创建就OK了(是不是很方便?):

TTest本地仓库,演示一下后面的相关操作,点击新建仓库的导航条:

README.md:

解释一下,已暂存就是相当于git add操作。
git commit -m "add new info"命令:

回滚操作,选择具体的提交记录,点击右键,选择提交回滚即可;SourceTree是如此的方便快捷!.ssh目录,再查看.ssh目录下有没有id_rsa和id_rsa.pub文件,如下,
发现没有上述的两个文件,这时需要创建:
Mac-Pro:~ kys-1 $ ssh-keygen -t rsa -C "youremail@example.com"
需要将邮件地址换成自己的地址,如下:
Mac-Pro:~ kys-1$ ssh-keygen -t rsa -C "40178217@qq.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/kys-1/.ssh/id_rsa): Created directory ‘/Users/kys-1/.ssh‘. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/kys-1/.ssh/id_rsa. Your public key has been saved in /Users/kys-1/.ssh/id_rsa.pub.
出现上述描述,就证明你成功了,然后到主目录下找到.ssh目录,查看id_rsa和id_rsa.pub文件,id_rsa是私钥,需要自己保留好,id_rsa.pub是公钥,别人知道也无妨。
Account settings,SSH Keys页面,添加id_rsa.pub文件的内容:
添加SSH key的作用,是保证推送到远程仓库的提交确实是你提交的而不是别人代替,以确保项目被他人修改;
添加远程库:首先登陆github账号,点击“New repository ”按钮,创建新仓库,如下:


需要填写仓库名,描述以及是否勾选创建初始化文件等。
Mac-Pro:~ kys-1$ git remote add origin yourGitAddress
然后,将本地库的所有内容推送到远程库:
Mac-Pro:~ kys-1$ git push -u origin master
到这一步,只要本地作了提交。就可以通过命令:
Mac-Pro:~ kys-1$ git push origin master
把本地master分支的最新修改推送到GitHub上去。
git clone命令将远程库克隆到本地:Mac-Pro:~ kys-1$ git clone yourLocalGitAddress1

源URL就是远程仓库的地址,目标路径是本地存储的路径。
创建dev分支,然后切换到dev分支,使用git checkout命令:
Mac-Pro:gitTest kys-1$ git checkout -b dev Switched to a new branch ‘dev‘
-b参数表示创建并切换.
使用git branch命令查看当前分支:
Mac-Pro:gitTest kys-1$ git branch * dev master



我们在dev分支进行操作,修改README.md 文件内容然后提交:
Mac-Pro:gitTest kys-1$ git add README.md Mac-Pro:gitTest kys-1$ git commit -m "dev branch" [dev 9be4a38] dev branch 1 file changed, 2 insertions(+), 1 deletion(-)


dev分支工作结束,切换到master分支,
Mac-Pro:gitTest kys-1$ git checkout master Switched to branch ‘master‘ Your branch is up-to-date with ‘origin/master‘.
SourceTree操作:

点击右键,选择检出master分支即可。

使用git merge 指令把dev分支的工作成果合并到master分支上:
Mac-Pro:gitTest kys-1$ git merge dev
Updating 2269ea8..9be4a38
Fast-forward
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
dev分支,右击选择合并dev至master即可,
使用git branch -d dev命令删除dev分支:
Mac-Pro:gitTest kys-1$ git branch -d dev Deleted branch dev (was 9be4a38).
dev分支,右击选择删除dev即可。查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git checkout -b <name> 合并某分支到当前分支:git merge <name> 删除分支:git branch -d <name>
制造冲突:先创建一个新的分子conflict,并在新分支上工作,修改README.md内容,操作如下:
Mac-Pro:gitTest kys-1$ git checkout -b conflict Switched to a new branch ‘conflict‘ Mac-Pro:gitTest kys-1$ git add README.md Mac-Pro:gitTest kys-1$ git commit -m "make a conflict" [conflict 1bc6611] make a conflict 1 file changed, 2 insertions(+), 1 deletion(-)

master,继续修改README.md内容,并提交修改:
Mac-Pro:gitTest kys-1$ git checkout master Switched to branch ‘master‘ Your branch is up-to-date with ‘origin/master‘. Mac-Pro:gitTest kys-1$ git add README.md Mac-Pro:gitTest kys-1$ git commit -m "add two conflicts" [master f43d5d1] add two conflicts 1 file changed, 2 insertions(+), 1 deletion(-)
合并分支,就会出现冲突:
Mac-Pro:gitTest kys-1$ git merge conflict Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result. Mac-Pro:gitTest kys-1$ git status On branch master Your branch is ahead of ‘origin/master‘ by 1 commit. (use "git push" to publish your local commits) You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md no changes added to commit (use "git add" and/or "git commit -a")
查看文件README.md就能看到冲突信息的提示了,红色标记的内容就是用来标注不同分支的内容:

将内容改为以下内容就行了,然后重新提交就可以了。
We will make a conflict!
Mac-Pro:gitTest kys-1$ git add README.md Mac-Pro:gitTest kys-1$ git commit -m "comflict fixed" [master dad373b] comflict fixed

git log --graph可以看到分支合并图;Fast forward模式,这种模式又优点也有缺点,就是删除分支后会丢掉分支信息,这时,我们需要使用一下普通模式,即使用带有--no-ff的git merge,继续上述流程:新建分支dev->修改提交->切换分支->合并分支:
Mac-Pro:gitTest kys-1$ git checkout -b dev //*** Switched to a new branch ‘dev‘ Mac-Pro:gitTest kys-1$ git add README.md //*** Mac-Pro:gitTest kys-1$ git commit -m "add new content" //*** [dev 97676b7] add new content 1 file changed, 1 insertion(+) Mac-Pro:gitTest kys-1$ git checkout master //*** Switched to branch ‘master‘ Your branch is up-to-date with ‘origin/master‘. Mac-Pro:gitTest kys-1$ git merge --no-ff -m"merge with --no-ff" dev //*** Merge made by the ‘recursive‘ strategy. README.md | 1 + 1 file changed, 1 insertion(+)
git log --graph --pretty=oneline --abbrev-commit命令查看分支历史:

master分支是非常稳定的,仅用来发布新版本,但不在上面开发;其次,创建dev分支开发,等到版本发布的时候在合并到master分支即可;团队中每个人都有自己的分支,及时合并即可。情形:当你在dev分支上工作还没有完成,正赶上有Bug需要修复,这时你需要将手头上工作暂存一下,抓紧时间修复Bug;
Mac-Pro:gitTest kys-1$ git status
On branch dev
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.md
no changes added to commit (use "git add" and/or "git commit -a")
git stash暂存当前工作区:
Mac-Pro:gitTest kys-1$ git stash Saved working directory and index state WIP on dev: 40be6d0 merge with --no-ff HEAD is now at 40be6d0 merge with --no-ff

git status查看工作区,并可以创建Bug分支了;master分支修复Bug:
Mac-Pro:gitTest kys-1$ git checkout master Switched to branch ‘master‘ Your branch is up-to-date with ‘origin/master‘. Mac-Pro:gitTest kys-1$ git checkout -b bug Switched to a new branch ‘bug‘ Mac-Pro:gitTest kys-1$ git add README.md Mac-Pro:gitTest kys-1$ git commit -m "fix bug" [bug 2c013d1] fix bug 1 file changed, 1 insertion(+), 1 deletion(-)

bug分支上修复完bug提交,就可以合并删除bug分支,如下:
Mac-Pro:gitTest kys-1$ git checkout master Switched to branch ‘master‘ Your branch is up-to-date with ‘origin/master‘. Mac-Pro:gitTest kys-1$ git merge --no-ff -m "merge bug" bug Merge made by the ‘recursive‘ strategy. README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) Mac-Pro:gitTest kys-1$ git branch -d bug Deleted branch bug (was 2c013d1).


Bug修复完,我们需要将暂存区的分支拿出来了,切换到dev分支,使用git stash list命令查看:
Mac-Pro:gitTest kys-1$ git stash list
stash@{0}: WIP on dev: 40be6d0 merge with --no-ff
工作区还在,可以使用两个命令恢复:一个是git stash apply(恢复后,stash内容不删除)还需用git stash drop删除;另一个是使用git stash pop恢复的同时将stash内容也删除了:
Mac-Pro:gitTest kys-1$ git stash pop On branch dev 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.md no changes added to commit (use "git add" and/or "git commit -a")

bug分支相同,不再赘述。Mac-Pro:gitTest kys-1$ git branch -d feature error: The branch ‘feature‘ is not fully merged. If you are sure you want to delete it, run ‘git branch -D feature‘.
git branch -D feature命令强制删除:
Mac-Pro:gitTest kys-1$ git branch -D feature Deleted branch feature (was 5f7e86c).


在指定提交节点上添加标签,默认的标签是打在最新提交的节点上的,使用git tag命令添加标签,使用git show命令查看标签信息:
Mac-Pro:gitTest kys-1$ git tag V1.0 //*** Mac-Pro:gitTest kys-1$ git tag //*** V1.0 Mac-Pro:gitTest kys-1$ git show V1.0 //*** commit d0fc2232015ae6737273fa5e94310bcfd4ef231c Author: <40188217@qq.com> Date: Wed Apr 13 07:45:48 2016 +0800 add a tag diff --git a/README.md b/README.md index 9082c3c..52ef851 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ Try a new way! Try doing a different thing! Try again! I am developing!!! - +I want to make a tag!
SourceTree操作:



git tag -d <tagname>命令删除本地标签,使用git push origin <tagname>命令可以推送一个本地标签,使用git push origin --tags命令可以推送全部未推送过的本地标签,使用git push origin :refs/tags/<tagname>命令可以删除一个远程标签,操作如下:
Mac-Pro:gitTest kys-1$ git tag -d V1.0 Deleted tag ‘V1.0‘ (was 6462641)
Mac-Pro:gitTest kys-1$ git push origin V1.0 //*** Counting objects: 5, done. Delta compression using up to 8 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 497 bytes | 0 bytes/s, done. Total 5 (delta 2), reused 0 (delta 0) To https://github.com/123sunxiaolin/gitTest.git * [new tag] V1.0 -> V1.0
Mac-Pro:gitTest kys-1$ git push origin --tags //*** Counting objects: 1, done. Writing objects: 100% (1/1), 154 bytes | 0 bytes/s, done. Total 1 (delta 0), reused 0 (delta 0) To https://github.com/123sunxiaolin/gitTest.git * [new tag] V0.1 -> V0.1
Mac-Pro:gitTest kys-1$ git tag -d V0.1 //*** Deleted tag ‘V0.1‘ (was 98817ff) Mac-Pro:gitTest kys-1$ git push origin :refs/tags/V0.1 //*** To https://github.com/123sunxiaolin/gitTest.git - [deleted] V0.1
删除所有远程标签可以删除远程标签,操作非常直观:
.gitignore文件;git touch .gitignore创建. gitignore文件,然后编辑:
Mac-Pro:gitTest kys-1$ touch .gitignore Mac-Pro:gitTest kys-1$ ls -ah . .DS_Store .gitignore .. .git README.md

/mtk/ 过滤整个文件夹
*.zip 过滤所有.zip文件
/mtk/do.c 过滤某个具体文件
gitignore还可以指定要将哪些文件添加到版本管理中,只是在文件前加一个!即可:
!*.zip
!/mtk/one.txt
其中,!/mtk/one.txt只需要管理/mtk/目录中的one.txt文件,这个目录中的其他文件都不需要管理,说到这想必大家都明白了。
唯一的区别就是规则开头多了一个感叹号,Git会将满足这类规则的文件添加到版本管理中。
. gitignore文件推送到远程仓库即可。
标签:local idt 本地存储 打开 tar address 研究 三次 read
原文地址:http://www.cnblogs.com/aksir/p/6852593.html