git多人协作-常用命令

教程源自 廖雪峰老师的博客,在此表示感谢,知识的先行者。

查看远程仓库信息 git remote -v是可以查看更多信息

$ git remote

推送分支,将本地的master推送到远程的origin

$ git push origin master

当你的小伙伴从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支。但是一般开发是在dev分支上的.
你的小伙伴要在dev分支上开发,就必须创建远程origin的dev分支到本地,于是他用这个命令创建本地dev分支:

$ git checkout -b dev origin/dev

一旦你和你的小伙伴同时对一个文件进行更改,那样你们最后一个提交的同学就会提交不上去。

$ git push origin dev
To github.com:michaelliao/learngit.git
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to '[email protected]:michaelliao/learngit.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git 提示你 只用git pull进行对文件的拉取。
有时候git pull也会失败,因为本地没有与远程仓库对应的分支

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull  

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/ dev

如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to origin/

$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.

pull 成功之后 ,手动修改冲突,然后再提交,再push;

对此,解决冲突提交之后,git log 的提交历史会变得不好判断,
rebase操作可以把本地未push的分叉提交历史整理成直线;
rebase的目的是使得我们在查看历史提交的变化时更容易,因为分叉的提交需要三方对比。

$ git rebase

git tag标签 :在当前分支上打标签 v1.0。标签默认会打到最近一次的commit上。

$ git tag v1.0

查看提交日志$ git log --pretty=oneline --abbrev-commit,再某个commit上打tag

$ git log --pretty=oneline --abbrev-commit
12a631b (HEAD -> master, tag: v1.0, origin/master) merged bug fix 101
4c805e2 fix bug 101
e1e9c68 merge with no-ff
f52c633 add merge
cf810e4 conflict fixed
5dc6824 & simple
14096d0 AND simple
b17d20e branch test
d46f35e remove test.txt
b84166e add test.txt
519219b git tracks changes
e43a48b understand how stage works
1094adb append GPL
e475afc add distributed
eaadf4e wrote a readme file

比方说要对add merge这次提交打标签,它对应的commit id是f52c633,敲入命令:

$ git tag v0.9 f52c633

查看标签

$ git tag

注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show 查看标签

$ git show v0.9
commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9)
Author: Michael Liao 
Date:   Fri May 18 21:56:54 2018 +0800

    add merge

diff --git a/readme.txt b/readme.txt
...

还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字:

$ git tag -a v0.1 -m "version 0.1 released" 1094adb

tag的其他操作
命令git push origin 可以推送一个本地标签;

命令git push origin --tags可以推送全部未推送过的本地标签;

命令git tag -d 可以删除一个本地标签;

命令git push origin :refs/tags/可以删除一个远程标签。

你可能感兴趣的:(git多人协作-常用命令)