之前配置过Git,但用的也是可视化工具sourceTree,但对于我这种转行的小白,原理一直是一知半解,只知道一些简单的功能键。配置teamcity和jenkins的时候也是吃了不少苦头,今天把这些内容整理下来,以后用到也能顺手点。
不同于集中式的版本控制系统,git是一个分布式版本控制系统。最大的区别是,本地会保存所有版本的内容,这样在中央库崩溃或者离线的情况时,依旧可以完成大部分的操作,减少与中央库的网络数据交互,提升效率。另外,越来越多的开源项目都迁移到了Github上,如果需要查看这部分代码,使用git最为方便。
git的安装教程网上很多,首先从https://git-scm.com/download上下载对应系统的版本,然后按照教程安装就好。安装好后可以查看版本git --version。windows下除了有专门的git shell之外,还可以用git Bash,进行命令行的操作。
git config --global user.namegit config --global user.email
git查询文档的命令:
git config --helpgit help configman git-config
基本的config增删改查操作:
BruceChan@changjiang MINGW32 ~/Desktop$ git config --global --unset user.email changjiang*BruceChan@changjiang MINGW32 ~/Desktop$ git config --global --listuser.name=Brucecore.autocrlf=truefilter.lfs.clean=git-lfs clean %ffilter.lfs.smudge=git-lfs smudge %ffilter.lfs.required=truepush.default=matchingalias.lol=log --oneline --decorate --graph --allBruceChan@changjiang MINGW32 ~/Desktop$ git config --global user.nameBruceBruceChan@changjiang MINGW32 ~/Desktop$ git config --global --get user.nameBruceBruceChan@changjiang MINGW32 ~/Desktop$ git config --global --add user.email xxx@yyy.comBruceChan@changjiang MINGW32 ~/Desktop$ git config --global --listuser.name=Bruceuser.email=xxx@yyy.comcore.autocrlf=truefilter.lfs.clean=git-lfs clean %ffilter.lfs.smudge=git-lfs smudge %ffilter.lfs.required=truepush.default=matchingalias.lol=log --oneline --decorate --graph --all
另外,config还可以给命令起别名(以查看dubbo历史记录演示):
f6cea42 DUBBO-422 修改父节点都为持久结点。5edb6a0 修改profile08c151f 修改profile7f3fabb DUBBO-370 泛化调用性能优化7eb7662 修改READMEa2189d1 修改demo依赖b8a90e8 修改demo依赖362071e DUBBO-422 订阅时创建节点,以免出现订阅空节点错误。c73252f DUBBO-422 订阅时创建节点,以免出现订阅空节点错误。20d5a66 DUBBO-435 zookeeper在删空数据后,没有通知empty消息:BruceChan@changjiang MINGW32 /e/company_project_git/dubbo ((dubbo-2.4.11))$ git config --global alias.lol "log --oneline"BruceChan@changjiang MINGW32 /e/company_project_git/dubbo ((dubbo-2.4.11))$ git lol0e1752e move executes attributes to abstractMethodType, so as to config executes on method40b5dad add timeout ZkclientZookeeperClient45e66f4 fix classload problem when dubbo jar at tomcat libs9a9d05f add dubbo-maven/src/ to git ignore261e71f update pom version to 2.4.11caff4bc update version to 2.4.107dd34d5 DUBBO-625 业务对象的toString方法异常会导致RemotingInvocationTimeoutScan失效2d73e4f DUBBO-635 JValidator在类名生成的类名有$,有frozen class异常aabd4bc DUBBO-633 fix ut failc81e278 解决method的配置executes参数的xsd验证错误ce1dde0 修改Redis注册中心宕机时,应用占用cpu高的问题。
---------------------------------------------------------------------------------------->
Git使用40个十六进制字符的SHA1 Hash来唯一标示一个对象,Git中所指的对象有四种,blob(一个文件)、tree(一个目录)、commit(一次提交)、tag(一个固定的版本)。一个tag指向一个commit,而一个commit又指向一个tree对象,这代表工作区的提交时的某个状态,一个tree对象又指向其它tree和blob,如果文件内容一致,他们指向一个blob,文件的名称等其他信息存放在tree对象中。对这些对象的内容进行sha1 hash之后,就可以得到唯一标示。
有了对象,我们就需要用到git仓库来对它们进行操作。git仓库分为带工作区--non-bare(含有.git)与不带工作区--bare两种,后者通常放在服务器上以供协作。对于裸仓(bare),它的工作文件就直接放在了仓库目录下,而非裸仓则放到了.git文件目录下。
git init git_non_bare_repogit init git_bare_repo --bare
注意裸仓和非裸仓在提示master的区别哦。如果已经存在了一份工作的代码,可以直接到这个目录下面,用git init这个命令来初始化一份git的工作空间。
克隆本地库或者远程地址:
git clone git_bare_repo/ git_bare_repo2
如果是远程的仓库地址,可以直接使用默认的仓库地址名称,除非与本地的地址名称有冲突。
---------------------------------------------------------------------------------------->
有了仓库,我们就可以进行基本操作,下面这张图基本囊括了几个比较重要的基本操作:
working directory是工作区,是日常管理代码的区域,它维护着一个树形结构;history repository是commit指向的tree结构,而staging area是工作区和历史区的中间层,代表需要提交的状态,维护虚拟结构。
git add -- 将工作区代码提交到历史git commit --将暂存区代码提交到服务器git status --查看暂存区的状态git rm -- 从暂存区删除不需要的元素git mv --工作区中重命名、移动文件并将之添加到暂存区gitignore --确定工作区里不想提交的文件(规则此处不详述)
基本操作:
创建文件提交暂存区,提交历史记录以及修改文件并提交:
BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ touch m nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c m nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statuswarning: LF will be replaced by CRLF in a.The file will have its original line endings in your working directory.On branch masterChanges to be committed: (use "git reset HEAD..." to unstage) modified: aUntracked files: (use "git add ..." to include in what will be committed) m nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git add .BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git commit -m "create m and n"[master warning: LF will be replaced by CRLF in a.The file will have its original line endings in your working directory.64e3367] create m and nwarning: LF will be replaced by CRLF in a.The file will have its original line endings in your working directory. 3 files changed, 1 insertion(+) create mode 100644 m create mode 100644 nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masternothing to commit, working directory clean
BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c m nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ vim mBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: mno changes added to commit (use "git add" and/or "git commit -a")BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git add .warning: LF will be replaced by CRLF in m.The file will have its original line endings in your working directory.giBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git commit -m "modify file m"[master warning: LF will be replaced by CRLF in m.The file will have its original line endings in your working directory.a83d53f] modify file mwarning: LF will be replaced by CRLF in m.The file will have its original line endings in your working directory. 1 file changed, 1 insertion(+)
git rm <file>删除的是工作区文件,同时把状态自动add进了暂存区,此时如果想从历史区checkout的话,需要先将暂存区的状态抹掉
BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git rm mrm 'm'BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) deleted: mBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git checkout merror: pathspec 'm' did not match any file(s) known to git.BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git reset HEAD mUnstaged changes after reset:D mBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git checkout mBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c m n
如果只是将暂存区的文件抹掉,可以用git rm -cached <file>,此时会发现在暂存区出现删除了file ,但工作区中,该文件仍然存在:
BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git rm --cached arm 'a'BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) deleted: aUntracked files: (use "git add ..." to include in what will be committed) aBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git commit -m "delete a in history but keep in working directory"[master c28a690] delete a in history but keep in working directory 1 file changed, 1 deletion(-) delete mode 100644 aBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c m nBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masterUntracked files: (use "git add ..." to include in what will be committed) anothing added to commit but untracked files present (use "git add" to track)
修改文件名称用git mv <fileFrom> <fileTo>
BruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ lsa b c n xBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git mv x yBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD..." to unstage) renamed: x -> yBruceChan@changjiang MINGW32 /e/company_project_git/git_non_bare_repo (master)$ git commit -m "rename x to y"[master d24f75f] rename x to y 1 file changed, 0 insertions(+), 0 deletions(-) rename x => y (100%)