Channy's blog

//Description: Git常用命令和进阶命令笔记

//Create Date: 2019-08-13 09:03:43

//Author: channy

[toc]

Git常用命令

其它笔记

$ git config –global user.name “qanny”

$ git config –global user.email qanny@example.com

git init 初始化

git add dir1

git add f1 f2

git add -A 添加所有文件

git add . 添加新文件和编辑过的文件不包括删除的文件

git add -u 添加编辑或者删除的文件不包括新添加的文件

git add –update :/ 添加修改

git rm 删除文件

git status 查看状态

git reset 撤销保存还未提交的状态

git commit -m “information about remand” 提交修改

git log –stat –summary 查看历史日志

q 退出当前历史记录

cd 到文件目录下 git log –pretty=oneline 文件名 查看某个文件的修改记录

git show 版本号 显示具体的修改

git branch mybranch 创建分支

git checkout mybranch

git checkout -b mybranch 切换到分支

创建并切换到新分支

git clone -b 克隆某个分支

git clone http://github.com/*.git 克隆整个分支

git 克隆时默认只克隆master分支,需要转到其它分支上才能看到其它文件

git reset *(版本号) 回退到某个版本

git reset –hard 回退到上个版本(取消本地未提交的修改)

git checkout – main.cpp 恢复一个文件main.cpp

git diff 获取两个版本之间的变更比较

如:git diff b45ba47d1b297217e3ec6a3ab0f61716a8d6ecbc c244d0bf06d56ec86aaedeefa5dcd84dd9febc60

一般来说,通过 hash 串的前 4~6 位就可以区分,所示可以简写为: git diff b45b 355e

提交时忽略特定的文件:修改.git/info/exclude

. 忽略所有文件

!*.h 只保留.h文件

!*.cpp 和.cpp文件

!CMakeLists.txt cmake文件也要

提交到github上: { 前提准备:

  1. ssh-keygen -t rsa -C “*@gmail.com”
  2. ssh-agent -s
  3. clip > ~/.ssh/id_rsa.pub
  4. 在github上设置–SSH keys–Add SSH key– }
  5. github上创建仓库,得地址 1.5 克隆到本地:git clone git@github.com:qanny/*.git
  6. git Bash 转到目录
  7. 生成本地git (或github上已有库,本地没有,新加) +. git config branch.master.remote origin +. git config branch.master.merge refs/heads/master +. git pull 3.5 git remote rm origin
  8. git remote add origin https://github.com/qanny/*.git
  9. git push -u origin master

常见问题解决方法:

git push -u origin master 时出现 Could not resolve hostname github.com

解决方法:ssh -T git@github.com

再次提交即可

git push -u origin master 时出现 error:src refspec master does not match any

目录中没有文件,空目录是不能提交上去的

git clone 到一半报错误

解决方案:文件大小限制,可能有文件超过了限定大小,默认50M, 可改成500M

git config http.postBuffer 524288000

Git进阶命令

以已有一坨有分支的代码为基础。

$ git checkout xxx

切换到xxx分支

$ git checkout -b dev

新建dev分支

git branch -a
* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
//dev
ls
README.md		file_init.cpp		file_new_branch_dev.cpp
git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

ls
README.md	a.out		file_init.cpp

此时master分支上是没有file_new_branch_dev.cpp的,因为该文件在dev分支上

$ git merge dev

合并dev分支到当前分支上

git merge dev
Updating 38eb274..29f6536
Fast-forward
 a.out                   | Bin 23032 -> 0 bytes
 file_new_branch_dev.cpp |   5 +++++
 2 files changed, 5 insertions(+)
 delete mode 100755 a.out
 create mode 100644 file_new_branch_dev.cpp

没有冲突的话现在dev是master的子集了

$ git diff master dev

查看两个分支的差异

git diff master dev
git diff dev master
diff --git a/file_init.cpp b/file_init.cpp
index d700354..c785fe0 100644
--- a/file_init.cpp
+++ b/file_init.cpp
@@ -3,7 +3,7 @@
 int main() {
        int res = 0;
        std::cout << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << std::endl;
-       std::cout << "add dev optput" << std::endl;
+       std::cout << "add master output" << std::endl;
        return res * 2;
 }

此时merge的话会出现冲突

git merge dev
Auto-merging file_init.cpp
CONFLICT (content): Merge conflict in file_init.cpp
Automatic merge failed; fix conflicts and then commit the result.

并且冲突文件上还有合并过的痕迹

<<<<<<< HEAD
        std::cout << "add master output" << std::endl;
=======
        std::cout << "add dev optput" << std::endl;
>>>>>>> dev
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

所以,git命令行修改冲突不方便-_-

git push只push当前分支,其它分支上的修改是不会push的。。。

$ git push –set-upstream origin dev

本地创建的branch在push时需要增加参数

git push
fatal: The current branch dev has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin dev

$ git fatch

git fatch + git merge = git pull

back