1.git基本操作:
$ git status #查看当前状态,常用命令#
2.显示文件日志
- $ git log #显示所有文件的日志信息#
- $ git log -p #显示所有文件提交前后的差别#
- $ git log file #显示指定文件的日志信息#
- $ git log -p file #指定文件的提交前后的差别#
3.分支操作
1.查看分支
$ git branch #显示分支列名,并确认当前所在的分支#
* master #*表示当前的分支#
2.创建分支
$ git checkout -b feature_A #创建分支 feature_A#
Switched to a new branch ‘feature_A’ #并指定当前为feature_A分支#
M “\347\275\221\344\270\212\344\271\246\345\272\227.txt”
2.1创建分支的分布操作
- 2.1.1 $ git branch feature_A -创建feature_A分支-
2.1.2 $ git checkout feature_A #切换到feature_A分支#
$ git checkout - #表示返回到上一个分支#
Switched to branch ‘master’
M “\347\275\221\344\270\212\344\271\246\345\272\227.txt”
Your branch is up-to-date with ‘origin/master’.3.在分支中写入数据
$ vim README.md #编辑README.md文件#
$ git add README.md #向git中添加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 commit -m “Add feature_A” #提交README.md文件#
[feature_A 92c854c] Add feature_A
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git log -p README.md #指定文件的提交前后的差别信息#
commit 92c854c2a7f5ad7d8e99f328bdb2f65f939db74b (HEAD -> feature_A)
Author: ting163 <894139169@qq.com>
Date: Thu Sep 14 01:28:07 2017 +0800
Add feature_A
diff –git a/README.md b/README.md
new file mode 100644
index 0000000..f38d6fd
— /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+this is branch feature_A
$ git push -u https://github.com/ting163/ebookstore(提交远端的仓库) feature_A
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/ting163/ebookstore
* [new branch] feature_A -> feature_A
Branch feature_A set up to track remote branch feature_A from https://github.com/ting163/ebookstore.
4. git merge 合并分支
4.1 $ git checkout - #将分支切换到master#
Switched to branch ‘master’
M “\347\275\221\344\270\212\344\271\246\345\272\227.txt”
Your branch is up-to-date with ‘origin/master’.
4.2 $ git merge – –no-ff feature_A #合并操作,feature_A的内容就合并到master分支中#
Merge made by the ‘recursive’ strategy.
README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 README.md
4.3 $ git log –graph #常用命令,以图形的方式查看分支#
1. commit caa2439657212a20871eece4641392e90bf18641 (HEAD -> master)
|\ Merge: 2eb363e 92c854c
| | Author: ting163 <894139169@qq.com>
| | Date: Thu Sep 14 01:37:52 2017 +0800
| |
| | Merge branch ‘feature_A’
| |
| * commit 92c854c2a7f5ad7d8e99f328bdb2f65f939db74b (feature_A)
|/ Author: ting163 <894139169@qq.com>
| Date: Thu Sep 14 01:28:07 2017 +0800
|
| Add feature_A
|
2. commit 2eb363e23303412d91e0b9cbdabe4e2bb3b8a485 (origin/master, origin/HEAD)
Author: ting163 <894139169@qq.com>
Date: Thu Sep 14 00:25:33 2017 +0800
Initial commit
4.4 git push #一定要push ,网络端分支才能合并到master分支中#
4.推送到远程仓库
1.git remote add ———添加远程仓库
$ git remote add origin https://github.com/ting163/ebookstore/test01.git(远程仓库)
2.git push ————–推送到远程仓库中
administer@administer-PC MINGW64 ~/Desktop/hongwing (master)
$ git push -u origin master #必须与上面的分支对应#
Everything up-to-date
Branch master set up to track remote branch master from origin.
3.问题
administer@administer-PC MINGW64 ~/Desktop/hongwing (master)
$ git remote add origin git@github.com:ting163/hongwing.git
fatal: remote origin already exists. #origin已经存在#
解决:
administer@administer-PC MINGW64 ~/Desktop/hongwing (master) #将其删掉#
$ git remote rm origin
4.问题
Merging is not possible because you have unmerged files.
Exiting because of an unresolved conflict.
(use “git add …” to update what will be committed)
(use “git checkout – …” to discard changes in working directory)
解决:git add .
5.问题
Git Merge Failed: You have not concluded your merge (MERGE_HEAD exists).
本地有修改和提交,如何强制用远程的库更新。重点内容
出现这种情况一般是git本地有commit,但是不能更新,也不能cleanup,导致无法push到服务器。
使用git pull -f 强制更新,仍然提示 You have not concluded your merge. (MERGE_HEAD exists)。
这种情况,可以做好备份,放弃本地修改,用远程内容reset本地目录,当然新建目录重新clone也可以。
以下是使用reset命令使用远程内容强制更新本地目录,
git fetch 下载远程的库的内容,不做任何的合并,git reset 把HEAD指向刚刚下载的最新的版本。
注意,这样操作,之前所做的修改会被覆盖!!!谨慎使用!
git fetch - -all
git reset - -hard origin/master(根据本地版本)