github常用命令(转)

来源:互联网 发布:盗取数据 编辑:程序博客网 时间:2024/06/06 17:03

1.创建一个新的repository:

先在github上创建并写好相关名字,描述。

$cd ~/hello-world       //到hello-world目录

$git init                  //初始化

$git add .                //把所有文件加入到索引(不想把所有文件加入,可以用gitignore或add具体文件)

$git commit            //提交到本地仓库,然后会填写更新日志( -m “更新日志”也可)

$git remote add origin git@github.com:WadeLeng/hello-world.git      //增加到remote

$git push origin master   //push到github上

2.更新项目(新加了文件):

$cd ~/hello-world

$git add .               //这样可以自动判断新加了哪些文件,或者手动加入文件名字

$git commit            //提交到本地仓库

$git push origin master   //不是新创建的,不用再add 到remote上了

3.更新项目(没新加文件,只有删除或者修改文件):

$cd ~/hello-world

$git commit -a        //记录删除或修改了哪些文件

$git push origin master  //提交到github

4.忽略一些文件,比如*.o等:

$cd ~/hello-world

$vim .gitignore    //把文件类型加入到.gitignore中,保存

然后就可以git add . 能自动过滤这种文件

5.clone代码到本地:

$gitclone git@github.com:WadeLeng/hello-world.git

假如本地已经存在了代码,而仓库里有更新,把更改的合并到本地的项目:

$git fetch origin   //获取远程更新

$git merge origin/master //把更新的内容合并到本地分支

6.撤销

$git reset

7.删除

$git rm  // 不是用rm

//------------------------------常见错误-----------------------------------

1.$ git remote add origingit@github.com:WadeLeng/hello-world.git

 错误提示:fatal: remote origin alreadyexists.

 解决办法:$ git remote rm origin

 然后在执行:$ git remote add origingit@github.com:WadeLeng/hello-world.git 就不会报错误了

 2. $ git push originmaster

 错误提示:error:failed to push som refsto

 解决办法:$ git pull origin master//先把远程服务器github上面的文件拉先来,再push 上去。

 

-------------------------------------------------------------------------------------------------

git remote add upstream https://github.com/winterIce/testTitle.git(别人的repository)   // 新建分支用于存放别人的repository

git clone https://github.com/winterIce/testTitle.git 克隆到本地

git fetch branch2//更新信息

git mergebranch2/master   //merge本地信息

git add . //添加新文件或者更改新文件

git remove  ** //删除文件

git commit -m 'by who do what'  //提交文件到本地

git push  push到服务器上

git pull originmaster 从服务器上拉取信息

git remote  查看repository上的所有分支

git branch -a  查看所有分支

git branch -r 查看远程分支

git branch -d *** //删除分支

git branch *** //新建分支

git checkout ***//切换分支

git status //查看状态

git log //查看提交修改记录

0 0