Windows下Git命令行基本使用

原文转自:https://blog.csdn.net/quiet_girl/article/details/78359202

一、准备工作

(1)在 https://github.com 网站上注册一个账号并设置用户名、密码,新建一个repository

(2)在网站 http://msysgit.github.io/ 上下载git,并安装

二、设置SSH

1、首先在本地创建ssh key,使用下面的命令:

[plain]  view plain  copy
  1. $ ssh-keygen -t rsa -C "[email protected]"  

后面的参数[email protected]改为你在github网站上注册的邮箱,之后要求确认路径和输入密码(密码就是网站上自己设置的密码),之后一路默认。成功之后可以在C://Users/.ssh下找到id_rsa.pub文件,打开全部复制。

回到github网页版,点击头像 --> Settings --> SSH和GPG keys --> New SSH key,粘贴上之前复制的密钥内容。如下图:


为了验证是够成功,在git bash下输入下面的命令:

[plain]  view plain  copy
  1. $ ssh -T [email protected]  

第一次会提示是够continue,输入“yes”,接着会看到“You've successfully authenticated, but GitHub does not provide shell access”,表示连接成功。

2、设置username和email,github每次commit都会记录他们。使用下面的命令:

[plain]  view plain  copy
  1. $ git config --global user.name "your name"  
  2. $ git config --global user.email "[email protected]"  

yourname是网页上的用户名,[email protected]是用户注册的邮箱。

三、上传本地文件到远程仓库

这里只讲述最简单的上传,所以采用了一种比较固定的模式,其他再多些的用法可自行搜索,只是成功上传了的一个流程(弄了好久,终于成功了..^_^..),说明:下面的内容几乎来自http://blog.csdn.net/sinat_33366020/article/details/73732769,非常感谢作者!!

1、先进入项目文件夹,比如我想把python下面的test文件夹及test文件夹下的文件上传,则进入python文件夹内(如上传python下面的文件及文件夹,则需要进入python的上一级目录),使用cd命令。

2、通过git init把当前目录变成git可以管理的仓库,使用如下命令:

[plain]  view plain  copy
  1. git init  
可以使用ls -a查看当前文件夹下是否包括一个.git的文件夹,若包含,则成功。

3、把文件添加到本地版本库,使用命令git add添加文件;添加到暂存区里面去,如果add后面加入的是“.”,则意味着需要添加当前文件夹下的所有文件,若只想添加/python/test/note.txt这个文件,则只需要将"."改成当前路径(/test/note.txt)即可,命令如下:

[plain]  view plain  copy
  1. git add .  

4、用命令git commit把文件提交到仓库,如下命令:

[plain]  view plain  copy
  1. git commit -m 'note'  
引号里面的是提交的说明,如果忘记带参数-m,则会自动打开vim编辑器。

5、关联到远程仓库,使用下面的命令:

[plain]  view plain  copy
  1. git remote add origin 你的远程库地址  

如:

[plain]  view plain  copy
  1. git remote add origin https://github.com/nnaa/helloworld.git  
nnaa是你的用户名,helloworld是新建的仓库名称。
如果发现上面步骤写错了,则使用下面的命令:
[plain]  view plain  copy
  1. git remote rm origin   //删除origin  
  2. git remote add origin https://github.com/nana/demo.git   //重新添加origin  
6、远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
[plain]  view plain  copy
  1. git pull --rebase origin master  
7、 将最新的修改推送到远程仓库,使用下面的命令:
[plain]  view plain  copy
  1. git push -u origin master  

其中,origin是远程仓库名字; master是远程仓库所属分支

注意: 我们第一次push的时候,加上-u参数,Git就会把本地的master分支和远程的master分支进行关联起来,我们以后的push操作就不再需要加上-u参数了

如果出现类似下面内容:

[plain]  view plain  copy
  1. Username for 'https://github.com': shiren1118  
  2. Password for 'https://[email protected]':  
  3. To https://github.com/shiren1118/iOS_code_agile.git  
  4.  ![rejected]        master -> master(non-fast-forward)  
  5. error: failed to push some refs to'https://github.com/shiren1118/iOS_code_agile.git'  
  6. hint: Updates were rejected because the tip of yourcurrent branch is behind  
  7. hint: its remote counterpart. Merge the remote changes(e.g. 'git pull')  
  8. hint: before pushing again.  
  9. hint: See the 'Note about fast-forwards' in 'git push--help' for details.  

则输入命令下列命令即可:

[plain]  view plain  copy
  1. git push -u origin master -f     

四、拷贝其他git文件

如果从在git服务器所在主机上的其他账户获取git服务器上面文件,则直接用gitclone + git仓库的路径,即:

[plain]  view plain  copy
  1. git clone /nnaa/sample.git(将仓库地址复制)  


参考文献:

http://www.runoob.com/w3cnote/git-guide.html

http://blog.csdn.net/sinat_33366020/article/details/73732769

http://www.ihref.com/read-16369.html

更多内容可参考:

http://www.ihref.com/read-16369.html