git 简明教程(1) --创建及提交

本文对 通过命令行进行 git 操作做简单 总结。


建立git

1.登录github官网完成注册。并下载git客户端。

2.为本机配置 git的用户名和 密码。

Username

First you need to tell git your name, so that it can properly label the commits you make.

git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit

Email

Git saves your email address into the commits you make. We use the email address to associate your commits with your GitHub account.

git config --global user.email "[email protected]"
# Sets the default email for git to use when you commit


创建仓库 repository
1. 在github官网 创建 reponsitory -- hello-world
2. 本地建立相应文件夹 
 
mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory

cd ~/Hello-World
# Changes the current working directory to your newly created directory

git init
# Sets up the necessary Git files
# Initialized empty Git repository in /Users/you/Hello-World/.git/

touch README
# Creates a file called "README" in your Hello-World directory     打开README 写入文本"hello,world"

3.commit 
git add README
# Stages your README file, adding it to the list of files to be committed

git commit -m 'first commit'
# Commits your files, adding the message "first commit"

此时完成了本地的 索引加载与提交,并未与 github 有任何联系,下面完成与 github的数据更新

4. push
首先在远端 建立一个连接,然后推送你的提交
git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository

git push origin master
# Sends your commits in the "master" branch to GitHub

以上为官网的做法,存在问题。

问题一:error: The requested URL returned error: 403.... fatal: remote re-origin already exists.
原因 git目前 http支持的不太好,或者说暂时不能用(个人不确定阿)。
解决方法:改用 ssh方法 来连接
 
  
git remote add re-origin [email protected]:usrname/Hello-World.git

问题二:
error: failed to push some refs to...Merge the remote changes before pushing again.
应先远端获取最新版本,然后提交,防止冲突
git pull origin master //从远端 origin获取最新版本到本地master中
git commit -am "update .."
git push origin master //向远端推送

此时打开 github相应 仓库 即可开到上传的文件。


简单总结
1.远端建立 reponsitory
2.本地建立相应文件并进入
3.编辑本地文件
$ git init //
$ git add . //完成对添加文件的索引
$ git commit -am 'init commit.' //本地提交
$ git remote add origin [email protected]:username/username.github.com //本地与远端建立关系 ,通过ssh方式
$ git pull origin master //从远端 origin获取最新版本到本地master中
 
    
 
    
$ git commit -am "update .."
$ git push origin master //向远端推送 完成简单的创建与 远端提交

参考此处 create a reponsitory

如有错误和意见,请大家指出,谢谢

你可能感兴趣的:(git 简明教程(1) --创建及提交)