Git命令回顾

团队从Eclipse迁移到Android Studio之后,也从SVN迁移到Git了。

一直忙于需求迭代无暇做迁移,现在才开始做,相见恨晚,好东西,高大上,词穷。

回顾和记录一下git的一些基本操作。下面完成这样一个流程:

远程机器创建git仓库 --> 本地机器创建git仓库 -->从本地仓库推代码到远程仓库 --> 从远程仓库把代码clone到本地新的仓库。

//先查看git装了没有
[user@remotemachine /]# git

//设置全局参数
[user@remotemachine /]# git config --global user.name "User"
[user@remotemachine /]# git config --global user.email "xxx@xxx.com"

//创建一个目录
[user@remotemachine /]# mkdir Git001

//进入目录
[user@remotemachine /]# cd Git001/

//初始化为一个git仓库,这里有坑
[user@remotemachine Git001]# git init
Initialized empty Git repository in /Git001/.git/

//用上面的方式创建的仓库是一个non-bare的仓库,是无法push本地仓库到这个non-bare仓库的,可参考bare and non-bare.

//所以要初始化一个bare的git仓库作为远程仓库
[user@remotemachine Git002]# git init --bare
Initialized empty Git repository in /Git002/
//查看一下(可带着-ah查看)
[user@remotemachine SwiftLearning]# ls
branches  config  description  HEAD  hooks  info  objects  refs

到现在远程git仓库就创建好了,下面来创建本地仓库并push到这个远程仓库。

//初始化git,这里就不用bare了
LocalMachine:GitA viyu$ git init
Initialized empty Git repository in /xxx/.git/

//查看git仓库的状态
LocalMachine:GitA viyu$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
Swift002.playground/
[...省略]
nothing added to commit but untracked files present (use "git add" to track)

//添加一个文件到本地仓库
LocalMachine:GitA viyu$ git add swift001.playground/

//添加所有文件到本地仓库
LocalMachine:GitA viyu$ git add .

//再次查看git状态
LocalMachine:GitA viyu$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file:   Swift002.playground/Contents.swift
[....省略]

//提交到本地仓库,不亏是分布式仓库,这就提交了 
LocalMachine:GitA viyu$ git commit -m "add all learning file to git"
20 files changed, 377 insertions(+)  

//再次查看git状态
LocalMachine:GitA viyu$ git status
On branch master
nothing to commit, working directory clean

//push本地仓库到上面建立的远程仓库,先链接
LocalMachine:GitA viyu$ git remote add origin ssh://user@remotemachine/Git002

fatal: remote origin already exists.
//假如报上面这个错误,origin已经存在了,要么另起一个名字,要么更新之:
LocalMachine:GitA viyu$ git remote set-url origin ssh://user@remotemachine/Git002

//再推送
LocalMachine:GitA viyu$ git push origin master 

现在已经把本地仓库推送到远程仓库了,下面再从远程仓库clone下来到一个新的本地仓库

//先初始化
ViyudeMacBook-Pro:gittest viyu$ git init

//再克隆
ViyudeMacBook-Pro:gittest viyu$ git clone ssh://user@remotemachine/Git002

//查看状态
ViyudeMacBook-Pro:gittest viyu$ git status

//查看总共几个分支
ViyudeMacBook-Pro:gittest viyu$ git branch
* master

待续...
posted @ 2016-03-17 20:15  Mosthink  阅读(211)  评论(0编辑  收藏  举报