作为程序员一定要保持良好的睡眠,才能好编程

git工作中常用命令 全网最全操作 Git 命令,快速手册,人人必备

发布时间:2020-01-30






解决冲突需要在编辑器中解决


3.5冲突是如何表示的

当产生合并冲突时,该部分会以<<<<<<<, =======和 >>>>>>>表示。在=======之前的部分是当前分支这边的情况,在=======之后的部分是对方分支的情况。


3.6如何解决冲突

在看到冲突以后,你可以选择以下两种方式:

决定不合并。这时,唯一要做的就是重置index到HEAD节点。git merge --abort用于这种情况。

解决冲突。Git会标记冲突的地方,解决完冲突的地方后使用git add加入到index中,然后使用git commit产生合并节点。

你可以用以下工具来解决冲突:

git reset

gitgoback.jpg



conflicts.jpg

git add add file 

git commit -m '增加file'

git push






合并完分支代码后,只是在本地,需要push 到远程 ,这样其他分支在拉去主干代码才能获取到最新代码

git.jpg


这张图的意思是:

合并主干代码

1、







生成rsa文件 ,免密码上传文件

https://blog.csdn.net/freelifewe/article/details/79666995



同一个git中使用多个远程仓库

https://www.jianshu.com/p/4cd46619b3a5

https://blog.csdn.net/mengzuchao/article/details/80489864



 

…or create a new repository on the command line



echo "# testgit" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/songyongzhan/testgit.git

git push -u origin master





…or push an existing repository from the command line

git remote add origin https://github.com/songyongzhan/testgit.git

git push -u origin master



git.exe clone --progress --branch fetch0514 -v "https://github.com/songyongzhan/test.git" "C:\Users\Administrator\Desktop\test\git\test4"





git.exe checkout hotfix0517 --


Your branch is up to date with 'origin/master'.

Switched to branch 'hotfix0517'

 

git.exe push --progress "origin" hotfix0517:master


Enumerating objects: 4, done.

Counting objects: 100% (4/4), done.

Delta compression using up to 4 threads

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 469 bytes | 156.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/songyongzhan/test.git

460fb93..aa988ab  hotfix0517 -> mastergit.exe clone --progress --branch fetch0514 -v "https://github.com/songyongzhan/test.git" "C:\Users\Administrator\Desktop\test\git\test4"


 

git.exe checkout hotfix0517 --


Your branch is up to date with 'origin/master'.

Switched to branch 'hotfix0517'


//拉去远程分支内容


git.exe clone --progress --branch fetch_orders_20190502 -v "https://github.com/songyongzhan/test.git" "test4"


//推送分支到指定目录


git.exe push --progress "origin" fetch_orders_20190502:fetch_orders_20190502


//从远程分支拉取内容

$ git pull origin fetch_orders_20190502



git status


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git status

On branch master


No commits yet


Changes to be committed:

  (use "git rm --cached <file>..." to unstage)


        new file:   README.md

        new file:   index.php



DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)


删除暂存区的index.php 文件

$ git rm --cached index.php

rm 'index.php'



#如果有改动,未添加到git 没有git commit 的一个提示信息

$ git status

On branch pub

Your branch is up to date with 'origin/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:   v.php


no changes added to commit (use "git add" and/or "git commit -a")





查看当前git的状态

$ git status

On branch master


No commits yet


Changes to be committed:

  (use "git rm --cached <file>..." to unstage)


        new file:   README.md


Untracked files: 未被追踪的文件,只要是新增的文件都会在这里。 就是 未git add的文件

  (use "git add <file>..." to include in what will be committed)


        index.php

        v.php



==============================================================

$ git add readme.md


$ git commit -m '//提交readme.md文件'


$ git push -u origin master

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

Counting objects: 3, done.

Writing objects: 100% (3/3), 240 bytes | 240.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To https://github.com/songyongzhan/testgit.git

 * [new branch]      master -> master

Branch 'master' set up to track remote branch 'master' from 'origin'.

==============================================================





==============================================================

$ git remote -v  和 git remote -verbose 是同样的效果

origin  https://github.com/songyongzhan/testgit.git (fetch)

origin  https://github.com/songyongzhan/testgit.git (push)


查看当前库详情

$ git remote show origin

* remote origin

  Fetch URL: https://github.com/songyongzhan/testgit.git

  Push  URL: https://github.com/songyongzhan/testgit.git

  HEAD branch: (unknown)


查看当前的远程库

$ git remote

origin


Git 默认使用这个origin名字来标识你所克隆的原始仓库




git remote add 


$ git remote -v

origin  https://github.com/songyongzhan/testgit.git (fetch)

origin  https://github.com/songyongzhan/testgit.git (push)


给testgit.git 添加pb仓库

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git remote add pb https://github.com/songyongzhan/testgit.git



删除远程仓库

git remote rm 



修改远程仓库名称 pb 到 www

$ git remote rename pb www

查看已经修改的名称

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git remote -v

origin  https://github.com/songyongzhan/testgit.git (fetch)

origin  https://github.com/songyongzhan/testgit.git (push)

www     https://github.com/songyongzhan/testgit.git (fetch)

www     https://github.com/songyongzhan/testgit.git (push)





git fetch <远程名>


git merge origin/master


==============================================================




===========创建分支并推送到远程============



git clone git地址 项目名


git branch feature/20190510_songyz_plan_tags


git push origin feature/20190510_songyz_plan_tags


git checkout feature/20190510_songyz_plan_tags


git branch 


===========创建分支并推送到远程============



#列出当前分支

$ git branch

* master


#创建 gitbugs-20190417 分支

$ git branch gitbugs-20190417


#查看远程分支

git branch -r


#查看本地分支

git branch -a


#创建分支

git branch [branch name]


#查看所有分支

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git branch

  gitbugs-20190417


#我们已经清楚看到 gitbugs-20190417


#切换分支

git checkout gitbugs-20190417


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git checkout gitbugs-20190417

Switched to branch 'gitbugs-20190417'


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (gitbugs-20190417)

$ git branch

* gitbugs-20190417

  master


#提交代码到新分支 切记这里的分支一定要写的分支名称

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (gitbugs-20190417)

$ git push -u origin gitbugs-20190417  #这里一定要是新分支的名称

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

Counting objects: 3, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 351 bytes | 351.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0)

remote:

remote: Create a pull request for 'gitbugs-20190417' on GitHub by visiting:

remote:      https://github.com/songyongzhan/testgit/pull/new/gitbugs-20190417

remote:

To https://github.com/songyongzhan/testgit.git

 * [new branch]      gitbugs-20190417 -> gitbugs-20190417

Branch 'gitbugs-20190417' set up to track remote branch 'gitbugs-20190417' from 'origin'.


#将新分支推送到github

命令如下:

git push origin [branch name]



#分支删除

git branch -d gitbugs-20190417


#分支合并

一般都是向主干分支合并

git merge gitbugs-20190417 (将gitbugs-20190417代码合并到master或者其他分支上)

当前分支需要是要合并到的分支。




#从远程服务器上主干上获取一个分支  使用git checkout 命令创建一个新的分支

#创建+切换分支

git checkout -b newBranch origin/master 

相当于这两个步骤:

git branch [branch name]

git checkout [branch name]

从远程服务器获取数(在origin/master的基础上)据并建立一个新的分支取名 newBranch。 


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git checkout  -b pub origin/master

Switched to a new branch 'pub'

Branch 'pub' set up to track remote branch 'master' from 'origin'.


#创建完成分支后,立马就切换到pub了

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (pub)

$ git branch

  master

* pub



#删除本地分支

git branch -d [branch name]


例:

$ git branch -d pub

Deleted branch pub (was 5854528).



#删除github远程分支


$ git push -u origin :pub

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

To https://github.com/songyongzhan/testgit.git

 - [deleted]         pub


#分支名前的冒号代表删除

git push origin :pub


这样就删除了远程分支





假设我们在newsbranch 这个分支上,直接使用 git add . 、git commit -m '' 、git push 

这样的三条命令,会把代码提交到当前所在的分支上。即 newsbranch


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (newsbranch)

$ git push

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

Counting objects: 3, done.

Delta compression using up to 8 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 370 bytes | 370.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0)

To https://github.com/songyongzhan/testgit.git

   42efab4..e1a91ed  newsbranch -> newsbranch



分支合并

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (newsbranch)

$ git branch

  master

* newsbranch


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (newsbranch)

$ git checkout master

Switched to branch 'master'

Your branch is up to date with 'origin/master'.


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git merge newsbranch

Updating 9ca5dd9..e1a91ed

Fast-forward

 index.php | 1 +

 v.php     | 2 ++

 2 files changed, 3 insertions(+)


 

================tag===================

git tag的用法 git命令之git tag 给当前分支打标签


我们常常在代码封板时,使用git 创建一个tag ,这样一个不可修改的历史代码版本就像被我们封存起来一样,不论是运维发布拉取,或者以后的代码版本管理,都是十分方便的



git的tag功能

git 下打标签其实有2种情况

  

打标签

git标签分为两种类型:轻量标签和附注标签。轻量标签是指向提交对象的引用,附注标签则是仓库中的一个独立对象。建议使用附注标签。


# 创建轻量标签

$ git tag v0.1.2-light

 

# 创建附注标签

$ git tag -a v0.1.2 -m “0.1.2版本”



给指定的commit打标签

打标签不必要在head之上,也可在之前的版本上打,这需要你知道某个提交对象的校验和(通过git log获取)。

# 补打标签

$ git tag -a v0.1.1 9fbc3d0

 


线上创建版本

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git tag -a 1.0 -m 'release 1.0'


DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git tag

1.0

 

#上传到服务器

通常的git push不会将标签对象提交到git服务器,我们需要进行显式的操作:

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

 

设置自动跟踪

git push --set-upstream origin feature/20190628-songyz-addGlobalId



$ git push origin 1.0 # 将1.0标签提交到git服务器


$ git push origin a

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

Total 0 (delta 0), reused 0 (delta 0)

remote:

remote: Create a pull request for 'a' on GitHub by visiting:

remote:      https://github.com/songyongzhan/testgit/pull/new/a

remote:

To https://github.com/songyongzhan/testgit.git

 * [new branch]      a -> a




#删除本地标签号

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git tag -d 1.0

Deleted tag '1.0' (was 67980df)



到这一步我们只是删除了本地 1.0的版本,可是线上1.0的版本还是存在,如何办?

这时我们可以推送的空的同名版本到线下,达到删除线上版本的目标:


git push origin :refs/tags/1.0


这样就删除github的标签库

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit (master)

$ git push origin :refs/tags/1.0

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

To https://github.com/songyongzhan/testgit.git

 - [deleted]         1.0



如何获取远程版本?


git fetch origin tag V1.2

这样我们可以精准拉取指定的某一个版本.





切换到标签

与切换分支命令相同,用git checkout [tagname]

查看标签信息

用git show命令可以查看标签的版本信息:

$ git show v0.1.2

 

 


$ git push

fatal: The current branch fetch1620 has no upstream branch.

To push the current branch and set the remote as upstream, use


    git push --set-upstream origin fetch1620




设置分支追踪分支

DELL@DESKTOP-HEOND63 MINGW64 /e/testgit/test4 (fetch1620)

$ git push --set-upstream origin fetch1620

fatal: HttpRequestException encountered.

   ▒▒▒▒▒▒▒▒ʱ▒▒▒▒

Username for 'https://github.com': 574482856@qq.com

Total 0 (delta 0), reused 0 (delta 0)

remote:

remote: Create a pull request for 'fetch1620' on GitHub by visiting:

remote:      https://github.com/songyongzhan/test/pull/new/fetch1620

remote:

To https://github.com/songyongzhan/test.git

 * [new branch]      fetch1620 -> fetch1620

Branch 'fetch1620' set up to track remote branch 'fetch1620' from 'origin'.





git branch -m m03 fetch0520  将m03分支 更名为 fetch0520


git checkout -b m03  在本地分支的基础上创建m03 这个分支



git branch -D m03 将本地分支删除


git merge master  合并master 分支


git remote add  www  github.git



================tag===================










================git pull 和  git fetch===================

git pull用法:

git pull命令的作用是:取回远程主机某个分支的更新,再与本地的指定分支合并。


一句话总结git pull和git fetch的区别:git pull = git fetch + git merge


git fetch不会进行合并执行后需要手动执行git merge合并分支,而git pull拉取远程分之后直接与本地分支进行合并。更准确地说,git pull使用给定的参数运行git fetch,并调用git merge将检索到的分支头合并到当前分支中。


基本用法:


git pull <远程主机名> <远程分支名>:<本地分支名>

1

例如执行下面语句:


git pull origin master:brantest

1

将远程主机origin的master分支拉取过来,与本地的brantest分支合并。


后面的冒号可以省略:


git pull origin master

1

表示将远程origin主机的master分支拉取过来和本地的当前分支进行合并。


上面的pull操作用fetch表示为:


git fetch origin master:brantest

git merge brantest

1

2

相比起来git fetch更安全一些,因为在merge前,我们可以查看更新情况,然后再决定是否合并。


================git pull 和  git fetch===================




全网最全操作 Git 命令,快速手册,人人必备

https://www.heibaiketang.com/blog/show/111.html