通常刚安装完git都会进行配置用户信息
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
git 存在三份配置文件。
linux
system /etc/gitconfig global ~/.gitconfig local .git/config
window
system D:/software/Git/mingw64/etc/gitconfig global C:/Users/Administrator/.gitconfig local .git/config
三份配置分别对应到指令 系统配置
git config --system
用户配置
git config --global
本地配置(指定仓库配置)
git config --local
作用范围 权重 local > global > system
使用指令查看当前目录使用值
git config user.name(key)
使用指令查看所有配置(相同key以最下方的为准)
git config -l
root用户执行以下命令配置文件
[root@localhost /]# mkdir data/test [root@localhost /]# cd data/test/ [root@localhost test]# git init Initialized empty Git repository in /data/test/.git/ [root@localhost test]# git config --local user.name 'localUser' [root@localhost test]# git config --global user.name 'globalUser' [root@localhost test]# git config --system user.name 'systemUser'
测试1
global 配置为每一个登陆用户单独一份
system 配置为系统配置(作用域最广)
local 为本地仓库配置(对应到一个项目仓库)
[root]
[root@localhost test]# git config --global user.name globalUser [root@localhost test]# git config --local user.name localUser [root@localhost test]# git config --global user.name globalUser [root@localhost test]# git config --system user.name systemUser
[gituser]
[gituser@localhost test]$ git config --local user.name localUser [gituser@localhost test]$ git config --global user.name [gituser@localhost test]$ git config --system user.name systemUser
测试2
root 增加配置
[root@localhost test]# git config --global user.email '786725551@qq.com' [root@localhost test]# git config --system user.email 'yui@test.com'
[root]
[root@localhost test]# git config user.email 786725551@qq.com [root@localhost test]# git config user.name localUser
1.三份配置同时存在 user.name 取 local 配置
2.global 和 system 配置同时存在 user.email ,local 不存在,去global
权重 local > global > system
测试3
git config -l
[root]
[root@localhost test]# git config -l user.name=systemUser user.email=yui@test.com user.name=globalUser user.email=786725551@qq.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true user.name=localUser
[root@localhost data]# mkdir my_repository [root@localhost data]# cd my_repository/ [root@localhost my_repository]# git init Initialized empty Git repository in /data/my_repository/.git/
1.添加文件,命令将文件内容添加到索引(将修改添加到暂存区)。也就是将要提交的文件的信息添加到索引库
git add fileName
2.提交文件
git commit -m '提交日志信息'
说明:add 可以多次使用。之后 commit 把 add 的文件一次性提交到仓库中
对文件进行修改后
1.查看当前仓库信息
git status
# On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.md # # 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 # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # testFile.md
在 master 分支中, add 没 commit (带提交), 修改没 add 没 commit, 新增没 add 没 commit
2.查看文件修改内容
git diff
diff --git a/readme.md b/readme.md index b5d1636..8fe7fa9 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,4 @@ # this is my test of git # this is the second commit # add +# test diff diff --git a/testFile.md b/testFile.md index 0bf9a0f..55ed874 100644 --- a/testFile.md +++ b/testFile.md @@ -1 +1,2 @@ # the test file 2 +# test diff
git log命令显示从最近到最远的提交日志
git log
commit 5888679a615f5f2d2387b1edd35718f44f0a9814 Author: globalUser <786725551@qq.com> Date: Wed Jan 23 00:51:56 2019 -0500 commit 4 commit a80f4aeca6efffa0ab36f6f403453b1a1e83107c Author: globalUser <786725551@qq.com> Date: Tue Jan 22 22:51:13 2019 -0500 add testFile.md update readme.md third[C commit 1566217be62b7e69146a0e5cadeca86e3ecdf019 Author: globalUser <786725551@qq.com> Date: Tue Jan 22 22:48:13 2019 -0500 second commit commit a2d84e1c4c818b0a2babed82bdcd2c581c506a8f Author: globalUser <786725551@qq.com> Date: Tue Jan 22 22:38:52 2019 -0500 add readme.md
隐藏详细信息
git log --pretty=oneline
[root@localhost my_repository]# git log --pretty=oneline 5888679a615f5f2d2387b1edd35718f44f0a9814 commit 4 a80f4aeca6efffa0ab36f6f403453b1a1e83107c add testFile.md update readme.md third[C 1566217be62b7e69146a0e5cadeca86e3ecdf019 second commit a2d84e1c4c818b0a2babed82bdcd2c581c506a8f add readme.md
类似 5888679a615f5f2d2387b1edd35718f44f0a9814 是版本号 id
查看被回滚(见 2.5)的版本
git reflog
[root@localhost my_repository]# git log --pretty=oneline 1566217be62b7e69146a0e5cadeca86e3ecdf019 second commit a2d84e1c4c818b0a2babed82bdcd2c581c506a8f add readme.md [root@localhost my_repository]# git reflog 1566217 HEAD@{0}: reset: moving to HEAD^^ 5888679 HEAD@{1}: commit: commit 4 a80f4ae HEAD@{2}: commit: add testFile.md update readme.md third[C 1566217 HEAD@{3}: commit: second commit a2d84e1 HEAD@{4}: commit (initial): add readme.md
log 路线图
git log --graph --pretty=oneline --abbrev-commit
由于 git 是分布式的版本控制系统所有,它使用的版本号id不限svn一样是一个递增的数字,而是一个 SHA1 计算出来的一个非常大的数字,用十六进制表示
git reset --hard HEAD^^
[root@localhost my_repository]# git log --pretty=oneline 5888679a615f5f2d2387b1edd35718f44f0a9814 commit 4 a80f4aeca6efffa0ab36f6f403453b1a1e83107c add testFile.md update readme.md third[C 1566217be62b7e69146a0e5cadeca86e3ecdf019 second commit a2d84e1c4c818b0a2babed82bdcd2c581c506a8f add readme.md [root@localhost my_repository]# git reset --hard HEAD^^ HEAD is now at 1566217 second commit [root@localhost my_repository]# git log --pretty=oneline 1566217be62b7e69146a0e5cadeca86e3ecdf019 second commit a2d84e1c4c818b0a2babed82bdcd2c581c506a8f add readme.md
git reset --hard 版本号
[--soft | --mixed | --hard | --merge | --keep] 此处暂且不讲
版本号可以只写前几位,唯一即可;
HEAD 为当前版本
HEAD^ 为上一个版本,多少个^多少个版本前
HEAD~100 表示 100 个^
工作区(Working Directory):仓库目录
版本库(Repository):工作区中的 .git 目录
暂存区(stage):在.git 文件夹里面还有很多文件,其中有一个index 文件 就是暂存区也可以叫做 stage ,git还为我们自动生成了一个分支master以及指向该分支的指针head
git add 是将文件添加到 stage 中, 而 git commit 这是将暂存区的文件提交到仓库中 已经修改的文件如果没有添加到暂存区中,是不会被 git commit 提交上去的
add 之前
git checkout -- file
git checkout 其实是用版本库里的版本替换工作区的版本
commit 之前,add 之后
git reset HEAD
git rm file
[root@localhost my_repository]# git rm test.md rm 'test.md' [root@localhost my_repository]# ls readme.md [root@localhost my_repository]# git commit -m "del test.md" [master b67629b] del test.md 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test.md
先在 github 创建一个仓库
本地关联到远程仓库
git remote add origin https://github.com/XuZhuohao/my_repository.git
推送至远程仓库
git push -u origin master
[root@localhost my_repository]# git remote add origin https://github.com/XuZhuohao/my_repository.git [root@localhost my_repository]# git push -u origin master Username for 'https://github.com': xuzhuohao^H^H Password for 'https://xuzhuoh@github.com': [root@localhost my_repository]# git push -u origin master Username for 'https://github.com': 786725551@qq.com Password for 'https://786725551@qq.com@github.com': Counting objects: 20, done. Compressing objects: 100% (13/13), done. Writing objects: 100% (20/20), 1.58 KiB | 0 bytes/s, done. Total 20 (delta 3), reused 0 (delta 0) remote: Resolving deltas: 100% (3/3), done. To https://github.com/XuZhuohao/my_repository.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
克隆远程仓库
git clone git@github.com
/my_repository.git
[root@localhost data]# mkdir my_repository02 [root@localhost data]# cd my_repository02 [root@localhost my_repository02]# ls [root@localhost my_repository02]# git clone git@github.com:XuZhuohao/my_repository.git Cloning into 'my_repository'... Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts. remote: Enumerating objects: 20, done. remote: Counting objects: 100% (20/20), done. remote: Compressing objects: 100% (10/10), done. Receiving objects: 100% (20/20), done. Resolving deltas: 100% (3/3), done. remote: Total 20 (delta 3), reused 20 (delta 3), pack-reused 0
GitHub给出的地址不止一个。实际上,Git支持多种协议,默认的 git:// 使用ssh,但也可以使用https等其他协议。
使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https
1.创建SSH Key
ssh-keygen -t rsa -C "youremail@example.com"
可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人
2.github 生成 SSH Key
github 个人页面 -> 右上角 Settings -> SSH and GPG keys -> SSH keys -> key 输入框中输入 id_rsa.pub 的数据 -> add SSH key
3.由 http 切换到 ssh 取消连接
git remote remove origin
连接到远程
git remote add origin git@xxxxx
[root@localhost my_repository]# git remote remove origin [root@localhost my_repository]# git remote add origin git@github.com:XuZhuohao/my_repository.git
创建分支
git branch dev
切换分支
git checkout dev
创建并切换分支
git checkout -b devTest
分支存在是会有异常
-B 这表示强制创建,存在分支会被重建
[root@localhost my_repository]# git branch dev [root@localhost my_repository]# git checkout dev Switched to branch 'dev' [root@localhost my_repository]# git checkout devTest error: pathspec 'devTest' did not match any file(s) known to git. [root@localhost my_repository]# git checkout -b devTest Switched to a new branch 'devTest' [root@localhost my_repository]# git checkout dev Switched to and reset branch 'dev' [root@localhost my_repository]# git checkout -b devTest fatal: A branch named 'devTest' already exists. [root@localhost my_repository]# git checkout -B devTest Switched to and reset branch 'devTest'
显示分支列表
git branch
[root@localhost my_repository]# git branch dev * devTest master
git branch命令会列出所有分支,当前分支前面会标一个* 号
分支合并
git merge branchName
将 branchName 合并到当前分支
[root@localhost my_repository]# git checkout master Switched to branch 'master' [root@localhost my_repository]# git merge dev Updating fecde0f..af83606 Fast-forward readme.md | 1 + 1 file changed, 1 insertion(+)
删除 删除分支
git branch -d name
[root@localhost my_repository]# git branch -d devTest Deleted branch devTest (was fecde0f). [root@localhost my_repository]# git branch dev * master
强行删除
git branch -D name
存在冲突 自动合并失败;修复冲突,然后提交结果
[root@localhost my_repository]# git merge dev Auto-merging readme.md CONFLICT (content): Merge conflict in readme.md Automatic merge failed; fix conflicts and then commit the result. [root@localhost my_repository]# git status # On branch master # 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") [root@localhost my_repository]# vim readme.md [root@localhost my_repository]# git add readme.md [root@localhost my_repository]# git commit -m "update" [master eae1d5c] update [root@localhost my_repository]# git log --graph--pretty=oneline --abbrev-commit fatal: unrecognized argument: --graph--pretty=oneline [root@localhost my_repository]# git log --graph --pretty=oneline --abbrev-commit * eae1d5c update |\ | * 59408a7 k * | 60e17ad tes |/ * af83606 update readme.md ...
通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。
如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息
git merge --no-ff -m "merge with no-ff" dev
[root@localhost my_repository]# git merge --no-ff -m "merge with no-ff" dev Merge made by the 'recursive' strategy. readme.md | 1 + 1 file changed, 1 insertion(+)
保存当前更改( commit 之前)
git stash
显示保存的版本
git stash list
恢复到某个状态
git stash apply stash@{0}
删除某个状态
git stash drop stash@{0}
恢复并删除状态
git stash pop
apply 和 drop 如果不声明状态标识,默认 stash@{0} stash@{0} 标识最新保存的数据 其实由 pop 指令可以大概猜测 git 使用堆栈一样的结构来进行 stash, 先进的在下面。
master: 主分支,主要用来版本发布。
**develop:**日常开发分支,该分支正常保存了开发的最新代码。
**feature:**具体的功能开发分支,只与 develop 分支交互。
**release:**release 分支可以认为是 master 分支的未测试版。比如说某一期的功能全部开发完成,那么就将 develop 分支合并到 release 分支,测试没有问题并且到了发布日期就合并到 master 分支,进行发布。
**hotfix:**线上 bug 修复分支,必须合并到 master 和 develop
查看远程分支
git branch -a
checkout 远程分支
git checkout -b dev origin/dev git pull
建立本地分支和远程分支的关联
git branch --set-upstream branch-name origin/branch-name
本地创建和远程分支对应的分支
git checkout -b branch-name origin/branch-name
本地和远程分支的名称最好一致
git pull相当于是git fetch + git merge
创建一个标签
git tag v1.0
默认最新提交的
指定id创建一个标签
git tag v1.0 f52c633
添加说明创建
git tag -a v0.1 -m "version 0.1 released" 1094adb
显示所有 tag
git tag
显示详细信息
git show tagName
删除标签
git tag -d v1.0
push 到远程
git push origin v1.1
删除远程标签 1.删除本地
git tag -d v1.1
2.推送删除
git push origin
/tags/v1.1
忽略某些文件时,需要编写.gitignore;
.gitignore文件本身要放到版本库里,并且可以对.gitignore做版本管理!
使用 st 代替 status
git config --global alias.st status
当然还有别的命令可以简写,很多人都用co表示checkout,ci表示commit,br表示branch:
git remote add upstream https://github.com/drami025/git-game.git
本文作者:Yui_HTT
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!