1.git本地仓库初始化:init
git init
# 创建 .git隐藏目录
2.设置签名:config
形式:
①用户名
②email地址
作用:
区分不同开发人员的身份
命令:
(1)项目级别/仓库级别:仅在当前仓库范围内有效
git config user.name li
git config user.email li@qq.com
签名保存位置:.git/config
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ git config user.name li
$ git config user.email 1466722091@qq.com
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ cd .git/
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo/.git (GIT_DIR!)
$ ls
config description HEAD hooks/ info/ objects/ refs/
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo/.git (GIT_DIR!)
$ cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[user]
name = li
email = 1466722091@qq.com
(2)系统用户级别:登录当前操作系统的用户范围
git config --global user.name li
git config --global user.email li@qq.com
# 信息保存位置:家目录下的 .gitconfig 文件
admin@Li-Honor MINGW64 ~
$ cat ~/.gitconfig
[user]
name = li
email = 1466722091@qq.com
(3)级别优先级:
就近原则:项目级别>系统级别,两者都存在时采用项目级别的签名。
如果只有系统用户级别的签名,就以系统用户级别的签名为准。
3.查看工作区、暂存区的状态:status
命令:git status
# 效果:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.md
nothing added to commit but untracked files present (use "git add" to track)
4.添加文件到暂存区以及移除暂存区的文件:add
命令:git add ,git rm
# 添加
$ git add readme.md
warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory.
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.md
# 移除:
$ git rm --cached readme.md
rm 'readme.md'
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme.md
nothing added to commit but untracked files present (use "git add" to track)
5.提交文件到本地仓库:commit
命令: git commit -m ”message“ [file]
$ git commit readme.md
warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory.
[master (root-commit) caf92e1] init readme.md
1 file changed, 3 insertions(+)
create mode 100644 readme.md
# 会提示加入提交日志
6.修改文件后的提示信息
$ vim readme.md
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ 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: readme.md
no changes added to commit (use "git add" and/or "git commit -a")
再次添加到暂存区并提交:
$ git add readme.md
warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory.
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.md
# -m参数:提交的详细日志
$ git commit -m "modify readme.md" readme.md
warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory.
[master 146624c] modify readme.md
1 file changed, 1 insertion(+)
7.查看日志:log & reflog
$ git log
commit 146624ced3cbb9dbea0858f5d09a6b5e2de48298 (HEAD -> master)
Author: li <1466722091@qq.com>
Date: Wed Jun 6 19:37:25 2018 +0800
modify readme.md
commit caf92e19a3f8d3bc15763216c77a712ab057da0b
Author: li <1466722091@qq.com>
Date: Wed Jun 6 19:28:12 2018 +0800
init readme.md
# 多屏日志时翻页:
# 空格:向下翻页
# b:向上翻页
# q:推出
其他命令:
$ git log --pretty=oneline
146624ced3cbb9dbea0858f5d09a6b5e2de48298 (HEAD -> master) modify readme.md
caf92e19a3f8d3bc15763216c77a712ab057da0b init readme.md
$ git log --oneline
146624c (HEAD -> master) modify readme.md
caf92e1 init readme.md
$ git reflog
146624c (HEAD -> master) HEAD@{0}: commit: modify readme.md
caf92e1 HEAD@{1}: commit (initial): init readme.md
# 会显示移动到某个版本的步数
8.版本前进与回退:reset
8.1 基于索引值
命令:git reset –hard <索引值>
(1)回退
$ git reflog
3592cb0 (HEAD -> master) HEAD@{0}: commit: insert eeeeee #现在处于的版本
146624c HEAD@{1}: commit: modify readme.md
caf92e1 HEAD@{2}: commit (initial): init readme.md
$ cat readme.md
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeeee #文件内容
$ git reset --hard 146624c
HEAD is now at 146624c modify readme.md
$ git reflog
146624c (HEAD -> master) HEAD@{0}: reset: moving to 146624c # 指针移动
3592cb0 HEAD@{1}: commit: insert eeeeee
146624c (HEAD -> master) HEAD@{2}: commit: modify readme.md # 回退到的版本
caf92e1 HEAD@{3}: commit (initial): init readme.md
$ cat readme.md
aaaaaaa
bbbbbbb
ccccccc #文件内容
(2)前进:与回退类似
8.2 ^符号(只能后退)
命令:git reset –hard HEAD^
注:^的个数表示回退的步数
8.3 ~符号(只能回退)
命令:git reset –hard HEAD~3
注:数字3表示回退3步
8.4 reset参数对比
(1)–soft: 仅仅再本地库移动HEAD指针
(2)–mixed:在本地库移动HEAD指针 + 重置暂存区
(3)–hard:在本地库移动HEAD指针 + 重置暂存区 + 重置工作区
示例:
$ git log --oneline
3592cb0 (HEAD -> master) insert eeeeee
146624c modify readme.md
caf92e1 init readme.md
$ git reset --soft 146624c # 回退
$ cat readme.md # 工作区的文件并不改变
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeeee
$ git status # 暂存区的状态是最新提交前的,而本地库回退到了上一个版本
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.md
8.5 删除文件并找回
事实上就时回退到被删除的文件存在的那个版本。
操作:
①删除操作已经提交到本地库:git reset --hard [历史版本]
②删除操作没有提交到本地库:git reset --hard HEAD # 指针指向当前版本
示例:
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ rm readme.md # 删除文件
$ ll # 文件不存在了
total 0
$ git status # 未提交
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git reset --hard HEAD # 移动指针即可回复文件
HEAD is now at 3592cb0 insert eeeeee
$ ll # 文件已经回复
total 1
-rw-r--r-- 1 admin 197609 45 6月 6 20:41 readme.md
8.6文件比较:diff
8.6.1 将工作区和暂存区的文件进行比较
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ vim readme.md
$ git diff readme.md
diff --git a/readme.md b/readme.md
index 18fc713..e8825ef 100644
--- a/readme.md
+++ b/readme.md
@@ -3,3 +3,4 @@ bbbbbbb
ccccccc
ddddddd
eeeeeee
+fffffff
8.6.2 将工作区的文件和本地库历史记录中的某个版本的文件进行比较
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ git diff HEAD^ readme.md # 与前一个版本进行比较
diff --git a/readme.md b/readme.md
index bac269f..e8825ef 100644
--- a/readme.md
+++ b/readme.md
@@ -2,3 +2,5 @@ aaaaaaa
bbbbbbb
ccccccc
ddddddd
+eeeeeee
+fffffff
8.6.3 不带文件名进行比较
例如:git diff HEAD
9.分支操作
9.1 创建分支:git branch [分支名]
9.2 查看分支:git branch -v
9.3 切换分支:git checkout [分支名]
$ git branch -v
* master 3592cb0 insert eeeeee
$ git branch dev # 创建分支
$ git branch -v # 查看分支
dev 3592cb0 insert eeeeee
* master 3592cb0 insert eeeeee # 处于master分支
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ git checkout dev # 切换分支
Switched to branch 'dev'
M readme.md
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev)
$ git branch -v
* dev 3592cb0 insert eeeeee # 处于dev分支
master 3592cb0 insert eeeeee
9.4 合并分支
步骤:
①切换到接受修改的分支上
②执行merge命令
示例:
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev)
$ vim readme.md # 修改文件
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev)
$ 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 add readme.md
$ git commit -m "dev insert ggggg" readme.md # dev 分支提交
[dev 7432504] dev insert ggggg
1 file changed, 2 insertions(+)
$ git branch -v
* dev 7432504 dev insert ggggg
master 3592cb0 insert eeeeee
$ git checkout master # 切换到master分支
Switched to branch 'master'
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (master)
$ git branch -v
dev 7432504 dev insert ggggg
* master 3592cb0 insert eeeeee
$ git merge dev # 执行合并操作
Updating 3592cb0..7432504
Fast-forward
readme.md | 2 ++
1 file changed, 2 insertions(+)
$ cat readme.md # 合并成功
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeeee
fffffff
dev:gggggg
9.5 解决冲突
$ git merge master # 合并时产生冲突
Auto-merging readme.md
CONFLICT (content): Merge conflict in readme.md
Automatic merge failed; fix conflicts and then commit the result.
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev|MERGING) # MERGING标志
$ vim readme.md
手动编辑文件解决冲突
继续合并:
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev|MERGING)
$ git status # 查看状态
On branch dev
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
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")
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev|MERGING)
$ git add readme.md
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev|MERGING)
$ git status
On branch dev
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: readme.md
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev|MERGING)
$ git commit -m "solve conflict" # 注意:提交不能带文件名
[dev f5ad8c1] solve conflict
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev)
$ git status
On branch dev
nothing to commit, working tree clean
admin@Li-Honor MINGW64 /d/developer/VCS/gitRepo (dev)
$ cat readme.md # 显示文件合并成功
aaaaaaa
bbbbbbb
ccccccc
ddddddd
eeeeeee edit by dev
eeeeeee edit by master
fffffff
dev:gggggg
步骤
①编辑文件,删除特殊符号
②修改文件到理想状态,保存退出
③git add [文件名]
④git commit -m “message”
注意:commit 后不带文件名
10.与GitHub远程仓库交互
10.1 创建远程分支别名
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ git remote add origin https://github.com/hundanLi/repo1.git
$ git remote -v
origin https://github.com/hundanLi/repo1.git (fetch)
origin https://github.com/hundanLi/repo1.git (push)
10.2 将本地仓库内容推到远程仓库
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 221.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/hundanLi/repo1.git
* [new branch] master -> master
10.3 将远程库克隆到本地仓库
admin@Li-Honor MINGW64 /d/developer/VCS/repo2
$ git clone https://github.com/hundanLi/repo1.git
Cloning into 'repo1'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ ll
total 0
drwxr-xr-x 1 admin 197609 0 6月 7 00:18 repo1/
效果:
①完整地将远程仓库下载到本地
②创建origin远程地址别名
③初始化本地仓库
10.4 邀请成员加入团队
GitHub操作流程:远程仓库 ==> settings ==> Collaborators ==> Add collaborators
然后等待对方的回应。
10.5 从远程库拉取最新内容
命令:pull = fetch + merge
$ git fetch origin # fetch 操作
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/hundanLi/repo1
2ccbb7a..2f5c9fd master -> origin/master
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ git checkout origin/master # 切换到fetch出来的分支
Note: checking out 'origin/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 2f5c9fd readme.md edit by hundanLZL
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 ((2f5c9fd...))
$ cat readme.md # 查看远程库的修改内容
test with github
edit by hundanLZL
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 ((2f5c9fd...))
$ git checkout master
Previous HEAD position was 2f5c9fd readme.md edit by hundanLZL
Switched to branch 'master'
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ cat readme.md # merge之前,本地库并无改变
test with github
# merge 以后把远程库的最新内容合并到本地库
$ git merge origin/master
Updating 2ccbb7a..2f5c9fd
Fast-forward
readme.md | 1 +
1 file changed, 1 insertion(+)
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ cat readme.md
test with github
edit by hundanLZL
10.6解决冲突
要点:
一、如果不是基于GitHub远程库最新版所做的修改,不能push,必须先pull到本地库。
二、拉取下来以后,如果进入冲突状态,手动编辑到想要的状态即可。
示例:
$ git push origin master # push失败的情况
To https://github.com/hundanLi/repo1.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/hundanLi/repo1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master)
$ git pull origin # 拉取远程库的最新版
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/hundanLi/repo1
2f5c9fd..deffc8c master -> origin/master
Auto-merging readme.md
CONFLICT (content): Merge conflict in readme.md
Automatic merge failed; fix conflicts and then commit the result.
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master|MERGING)
$ vim readme.md # 解决冲突
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master|MERGING)
$ git add readme.md
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master|MERGING)
$ git commit -m "conflicts solved" readme.md # 注意解决冲突后提及不能加文件名
fatal: cannot do a partial commit during a merge.
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master|MERGING)
$ git commit -m "conflicts solved" # 提交成功
[master 27cdc19] conflicts solved
admin@Li-Honor MINGW64 /d/developer/VCS/repo2/repo1 (master)
$ git push origin # push 成功
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 527 bytes | 527.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
10.7 团队以外的人参与开发
流程:fork ==> 开发完成后 ==> pull request
项目管理员:在仓库的pull Request中查看他人的pull request审核后即可执行merge操作。
10.8 生成ssh key
admin@Li-Honor MINGW64 ~
$ rm -r .ssh/ # 删除原来的.ssh 目录
admin@Li-Honor MINGW64 ~
$ ssh-keygen -t rsa -C 1466722091@qq.com # 用邮箱生成ssh keys
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/14667/.ssh/id_rsa):
Created directory '/c/Users/14667/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/14667/.ssh/id_rsa.
Your public key has been saved in /c/Users/14667/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:QmVSgnSIhBMreLTm4fby0C4RZaK9E2VEcS/miDqsgu4 1466722091@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|.+o==+=.+ |
|+oo.Bo * |
|++=* + . |
|o=+o = . |
| =+. o S |
|.o+o . |
|+.ooo |
|+..= |
|=E .o |
+----[SHA256]-----+
admin@Li-Honor MINGW64 ~
$ cd .ssh/
admin@Li-Honor MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub
admin@Li-Honor MINGW64 ~/.ssh
$ cat id_rsa.pub # 将其粘贴到GitHub的SSH keys中
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8NUcB7Yuo5HZi07zvtcia1IU9e6+09xCUCDaBwXDQb7fgJ/oqNw36zBtrCOXizsTCzgosSE6qTxLPo+y4wX0utdMldNjzJZcQdzYIecUN1xHoiRR3C0Gm2Qv/PYNJOQ7IRwJ1SOBRrGUNWm6YUitNgVrSh8Wtg8ELtT6mJna5pVQOAvORhMVxn+PbvMKphhGxk9T18AfQMYa5douj3VLZAQkw65LmYKQc+YLDpzghzVsbUGdKHxK0/M1sePTAvuFoxSHIGVz22spw1lITc7y/UggQbjUMQZ6/AO/cSe6Haza5m54wDTrJBQz5VIWQIjeFGjPIlJn1lWEGKpCM5iNd 1466722091@qq.com
添加ssh远程库别名:
$ git remote -v
origin https://github.com/hundanLi/repo1.git (fetch)
origin https://github.com/hundanLi/repo1.git (push)
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ git remote add origin_ssh git@github.com:hundanLi/repo1.git # 添加ssh远程库别名
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ git remote -v # 添加成功
origin https://github.com/hundanLi/repo1.git (fetch)
origin https://github.com/hundanLi/repo1.git (push)
origin_ssh git@github.com:hundanLi/repo1.git (fetch)
origin_ssh git@github.com:hundanLi/repo1.git (push)
admin@Li-Honor MINGW64 /d/developer/VCS/repo1 (master)
$ git push origin_ssh master # push 就不用再输入密码
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Everything up-to-date
11 eclipse操作
11.1 操作流程
一、项目==>右键==>Team==>Git==>share Project ==> 勾选use or create...
二、点击create Repository ==> Finish
完成后达到如下效果:
11.2 Eclipse中 git追踪时应该忽略的文件
11.2.1忽略文件清单
.classpath 文件
.project 文件
.settings 文件
target目录下所有文件
11.2.2 操作流程
①在家目录下创建java.gitignore文件(参考链接:https://github.com/github/gitignore/blob/master/Java.gitignore)
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# eclipse忽略文件:
*.classpath
*.project
*.settings
target
②修改家目录下的.gitconfig文件引入上述的java.gitignore文件
[user]
name = li
email = 1466722091@qq.com
[core]
excludesfile=C:/Users/14667/java.gitignore
注:文件路径用linux风格的正斜线:“/”,不能使用“\”
最终效果:
只剩下与项目代码相关的文件被git追踪:
11.2.3把未追踪的文件添加到暂存区和提交
Add
方法一:右键项目 ==> Team ==> Add to Index
方法二:右键项目 ==> Team ==> Commit(快捷键 Ctrl + shift + 3) 调出Commit窗口把unstaged changes 的文件拖拽到staged changes窗口,效果如下图:
Commit
在上面图中编辑Commit Message后点击Commit即可。
11.3 推送到远程库
右键项目 ==> Team ==> remote ==> push
11.4 STS从GitHub克隆工程
一、Import from git
二、clone URI
三、将远程库地址https://github.com/hundanLi/testGit.git粘贴到URI栏
Next:
四、以general project方式导入eclipse
五、右键项目 ==> Configure ==> Convert to maven project:转换成maven工程
- pull远程仓库的最新修改产生冲突及解决办法
12.1冲突
12.2 解决冲突的渐变方法
流程:右键冲突文件 ==> Team ==> Merge Tool
对比两个版本的文件差异并手动合并保存
13.新建分支开发新功能或者修复bug
13.1 新建分支的操作流程:右键项目 ==> Team ==> Switch To ==> New Branch
将开发好的新分支推送到远程库给项目主管审核
远程库多了一个hot_fix分支:
13.2 项目主管从远程库拉取到本地审核与合并
(1)拉取操作
①右键项目 ==> Team ==> Pull
②右键项目 ==> Team ==> Switch To ==> other ==> origin/hot_fix ==> Check Out ==> Check Out as New Local Branch
结果如下:
(2)合并到本地的master分支
①切换到master分支:右键项目 ==> Team ==> Switch To ==> master
②合并:右键项目 ==> Team ==> Merge ==> Local/hot_fix
③push到远程库
远程库的master分支得到更新: