git——分布式版本控制系统

git

git 分布式 版本控制系统、协作编辑工具(cvs、svn都是集中式)。

git有一个工作区和暂存区,每个分支有一个提交区。

git 中的HEAD表示当前版本,是指针,版本号实际是sha1的16进制字符串。

标签 tag 只是作为人可读的表示形式,标签一定有名字,可带标注

git commit信息规范,(): 指本次提交更改的类型,一般为 feat新特性、fix解决bug,docs, style代码排版改动,refactor重构,不影响代码功能逻辑,test,chore辅助工具或构建过程改动。 scope是指影响范围。subject是简单描述。

相对提交名:一个提交有多个父提交,对于提交名C(id或ref),^(C2)操作符用于选取其第n(第2个)提交,默认第一个;`~`<n>(C~2)用于选取其第n代前辈提交的第一个(第2带前辈即祖父辈提交的第一个,相当于C11亦即C^)

概念:

  • repository仓库 一个项目及其git相关资源
  • branch分支 一个repository有一个主分支(master)
  • ...

git命令及功能:


#初始化当前目录为git管理的目录
git init  
#配置(默认)用户名
git config [--global] user.name 'xxx'  
#配置(默认)邮箱
git config [--global] user.email 'xx@x.com'  
#查看配置
git config [--global] user.name/user.email
#查看当前所有配置
git config --list
#查看当前全局配置
git config --global
#删除配置
git config --unset <key>
#绑定到远程仓库

git remote add origin http://...    
#查看绑定的远程仓
git remote -v  
#改变远程仓库url
git remote set-url origin git@xxx.xx/xx.git

#添加或更新已修改的文件
git add FILES...  
#添加整个目录及其所有内容
git add DIR
git add .   #添加所有
#获取远程仓库更新
git pull  
#查看版本改动状态 
git status  

# commit 提交改动
#提交修改
git commit -m 'comment'  
#将提交推送到远程仓库
git push -u <remote> <branch>       # -u是设置当前分支的upstream
git push -u origin master
git push        #如果已设置upstream,可省略<remote> <branch>

#修改最近一次提交的注释
git commit --amend (进入注释修改界面)

#查看提交历史
git log [--pretty=oneline 简要输出] [--follow所有关联记录] [/path/to/file | branch]  
git show commit-id  

#撤销提交
git reset [--hard|--soft] <commit-id>   #--hard会丢弃未提交的改动,使用先请仔细检查。该方式实质是重置指针,<commit-id>为你想要回退到的某个历史提交
#撤销推送到远程的提交(先进行本地撤销)
git push --force [<remote> <branch>]  #必须加上--force,在被保护的分支往前force push会失败(pre-receive hook declined)

#操作命令历史(HEAD指针变化历史)
git reflog  


#重置到历史版本(回退版本):
git log #查看历史版本号
#git blame ... #查看选项中指定内容的提交者
git reset --hard commit-id # 版本号不定写全,写能区分的前面字符即可。注意--hard会丢弃未提交的改动,使用前请仔细检查

#回退到历史版本后又想重置到后面某个版本:
git reflog #查看版本号
git reset --hard commit-id


# clone branch克隆分支:
git clone -b <branch> <remote_repo>
git clone -b mybranch [origin/mybranch] https://xxx.com/xx/xx.git
#clone master branch
git clone https://xxx.com/xx/xx.git 或 git clone git@:xxx.com:xxx/xx.git


# clone single branch只克隆分支:
git clone -b <branch> --single <remote_repo>

# list branches列出分支: 
git branch    

# list remot branches远程分支:
git branch -r  

# list branches including those on remote列出本地及远程分支:
git branch -a  

# switch to another local branch: 
git checkout <branch> # 这个命令只是切换到分支,不会从远程checkout资源到本地

# check out remote branch远程的branch 
# checkout到本地需要有名字(通常与远程的一致): 
git checkout -b <local-branch-name> <remote>/<remote-branch>  

# create a new branch创建(并转到)分支: 
git checkout -b <branch-name>  
git branch branch-name [commit-id] 从commit-id(默认HEAD)新建分支

# delete a branch
git branch -d <branch-name>
git branch -D <branch-name>   git branch -d -f <name> # delete force


git rev-parse <SHA1前缀>  输出完整sha1;git rev-parse master; git rev-parse master~3^2^3  
git rev-parse <标签名>  输出完整sha1  
git cat-file -p <SHA1> 输出对象内容(文件对象或目录树对象)
git list-files -s  
git hash-object /path/to/data 计算对象的hash  
git commit -a (--all)会提交所有变化,包括untracked, unstaged, rm等的  

# revert=rm+checkout回复文件:
git rm --cached -f -- src/main/path/to/file
git checkout HEAD -- src/main/path/to/file

# branch
#从当前提交点创建新分支
git branch <name>
#从当前提交点创建新分支 ,同时切入,利用chekcout
git checkout -b <branch-name>
#列举所有分支(local&remote)
git branch -a
#删除远程分支
git push origin :branch-name
git show-branch查看分支提交  
git show branch-name:/path/to/file 打印指定分支中文件的内容
gitk 显示提交图  
#重命名当前分支
git branch -m new-name
#重命名指定分支
git branch -m old-name new-name
#Delete the old-name remote branch and push the new-name local branch
git push origin :old-name new-name


# 配置命令及对应存储位置
git config <---> .git/config  
git config --global <---> ~/.git/config  
git config --system <---> /etc/gitconfig  

git config --unset <config-name>  
git config -l(--list) [--global | --systemrr]

# tag
git tag # list tags
git tag <name> # creat a tag
git tag <name> <commit-id>
git push --tags # push all newly created tags

#merge 
#将其他分支的并入到当前分支
git merge <another-branch>
#如果发生冲突,想要撤销合并
git merge --abort

git可设置命令别名。
git仓库下的文件处于三种状态之一:tracked, untracked, ignored。
基于内容追踪,而非文件位置(文件名)。

新建branch需基于一个branch,也就说新建的branch并非什么文件都没有,而是在一个branch的基础上,在做任何修改前,新branch与基础branch是一致的。

git中的ref:
HEAD: 当前分支的最近提交
ORIG_HEAD: merge, reset等操作前的当前HEAD
FETCH_HEAD:
MERGE_HEAD: merge时其他分支的HEAD

在一个分支修改文件后,如果要checkout出另一个分支,那checkout出的新文件会覆盖原文件,应该先commit当前的修改,然后checkout新分支,如此,将分支checkout回来时git才知道应该如何恢复文件。

分支名中可包含/,以创建分级名字,但不得以/结尾。

环境变量
GIT_EDITOR配置git中的编辑器(在需要时,如填写注释等,git将打开编辑器)
GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL
GIT_DIR git文件的目录名,默认.git
环境变量会覆盖git config配置

eclipse中git merge冲突,手动解决冲突后需team->git add index使得状态变成可提交。

posted @ 2019-03-04 22:58  二球悬铃木  阅读(223)  评论(0编辑  收藏  举报