GIT使用-1.GIT常见命令学习

来源:互联网 发布:材料预算软件 编辑:程序博客网 时间:2024/06/08 11:58

1.git clone

说明

克隆远端仓库中代码到本地仓库
$ git clone <远端仓库地址>  <本地目录名>
本地目录名是你设定的项目目录名,可选项,不设定会以默认名作为选项

举例

$ git clone https://github.com/hewin/machielearning-MLP.git  MLP
$ git clone https://github.com/hewin/machielearning-MLP.git
$ ls -al
total 0
drwxr-xr-x  5 chenwenxin  staff  170 10 19 14:05 MLP
drwxr-xr-x  6 chenwenxin  staff  204 10 19 13:57 machielearning-MLP


这两个语句会生成不同名词的文件夹,但是里面的内容是一样的。

2.git remote

说明

用来对远程主机进行的增、删、改、查操作

增:git remote add <主机名>  <远端库地址>
删:git remote rm <主机名>
查:git remote show <主机名> 用于查看远端主机,不加主机名会显示主机列表,加主机名会显示详细信息
改:git remote rename <原主机名>  <新主机名>

3.git fetch

说明1

将远端仓库的代码下载到本地仓库,供后面使用,比如再新建一个分支或者是merge到某个分支
git fecth <远程主机> <分支名>

示例1:git fetch不加其他内容,直接将所有的远端仓代码fetch到本地仓

$ git fetch

Fromhttps://github.com/hewin/machielearning-MLP
 * [new branch]      newBranch  -> origin/newBranch
 * [new branch]      test       -> origin/test

示例2:将远端仓mlp的各个分支代码都拉到本地仓

$ git fetch mlp

From https://github.com/hewin/machielearning-MLP
 * [new branch]      newBranch  -> mlp/newBranch
 * [new branch]      test       -> mlp/test

示例3:将远端mlp仓库的test分支代码fetch到本地仓

$ git fetch mlp test

From https://github.com/hewin/machielearning-MLP
 * branch            test       -> FETCH_HEAD

说明2

另外可以可以用git merge或者git rebase 命令,在本地分支上合并远程分支

示例1:rebase远程分支到本地分支

$ git rebase origin/newBranch

Current branch newBranch is up to date.

示例2:merge远程分支到本地分支

$ git merge origin/newBranch

Already up-to-date.

4.git checkout 

说明

切换、创建分支命令
git checkout -b <分支名> <仓库名/分支名>

示例1:本地仓建立分支

$ git checkout -b newBranch origin/master


Branch newBranch set up to track remote branch master from origin.
Switched to a new branch 'newBranch'

$ git branch

  master
*newBranch

示例2:从本地分支建立新分支

$ git checkout -b newBranch2 newBranch

Switched  to a new branch ‘newBranch2'

$ git branch

  master
  newBranch
*newBranch2

示例3:切换分支

git checkout newBranch
Switched to branch 'newBranch'
Your branch is up-to-date with 'origin/master’.

$ git branch
 master
*newBranch
 newBranch2

5.git branch

说明:

查看分支(包括本地远程)命令
git branch <-r / -a>

示例:不加参数查看本地分支,-r表示查看远端分支,-a查看所有分支(远端和本地)

$ git branch

master

newBranch
*newBranch2

$ git branch -r

origin/HEAD-> origin/master

origin/master


$ git branch -a

*master

newBranch

remotes/origin/HEAD-> origin/master

remotes/origin/master


6.git pull

说明

同步远端库代码合并到本地分支
$ git pull <远程主机名> <远程分支名>:<本地分支名>

示例1:将远端仓库origin的master分支与本地newBranch分支合并

$ git pull origin master:newBranch
Already up-to-date.

示例2:将远端仓库origin的master分支与默认分支合并

$ git pull origin master
Fromhttps://github.com/hewin/machielearning-MLP
 * branch            master     -> FETCH_HEAD
Already up-to-date.

示例3:以rebase模式,从远端仓库origin的master分支与newBranch分支合并

$ git pull --rebase origin master:newBranch
Already up-to-date.


7.git push

说明

s将本地分支的更新推送到远程主机,格式类似git pull(冒号前后的分支顺序相互调换)。如果远程分支名不存在,则会被新建
$ git pull <远程主机名> <本地分支>:<远程分支>

示例1:将本地仓库的newBranch分支的更新推送到远端仓库origin的master分支

$ git push origin newBranch:master
Everything up-to-date

示例2:远端test分支不存在,则会在远端新建test分支

$ git push origin newBranch:test
Total 0 (delta 0), reused 0 (delta 0)
Tohttps://github.com/hewin/machielearning-MLP.git
 * [new branch]      newBranch -> test

$ git branch -a
 master
*newBranch
 remotes/mlp/master
 remotes/origin/HEAD-> origin/master
 remotes/origin/master
 remotes/origin/test

示例3:可以指定远程默认主机

$ git push -u origin newBranch
Total 0 (delta 0), reused 0 (delta 0)
Tohttps://github.com/hewin/machielearning-MLP.git
 * [new branch]      newBranch -> newBranch

$ git pull
Already up-to-date.

参考:http://www.ruanyifeng.com/blog/2014/06/git_remote.html