1.安装
yum install -y git

2.配置帐户(github.com注册)
git config --global user.name goozgk
git config --global user.email goozgk@qq.com

3.创建一个新的仓库repo
mkdir -p /work/git_repo
cd /worl/git_repo
git init # 初始化

4.编写程序
vim test.py

5.查看状态
git status
[root@localhost git_repo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.py
nothing added to commit but untracked files present (use "git add" to track)

6.暂存 – git add

[root@localhost git_repo]# git add test.py
[root@localhost git_repo]# git add -A # if you want to commit all the file in the current dir.
[root@localhost git_repo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.py
#
[root@localhost git_repo]#

7.提交 – git commit 到本地仓库(master)中。
[root@localhost git_repo]# git commit -m "create test.py"
[master (root-commit) 8fe112e] Mon Jul 11 07:31:36 PDT 2016
1 files changed, 4 insertions(+), 0 deletions(-)
create mode 100755 test.py
[root@localhost git_repo]#

8.链接远端仓库 – git remote add
git remote add origin https://github.com/goozgk/work.git
# 通常主远端仓库被称为origin; 可以有其他仓库,另起名字即可
# git remote add <remoteRepositoryName> <remoteRepositroyURL>

9.上传到服务器 – git push
git push origin master
# git push 远端仓库名字(origin) 分支名字(master)

[root@localhost git_repo]# git push origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/goozgk/work.git/info/refs

fatal: HTTP request failed

失败。。。

改为使用ssh登陆,实际上github暂不支持http的读写权限
ssh-keygen -t rsa 创建秘钥对(参考http://www.cnblogs.com/goozgk/p/5663453.html)
复制id_rsa.pub的内容到github.com自己项目主页settings->Deploy Keys中

设置并确认
git remote -v
git remote set-url origin ssh://git@github.com/goozgk/work.git
git remote -v

格式:
git remote set-url <name> https://yourusername@github.com/<repo>.git # https暂不可用无法验证
git remote set-url <name> ssh://git@github.com/<username>/<repo>.git

[root@localhost .ssh]# ssh -T goozgk@github.com
Hi goozgk/work! You've successfully authenticated, but GitHub does not provide shell access.

可以了!

[root@localhost git_repo]# git push
To ssh://git@github.com/goozgk/work.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://git@github.com/goozgk/work.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
[root@localhost git_repo]#

原因:origin分支有更新,不允许提交,应先把origin fetch回本地,merge后提交
----------------------------------------------------------
git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME(master) [git rebase #注意,和git merge有区别的!]
# Merges updates made online with your local work

OR

git pull origin YOUR_BRANCH_NAME(master) # 将远端仓库内容pull回本地并merge
# Grabs online updates and merges them with your local work

----------------------------------------------------------
git push -f
# 强推,会破坏remote仓库内容,不建议使用
----------------------------------------------------------

git push origin master

成功!!!

10.克隆仓库 – git clone
放在Github上的开源项目,人们可以看到你的代码。可以使用 git clone进行下载到本地。
# git clone https://github.com/goozgk/work.git
本地也会创建一个新的仓库,并自动将github上的分支设为远端分支。
[root@localhost work]# git remote -v
origin https://github.com/goozgk/work.git (fetch)
origin https://github.com/goozgk/work.git (push)

。。。可惜github.com不支持https上传修改,你也没有ssh KEY, 就是说你只能看,而不能随意修改别人的代码。只需要把你自己的公钥key部署到github.com中的project中就可以了。。。废话。。

It's not allow to use the same KEY to manipulate 2 or more Projects int the Github.com,ちょっとおかしいね!
It's also not allow 2 accounts to use the same key to access one Project。。。没有验证如何控制的。。

11.分支

创建新分支
git branch <bra1>

切换到新分支
git checkout <bra1>

删除分支
git branch -d <BranchName>

查看当前分支
git branch
git show-branch

切换到各个分支后,新建的文件,各个分支都可以看到的!!!(提交之前),但是在某个分支上commit后,只有这个分支可以看到!!!

在分支上测试完成后,git branch master切换回master分支,执行
git merge <branchName>; git branch -d <branchName>

12.查看log
git log
git log --oneline

查看具体提交了什么
git show <id number> #前几位就行,只要不会冲突

比较两次提交的不同
git diff [commit-from]..[commit-to]

13.回滚某个文件到之前的版本
git checkout 09bd8cc1 hello.txt
# 格式git checkout <id number> <fileFullPath>

14.复杂指令
git commmit --amend #打回最新提交到暂存区
git revert HEAD #打回最新提交到暂存区
git revert <id number> #打回提交<id number>到暂存区

15.配置 .gitignore
在项目根目录创建.gitignore文件,在文件中列出不需要提交的文件名,文件夹名,每个一行,.gitignore文件需要提交,就像普通文件一样

16.本地建两个repository,模拟本地和远端

[root@localhost work]# git clone ssh://git@github.com/goozgk/work.git
Initialized empty Git repository in /work/work/.git/
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta ), pack-reused
Receiving objects: % (/), done.
Resolving deltas: % (/), done.
[root@localhost work]# cd work/
[root@localhost work]# git push
Everything up-to-date
[root@localhost work]# git remote -v
origin ssh://git@github.com/goozgk/work.git (fetch)
origin ssh://git@github.com/goozgk/work.git (push)
[root@localhost work]#
[root@localhost work]#至此,创建github.com和本地(remote <>local)模式的普通系统 [root@localhost work]#
[root@localhost work]# cd ..
[root@localhost work]# mkdir a;cd a
[root@localhost a]# git clone /work/work/.git/ . #github.com < /work/work/.git < /work/a/.git 系统建立,看似完美!
Initialized empty Git repository in /work/a/.git/
[root@localhost a]# ll
total
-rw-r--r-- root root Jul : README.md
-rwxr-xr-x root root Jul : test1.py
-rwxr-xr-x root root Jul : test.py
[root@localhost a]# git push #提交也ok
Everything up-to-date
[root@localhost a]# git remote -v #确认也没问题
origin /work/work/.git/ (fetch)
origin /work/work/.git/ (push)
[root@localhost a]# git branch
* master
[root@localhost a]#
[root@localhost a]#
[root@localhost a]#
[root@localhost a]# ll
total
-rw-r--r-- root root Jul : README.md
-rwxr-xr-x root root Jul : test1.py
-rwxr-xr-x root root Jul : test.py
[root@localhost a]# touch a1 #创建新文件
[root@localhost a]# git push
Everything up-to-date
[root@localhost a]# git add -A
[root@localhost a]# git commit -m "add a1 on branch a" #提交,ok
[master 2c43137] add a1 on branch a
files changed, insertions(+), deletions(-)
create mode a1
[root@localhost a]# git push #远程push失败,不推荐此种方式!会引起混乱不一致。还没具体深究。。。。。。
Counting objects: , done.
Compressing objects: % (/), done.
Writing objects: % (/), bytes, done.
Total (delta ), reused (delta )
Unpacking objects: % (/), done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /work/work/.git/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/work/work/.git/'
[root@localhost a]#

github简单命令的更多相关文章

  1. GitHub简单命令行# 使用命令行传代码到GitHub

    第一次提交代码到Github 第一步: 建立本地仓库cd到你的本地项目根目录下,执行git命令 cd到本地项目 git init 第二步: 将本地项目工作区的所有文件添加到暂存区 git add . ...

  2. Git和Github简单教程

    原文链接:Git和Github简单教程 网络上关于Git和GitHub的教程不少,但是这些教程有的命令太少不够用,有的命令太多,使得初期学习的时候需要额外花不少时间在一些当前用不到的命令上. 这篇文章 ...

  3. Git和Github简单教程(收藏)

    原文链接:Git和Github简单教程 目录: 零.Git是什么 一.Git的主要功能:版本控制 二.概览 三.Git for Windows软件安装 四.本地Git的使用 五.Github与Git的 ...

  4. Git和Github简单教程【转】

    转自:https://www.cnblogs.com/schaepher/p/5561193.html#clone 原文链接:Git和Github简单教程 网络上关于Git和GitHub的教程不少,但 ...

  5. Elasticsearch学习(一)————简单命令

    Elasticsearch一.简介**Elasticsearch 是一个分布式的搜索和分析引擎,可以用于全文检索.结构化检索和分析,并能将这三者结合起来.Elasticsearch 基于 Lucene ...

  6. GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载

    GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...

  7. 转:Git和Github简单教程

    转自:https://www.cnblogs.com/schaepher/p/5561193.html Git和Github简单教程   原文链接:Git和Github简单教程 网络上关于Git和Gi ...

  8. Apache 的搭建及vim的简单命令

    一. vim 简单命令 pwd     当前路径 ls    当前路径所有目录 cd  目录地址   跳转到指定目录 /xxx  查找xxx x 删除当前字符 n 执行上一次查找 二.为什么使用apa ...

  9. (2016春) 作业1:博客和Github简单练习

    0. 博客和Github简单练习 总分:10分 1. 目的 博客使用:注册.发布博客.博客管理练习 Github使用:注册.文件同步等练习 2. 要求 (总体作业要求参考[链接]) 发布一篇博客: 介 ...

随机推荐

  1. Python网络爬虫之requests模块(2)

    session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 有些时候,我们在使用爬 ...

  2. Keepalived 进程无法关闭

    操作系统:SLES12sp2 keepalived版本:1.2.12 问题描述: 使用命令"systemctl stop keepalived" 无法关闭keepalived,使用 ...

  3. 如何让jpa 持久化时不校验指定字段

    源文:https://www.toocruel.net/jpa-validate/ 怎么让jpa 持久化时不校验指定字段 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥 ...

  4. 测试那些事儿—BUG

    一.作为测试人员,你应该这样报BUG: 不要对程序员说,你的代码有BUG. 他的第一反应是:1.你的环境有问题吧:2.你踏马到底会不会用? 如果你委婉的说:你这个程序和预期的不一样,你看看是不是我的方 ...

  5. Learning by doing——小黄杉获得感想

    突然想起来前一个月答应了栋哥要写一篇博客的,后来一直忙于复习就忘了. 不过答应了的事就要完成嘛. 获得感言 首先就是非常高兴的了,这也是对我的能力的一种肯定 这次的获得原因是期中考最快满分,emmm侧 ...

  6. 矩阵快速幂(以HDU1757为例)

    对于数据量大的求余运算,在有递推式的情况下,可以构造矩阵求解. A - A Simple Math Problem Lele now is thinking about a simple functi ...

  7. 一个简单的 openssl 示例

    ////生成一个私钥////$key=openssl_pkey_new();openssl_pkey_export($key,$out);//等于下面写入的内容////将私钥写入一个文件////ope ...

  8. 2018.4.2 flask web

    from flask import Flask,request from flask import jsonify from flask import render_template app = Fl ...

  9. 2017.4.5 OpenStack简介

    OpenStack 管理的资源不是单机的而是一个分布的系统,把分布的计算.存储.网络.设备.资源组织起来,形成一个完整的云计算系统:OpenStack 也提供一个 UI,这里包括一个图形化的 UI:H ...

  10. 【java多线程】线程状态分析

    一.java线程的状态 NEW: 新建状态,线程对象已经创建,但尚未启动 RUNNABLE:就绪状态,可运行状态,调用了线程的start方法,已经在java虚拟机中执行,等待获取操作系统资源如CPU, ...