Git remote repository and branch management

One problem: After configuring local git, every time I need to submit a password NND?  

Solution:

Again with ssh-keygen -t rsa -C "[email protected]"  生成密钥

Among them, ask if you cover the input y -? Overwrite (y / n) id-rsa before the y cover

Enter all the rest, do not enter the password in the Enter passphrase (empty for no passphrase).

Finally, in the Settings github of SSH and GPG keys, click the new SSH keys, copy the contents id-rsa.pub under .ssh directory key to this column.

carry out! !

 

Git remote repository:

A: the local Git project uploaded to the new project, and build relationships.!

Register a github create a new Repository

Create a local git init git project and add a file (no files are empty when the project will be prompted to push to a remote repository wrong)

Then use git add and git commit -m

And then executed in the local repository: git remote add origin git@github.com:yourProjectName/Name.git

The last execution in the push command:    git push -u Origin Master

The contents of the local repository pushed to the remote, use git pushthe command, in fact, the current branch masterpushed to the remote.

Because the remote library is empty, the first time we push mastera branch, with the -uparameters, Git will not only local masterremote content push new branch of the masterbranch, but also the local masterbranch and remote masterassociation branch up after command can be simplified push or pull.

wangjiaingdeAir: learnGit wangjianqing $ git remote control add origin1 [email protected]: FrankWangJQ / learnGIt.git

wangjiaingdeAir:learnGit wangjianqing$ git push -u origin master

To github.com:FrankWangJQ/Lonch.git

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to '[email protected]:FrankWangJQ/Lonch.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.

wangjiaingdeAir:learnGit wangjianqing$ git push -u origin1 master

Counting objects: 3, done.

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

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

To github.com:FrankWangJQ/learnGIt.git

 * [new branch]      master -> master

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

 之后本地的learnGit项目再有任何改动,只要执行完提交, 然后直接使用命令:git push origin master  就可以了.

 

二:把远程已经存在的项目克隆到本地并建立关联

在Git目录中执行  

git clone [email protected]:FrankWangJQ/Lonch.git

会在该目录中生成一个新新项目 Lonch  并把项目中的文件克隆到本地

wangjiaingdeAir:PycharmProjects wangjianqing$ git clone [email protected]:FrankWangJQ/Lonch.git

Cloning into 'Lonch'...

remote: Enumerating objects: 6, done.

remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 6

Receiving objects: 100% (6/6), done.

wangjiaingdeAir:PycharmProjects wangjianqing$ ls

Fastmonkey YunZhens_UI-master geekbangpython

Lonch YunZhens_api-master learnGit

UI_demo_python dubboApi_testDemo

UI_java_wechat dubboDemo

wangjiaingdeAir:PycharmProjects wangjianqing$ cd Lonch/

wangjiaingdeAir:Lonch wangjianqing$ ls

README.md

 

 

 

Git的分支管理:

开启一条新的分支  dev  开发分支:  

git checkout -b dev 创建并切换分支  创建分支:git branch <name>  切换分支:git checkout <name>

查看当前项目的分支:

$ git branch
* dev
  master

此时在项目中提交的任何修改  在push后都会提交到远程的分支库中,  因为当前的HEAD  指向的 dev分支.  dev分支的修改不会影响到主干代码

修改完成后, 可以切换回原来的主干  $ git checkout master   

这时,你之前的修改是不会在master中展示的   这时候再把分支中的修改合并至主干才能生效:  $ git merge dev

合并完成后可以将原有分支删除  $ git branch -d dev

 

如果在git merge dev 过程中存在冲突 (比如你在开发dev的过程中修改了readMe,其他人在主干上也修改了readMe),此时用主干去合并dev时会提示冲突!

可以使用 $ git status   命令查看冲突内容:

手动修改后  提交代码  然后执行合并操作

使用:$ git log --graph --pretty=oneline --abbrev-commit  查看分支合并图

通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。

如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。

合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

git merge --no-ff -m "merge with no-ff" dev

因为本次合并要创建一个新的commit,所以加上-m参数,把commit描述写进去

 

修复bug临时创建分支:

修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

当手头工作没有完成时,需要先

$ git stash  将现有已经完成的部分开发工作保存起来, 然后切换到master分支  在master分支基础上创建bug分支, 

修改完bug后 合并到master分支;  此时需要跳转到之前开发时的分支上 $ git stash list 查看stash状态(可能有多个);然后你想回复到哪个

$ git stash apply stash@{0} 

另一种方式是用 git stash pop,恢复的同时把stash内容也删了

如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除

 

开发过程中分支使用流程一般是

  1. 首先,可以试图用git push origin <branch-name>推送自己的修改;

  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

  3. 如果合并有冲突,则解决冲突,并在本地提交;

  4. 没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!

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

参考资料:  https://www.liaoxuefeng.com/wiki/896043488029600/900375748016320

 

Guess you like

Origin www.cnblogs.com/1026164853qqcom/p/11267319.html