Git 总结
描述
git是管理项目的一种工具,目前开发人员比较常用的工具,git简单实用,管理项目方便,接下来简单的介绍下git的常用命令。
Git官网:https://git-scm.com/
Git 使用工具
下载官网:https://www.syntevo.com/smartgit/download/ 该工具简单使用,操作方便,是开发人员的必备神器
Git 管理项目模型
Remote:远程仓库的地址
Repository:本地仓库区,用于存放本地的仓库代码
Index/Stage:代码的暂存区域
Workspace:工作区域,也就是自己开发的代码
Git常用操作
下载Git
yum系列: sudo yum -y install git
apt系列: sudo apt-get install -y git
新建代码库
初始化项目,需要在项目中执行
$ git init
Initialized empty Git repository in F:/jbossWorkspace/testgit/.git/
新建一个目录,并初始化代码库
$ git init test
Initialized empty Git repository in F:/jbossWorkspace/xiaoxu/test/.git/
克隆远程代码
$git clone git@github.com:xfg0218/testgit.git
Cloning into 'testgit'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
克隆指定版本的代码库
$git clone -b v1.0.1 git@github.com:xfg0218/testgit.git
-b:是制定的版本
配置信息
显示当前git的配置
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=F:/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
credential.helper=manager
user.name=xiaoxu
user.email=1234@qq.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=git@github.com:xfg0218/testgit.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
增加与删除文件到本地仓库
添加制定后缀的文件到本地仓库
$ git add *.java
或
$ git add *
制定文件夹下的所有文件到本地仓库
$ git add com/*
增加所有的文件到本地仓库
$ git add .
显示新增加的数据
$ git add -p
diff --git a/test/test.java b/test/test.java
index b9a1dd0..3e72fc2 100644
--- a/test/test.java
+++ b/test/test.java
@@ -1 +1 @@
-dddd
+ddddaaaa
删除本地仓库的本地文件
以下操作会删除工作区的文件,请谨慎操作
$ git rm -f -r com/*
rm 'com/test/test.java'
$ git rm --cached test/test.java
rm 'test/test.java'
提交本地的代码到远程仓库
给所有的本地文件添加注解到本地仓库
$ git commit -m "add message"
[master 0d71aca] add message
1 file changed, 1 insertion(+)
create mode 100644 test/test.java
给制定的文件添加注解到本地仓库
$git add *.log
添加提交说明
$ git commit -m "add new file"
[master 648d583] add new file
1 file changed, 1 insertion(+)
create mode 100644 test.log
提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a
提交时显示所有diff信息
$ git commit -v
提交代码到master分支,如果有其他分支请指定其他分支即可
git push -u origin master
分支管理
分支查看
列出当前本地仓库的所有分支
$ git branch
列出远程所有的分支
$ git branch -r
列出本地分支与远程分支
$ git branch -a
创建分支
创建一个分支,并把分支指向当前的分支
$ git branch [branch-name]
例如以下命令即使创建了一个0.0.1的分支
$ git branch 0.0.1
新建一个分支,并切换到该分支
$ git checkout -b [branch]
例如
$ git checkout -b 0.0.1
创建一个分支并指向commit
$ git branch [branch-name] [commit]
切换分支
$ git checkout [branch-name]
合并分支
合并指定分支到当前分支
$ git merge [branch-name]
删除分支
删除本地分支
$ git branch -d [branch-name]
删除远程分支
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
标签管理
查看标签
$ git tag
$ git show [tag-name]
新建标签
新建一个tag在当前commit
$ git tag [commit]
新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]
删除标签
删除本地的标签
$ git tag -d [tag-name ]
删除远程的标签
$ git push origin :refs/tags/[tagName]
解决冲突
冲突的文件样式如下
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
其中,git使用<<<<<<<,=======,>>>>>>>标记文件中自己和别人产生冲突的部分。
在<<<<<<<,=======之间为自己的代码;
=======,>>>>>>>之间为别人的代码。
如果保留自己的代码,将别人的代码删掉即可。
冲突解决后提交
git status
git add ***
git commit -m "fix conflict"
git push origin 分支名
查看信息命令
显示有变更的文件
$ git status
显示当前分支的版本历史
$ git log
显示commit历史,以及每次commit发生变更的文件
$ git log --stat
搜索提交历史,根据关键词
$git log -S [keyword]
显示指定文件相关的每一次diff
$ git log -p [file]
显示最近5次提交的信息
$ git log -5 --pretty --oneline
显示本地仓库与远程仓库的代码差异
$ git diff
显示暂存区和上一个commit的差异
$ git diff --cached [file]
# 显示工作区与当前分支最新commit之间的差异
$ git diff HEAD
显示今天你写了多少行代码
$ git diff --shortstat "@{0 day ago}"
显示最近提交的历史记录
$ git reflog
# 从本地master拉取代码更新当前分支:branch 一般为master
$ git rebase [branch]
远程仓库同步
下载远程仓库的所有变动
$ git fetch [remote]
显示所有远程仓库
$ git remote -v
显示某个远程仓库的信息
$ git remote show [remote]
增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]
# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]
# 上传本地指定分支到远程仓库
$ git push [remote] [branch]
# 推送所有分支到远程仓库
$ git push [remote] --all
撤销命令
恢复暂存区的文件
$ git checkout [file]
# 恢复某个commit的指定文件到暂存区和工作区
$ git checkout [commit] [file]
# 恢复暂存区的所有文件到工作区
$ git checkout .
# 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard
# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop
生成可以发布的版本
# 生成一个可供发布的压缩包
$ git archive
常用git命令
git checkout master
git checkout -b issues1234
git push origin issues1234
git add ..
git commit -m "***"
git push origin issues1234