一、使用git在本地创建一个项目的过程,Git 上传本地文件到github
$ makdir ~/hello-world //创建一个项目hello-world 
$ cd ~/hello-world //打开这个项目
$ git init //初始化
$ git add . //提交所有文件
$ touch README $ git add README //更新README文件
$ git commit -m ‘first commit’ //提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:xxxx/hello-world.git //连接远程github项目
$ git push -u origin master //将本地项目更新到github项目上去

二、操作笔记

删除文件:

rm filename 本地删除

-> git commit -m “remarks info”  提交,彻底删除,版本库删除

-> git checkout filename               撤销,恢复

远程仓库:本地Git仓库和github仓库关联

github账号

创建SSH Key

查看是否有id_rsa和id_rsa.pub: cd ~/.ssh/   ls -l

不存在-> ssh-keygen -t rsa –C “youremail@example.com”

分支

查看分支:git branch

创建分支:git branch branch-name

切换分支:git checkout branch-name

创建+切换分支:git checkout –b branch-name

合并某分支到当前分支:

(1) git merge branch-name (默认,fast forward  模式下,删除分支后,会丢掉分支信息)

(2) git merge –no-ff -m “注释” branch-name (禁用 fast forward )

删除分支:git branch –d branch-name

推送分支到远程库:git push origin branch-name

查看远程库信息 : git remote / git remote -v (详细信息)

查看当前状态:git status

查看历史信息:git log / git reflog

查看分支树形图: git log —graph —pretty=oneline —abbrev-commit

版本回退:

git reset —hard 版本号

git reset —hard HEAD^ (^的个数 === num)

git reset —hard HEAD~num (num 前几版本)

git reset HEAD filename 暂存区-> 工作区 HEAD最新版本

git checkout  — filename  丢弃工作区修改

中断此时任务,切换到其他分支工作

git stash 隐藏工作现场

git stash list 列表工作现场

git stash pop = git stash apply 恢复现场 + git stash drop 删除stash暂存任务

三、一些可能遇到的问题解决:
(1)如果输入$ git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git
提示出错信息:fatal: remote origin already exists.
解决办法如下:
1、先输入$ git remote rm origin
2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!
3、如果输入$ git remote rm origin 还是报错的话,error: Could not remove config section ‘remote.origin’. 我们需要修改gitconfig文件的内容
4、找到你的github的安装路径,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
5、找到一个名为gitconfig的文件,打开它把里面的[remote "origin"]那一行删掉就好了!
(2)如果输入$ ssh -T git@github.com
出现错误提示:Permission denied (publickey).因为新生成的key不能加入ssh就会导致连接不上github。
解决办法如下:
1、先输入$ ssh-agent,再输入$ ssh-add ~/.ssh/id_key,这样就可以了。
2、如果还是不行的话,输入ssh-add ~/.ssh/id_key 命令后出现报错Could not open a connection to your authentication agent.解决方法是key用Git Gui的ssh工具生成,这样生成的时候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行来做。
3、最好检查一下在你复制id_rsa.pub文件的内容时有没有产生多余的空格或空行,有些编辑器会帮你添加这些的。

(3)如果输入$ git push origin master
提示出错信息:error:failed to push som refs to …….
解决办法如下:
1、先输入$ git pull origin master //先把远程服务器github上面的文件拉下来
2、再输入$ git push origin master
3、如果出现报错 fatal: Couldn’t find remote ref master或者fatal: ‘origin’ does not appear to be a git repository以及fatal: Could not read from remote repository.
4、则需要重新输入$ git remote add origingit@github.com:djqiang/gitdemo.git (4) 从服务器获取更新
git status 是一个很有用的命令,用来查看本地repository的状态,它会显示文件的新增/修改/删除的状态。

  如果服务端有更新,git status也会有相应的提示。

D:\Dev\Github\bid>git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

nothing to commit, working directory clean

  上面的显示的意思就是,有一个更新还没有反应到我本地来,可能是别人往server上checkin了一点东西。 使用git pull命令拿这些更新到本地来。

D:\Dev\Github\bid>git pull
Updating abf79f6..db7b6e3
Fast-forward
routes.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)


  如果服务端和本地文件都做了改动,使用git pull时就会提示冲突:

D:\Dev\Github\bid>git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/*****/***
db7b6e3..211ff7f master -> origin/master
Updating db7b6e3..211ff7f
error: Your local changes to the following files would be overwritten by merge:
routes.js
Please, commit your changes or stash them before you can merge.
Aborting

  这时候推荐手动检查一下文件的版本,到底需要哪个。有一个git mergetool可以帮助merge code。

  使用git checkout可以丢弃掉本地的改动,然后使用git pull去拿server上的最新更新。

    丢弃一个文件的所有改动:

git checkout xx.js

  然后再使用git pull,就可以拿到server上的版本的代码了。

 (5)本地没有update到最新版本的项目(git上有README.md文件没下载下来)

    本地直接push所以会出错:

    ! [rejected] master -> master (fetch first)

    error: failed to push some refs to 'git@github.com:xxxx/xxxxx.git'

    解决方法: git pull --rebase origin master 再 git push -u origin master

github一般操作流程(新建上传、追加)

##Create a new repository on the command line
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:teamtogether/TestProject.git
git push -u origin master ##Push an existing repository from the command line
git remote add origin git@github.com:teamtogether/TestProject.git
git push -u origin master

 附上一张git操作图:

git 操作遇到的问题与解决方法的更多相关文章

  1. sqlite:多线程操作数据库“database is locked”解决方法(二)

    上一篇博客<sqlite:多线程操作数据库“database is locked”解决方法>通过注册延时函数的方法来处理数据库被锁的问题.此方法固然能解决问题,但是在多个线程向数据库写入大 ...

  2. git pull“No remote repository specified”解决方法

    git pull“No remote repository specified”解决方法 学习了:http://www.paopaoche.net/jiaocheng/77226.html 修改“.g ...

  3. OS X升级El Capitan后,git difftool无法打开diffmerge的解决方法

    在git项目下执行git difftool,出现如下报错 /Library/Developer/CommandLineTools/usr/libexec/git-core/mergetools/dif ...

  4. Hadoop中操作HDFS出现异常的解决方法

    Hadoop环境搭建成功后,一般会运行一个小例子,这时候就涉及到了对HDFS文件系统的操作,对于刚开始学习Hadoop的初学者一般会多次的进行name节点的格式化操作,最后导致上传文件会抛出异常,通过 ...

  5. 升级了git版本后git clone报ssl错误的解决方法

    由于升级了git版本,git clone 的时候报了如下的错误 fatal: unable to access 'https://github.com/open-falcon/falcon-plus. ...

  6. js操作改变原数组的解决方法

    最近在开发的时候发现js中的循环操作会改变原数组,var一个变量承接也不行 甚至连map方法都会改变原数组,下面是解决方法 let a = ['a','b','c'] let b = [[2, 0, ...

  7. 误操作yum导致error: rpmdb解决方法

    错误:[root@test ~]# yum makecache error: rpmdb: BDB0113 Thread/process 18967/139716328294400 failed: B ...

  8. ---解决git pull 后出现冲突的解决方法

    0. git statusOn branch masterYour branch and 'origin/master' have diverged,and have 1 and 3 differen ...

  9. 虚拟机出现“操作文件.PhysicalDrive1失败”的解决方法

    今天打算利用U盘给虚拟机装系统做实验,中途遇到了"操作文件.PhysicalDrive1失败"的错误,试了网上的方法都没有成功,最后自己试了很久总算弄出来了.鉴于本人的基础水平有限 ...

随机推荐

  1. Hello Kotlin! Kotlin学习资料

    今天谷歌搞了条大新闻.宣布Kotlin成为android开发的一级(One Class)语言,这说明谷歌是被甲骨文恶心坏了,打算一步步脱离掉java或者说是甲骨文公司的束缚了.原先网上大家还琢磨着会不 ...

  2. Spark Streaming的优化之路—从Receiver到Direct模式

    作者:个推数据研发工程师 学长     1 业务背景   随着大数据的快速发展,业务场景越来越复杂,离线式的批处理框架MapReduce已经不能满足业务,大量的场景需要实时的数据处理结果来进行分析.决 ...

  3. textarea组件

    textarea组件:多行输入框:(文本域) textarea组件属性: value:类型 字符串 输入框的内容 placeholder:类型 字符串 输入框为空时的占位符 placeholder-s ...

  4. JavaVM & JNIEnv

    JNIEnv提供了大多数的JNI函数.你的本地方法都会接收JNIEnv作为第一个参数.JNIEnv用于本地线程存储.因此,你不能在线程间共享同一个JNIEnv.如果一个代码段没有其他方式获取它自身线程 ...

  5. 005-unity3d 添加背景音乐、音效 以及 天空盒子

    一.基础知识 1.项目中需要有AudioListener,播放器中播放的声音就是AudioListener组件坐在的位置听到的声音.默认AudioListener是放到Main Camera上.没有A ...

  6. SharpBrowser

    SharpBrowser is the fastest open source C# web browser there is! Slightly faster than Google Chrome ...

  7. spring-data-elasticsearch使用出现的一些小问题

    问题一failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MapperParsingException: No ty ...

  8. Robot Framework课件汇总

    http://www.testclass.net/rf/ 测试教程网http://www.testclass.net/all

  9. oracle--权限的传递

    sys 用户 普通授权lisi grant alter any table to lisi; 将权限指定admin ,可以权限传递给其他用户 grant alter any table to lisi ...

  10. C#网络编程 多线程和高并发

    在任何 TCP Server 的实现中,一定存在一个 Accept Socket Loop,用于接收 Client 端的 Connect 请求以建立 TCP Connection. 在任何 TCP S ...