您的位置:首页 > 其它

git常用命令

2015-09-11 15:51 281 查看
1.配置帐号信息

在cmd或者终端下输入以下命令行:

git config --global user.name yourname
git config --global user.email youremail

2.创建本地ssh

这是一种传输代码的方法,速度快又安全。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。

在终端或cmd输入以下命令行:

ssh-keygen -t rsa -C "youremail"

3.将ssh配置到GitHub中

在mac os X 下前往文件夹,/Users/自己电脑用户名/.ssh。

windows应该是(C:\Documents and Settings\Administrator\.ssh (或者 C:\Users\自己电脑用户名\.ssh)中)。

然后用记事本打开id_rsa.pub,将里面的全部代码复制到github的SSH中。

创建版本库

第一步,在本地创建一个版本库,代码如下:

$ mkdir test   #test是你的文件夹的名字

$ cd test     #进入test所在目录

$ pwd        #pwd命令用于显示当前目录    

/Users/trigkit4/test #这是在我的Mac上的目录

第二步,通过git init命令把这个目录变成Git可以管理的仓库:

$ git init

然后会输出以下信息:

Initialized empty Git repository in /Users/trigkit4/banner/.git/

这里的.git目录是Git用来跟踪管理版本库的,默认是隐藏的。

第三部,接着,在github上创建一个你自己的new repository,然后下一步,

mkdir test  

cd test  

git init    

# initialize your git repository  

touch README  

# create a file named README  

git add README    

# add README to cache  

git commit -m 'first commit'  

# commit your files to local repository  

然后我们将本地的文件传送至github中,使用如下命令:

git remote add origin https://github.com/yourname/test.git  

git push -u origin master  

从现有仓库克隆

git clone git://github.com/yourname/test.git

git clone git://github.com/yourname/grit.git mypro#克隆到自定义文件夹

本地

git add *#跟踪新文件

rm *&git rm *#移除文件

git rm -f *#移除文件

git rm --cached *#取消跟踪

git mv file_from file_to#重命名跟踪文件

git log#查看提交记录

git commit#提交更新

git commit -m 'message'

git commit -a#跳过使用暂存区域,把所有已经跟踪过的文件暂存起来一并提交

git commit --amend#修改最后一次提交

git reset HEAD *#取消已经暂存的文件

git checkout -- file#取消对文件的修改(从暂存区去除file)

git checkout branch|tag|commit -- file_name#从仓库取出file覆盖当前分支

git checkout -- .#从暂存区去除文件覆盖工作区

分支

git branch#列出本地分支

git branch -r#列出远端分支

git branch -a#列出所有分支

git branch -v#查看各个分支最后一个提交对象的信息

git branch --merge#查看已经合并到当前分支的分支

git branch --no-merge#查看为合并到当前分支的分支

git branch test#新建test分支

git checkout test#切换到test分支

git checkout -b test#新建+切换到test分支

git checkout -b test dev#基于dev新建test分支,并切换

git branch -d test#删除test分支

git branch -D test#强制删除test分支

git merge test#将test分支合并到当前分支

git rebase master#将master分之上超前的提交,变基到当前分支

常见错误

第一个:

error: src refspec master does not match any.

error: failed to push some refs to 'https://github.com/yourname/test.git'

输入如下代码:

$ cd myproject

$ git init

$ git add .

$ git commit -m 'initial commit'

$ git push origin master

第二个:

git push -u origin master fatal: unable to access 'https://github.com/ZeyuChen/TPlus.git/': SSLRead() return error -9806

这种错误就是https挂了,解决方案就是在project目录里面的.git/config文件里面,修改repo的url连接,从https修改为git协议就可以了。

本来是url = https://github.com/ZeyuChen/TPlus.git
修改为

[remote "origin"]

   url = ssh://git@github.com/ZeyuChen/TPlus.git

   fetch = +refs/heads/*:refs/remotes/origin/*

再次push就ok了。

Git 命令详解

现在我们有了本地和远程的版本库,让我们来试着用用Git的基本命令:

git pull:从其他的版本库(既可以是远程的也可以是本地的)将代码更新到本地,例如:'git pull origin master'就是将origin这个版本库的代码更新到本地的master主枝,该功能类似于SVN的update

git add:是 将当前更改或者新增的文件加入到Git的索引中,加入到Git的索引中就表示记入了版本历史中,这也是提交之前所需要执行的一步,例如'git add app/model/user.rb'就会增加app/model/user.rb文件到Git的索引中,该功能类似于SVN的add

git rm:从当前的工作空间中和索引中删除文件,例如'git rm app/model/user.rb',该功能类似于SVN的rm、del

git commit:提交当前工作空间的修改内容,类似于SVN的commit命令,例如'git commit -m story #3, add user model',提交的时候必须用-m来输入一条提交信息,该功能类似于SVN的commit

git push:将本地commit的代码更新到远程版本库中,例如'git push origin'就会将本地的代码更新到名为orgin的远程版本库中

git log:查看历史日志,该功能类似于SVN的log

git revert:还原一个版本的修改,必须提供一个具体的Git版本号,例如'git revert bbaf6fb5060b4875b18ff9ff637ce118256d6f20',Git的版本号都是生成的一个哈希值

提交时不用输入密码的办法

https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速

设置记住密码(默认15分钟):

git config --global credential.helper cache

如果想自己设置时间,可以这样做:

git config credential.helper 'cache --timeout=3600'

这样就设置一个小时之后失效

长期存储密码:

git config --global credential.helper store

增加远程地址的时候带上密码也是可以的。(推荐)
http://yourname:password@git.oschina.net/name/project.git
补充:使用客户端也可以存储密码的。

如果你正在使用ssh而且想体验https带来的高速,那么你可以这样做: 切换到项目目录下 :

cd projectfile/
4000

移除远程ssh方式的仓库地址

git remote rm origin

增加https远程仓库地址

git remote add origin http://yourname:password@git.oschina.net/name/project.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: