Git的使用

新建:

先create一个仓库,在github:

创建完成后在本地仓库右键git bash:

1 git init //初始化git仓库,完成以后会有一个隐藏文件.git
2 git remote add origin https://github.com/xym4869/Graduation_Project.git  //远程连接仓库
3 git status  //查看仓库里文件状态
4 git add xxx  //暂存本地
5 git commit -m "xxx"  //描述一下
6 git push -u origin master  //本地push到master分支

error: src refspec master does not match any.

解决:我这里缺了第5行.

 

error2:

$ git push -u origin master
Warning: Permanently added the RSA host key for IP address 'xx.xx.xxx.xxx' to the list of known hosts.
To git@github.com:hahaha/ftpmanage.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:hahahah/ftpmanage.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决:提示先pull再push

git pull --rebase origin master

git push -u origin master

 

push进入一个新建的branch

首先,进入工程的文件夹中,
git checkout -b xxxx //git进入本地的仓库,新建一个xxxx的分支  
git status  //查看当前的状态,红色的是被更改过的部分
git add xxx  //这里的xxx可以是红色部分最左的文件夹的名字,是将更改部分放入本地暂存区
git commit -m "xxxxxxxxxx"  //添加更改部分注释,xxxx部分代表注释的内容,可以是中文
git push origin xxxx  //push到branch中,xxxx可以存在也可以不存在,不存在即新建

 

push进一个branch

 git checkout master   (切换到本地的master分支)
 git pull origin master   (更新远程最新的master分支到本地)
git checkout -b  xxxx   //基于master创建本次工作的开发分支,注意:-b参数仅在新建分支时用;如果本地分支已经创建了,在切换本地分支时,不需要-b参数
git push origin xxxx  //第一次提交分支到远程时增加一个 -u参数, 例如  git push -u origin xxxx  ,会在提交同时建立本地分支到远程分支的映射关系;如果已经-u提交过了,再次提交时,不需要-u参数

 git强制提交本地分支覆盖远程分支

git push origin 本地分支名:远程分支名 --force
//不加本地分支名可能报错:src refspec 远程分支名 does not match any

 

从特定分支clone:

git clone -b xxx(分支名称) http...(clone的地址)

 

更新本地代码:

git pull  //分支不变,仅仅更新本地代码

//如果需要查看一下更新过的内容 git status

 

拉取远程分支到本地

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

//如拉取远程的master分支到本地wy分支:
git pull origin master:wy

 merge:

git checkout dev //切换到dev分支
git merge master //将master合并到dev

 

一次性更新所有文件:

git add .

 

posted @ 2018-02-27 11:39  Shaw_喆宇  阅读(180)  评论(0编辑  收藏  举报