git简易入门(github)

本文讲解下git的使用,包括使用git上传项目工程到github,以及错误解决。

1.安装git

使用apt-get安

sudo apt-get update 
sudo apt-get install git

使用下载安装

  1. 安装依赖库:
sudo apt-get update 
sudo apt-get install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
  1. 下载:
wget https://github.com/git/git/archive/v1.9.2.zip -O git.zip
unzip git.zip
cd git-*
  1. 安装:
make prefix=/usr/local all
sudo make prefix=/usr/local install
  1. 更新:
make prefix=/usr/local all
sudo make prefix=/usr/local install

2.配置你的github:

  1. 配置github的个人信息
git config --global user.name "Your Name" #名字随意
git config --global user.email "youremail@gmail.com"
  1. 查看配置信息和编辑:
#查看:
git config --list
#编辑配置信息:
sudo vim ~/.gitconfig
##可以修改的地方
[user]
       name = Your Name
       email = youremail@domain.com

3.创建公钥:

ssh-keygen -C 'you email address@gmail.com' -t rsa   #会在 用户目录 ~/.ssh/ 下建立相应的密钥文件
#上传公钥
在 github.com 的界面中 选择右上角的 Account Settings,然后选择 SSH Public Keys ,选择新加。
Title 可以随便命名,Key 的内容拷贝自 ~/.ssh/id_rsa.pub 中的内容,完成后,可以再使用

#测试:
ssh -v git@github.com
会返回提示信息:
Hi wpeace1212! You've successfully authenticated, but GitHub does not provide shell access.

4. 使用git

  1. 在想要上传的工程目录下建立README文件
  2. 初始化该目录:git init
  3. 新增文件到gi
#全部增加:
git add .
#指定增加:
git add filename #filename文件名
  1. 提交文件(本地)
#提交所有
git commit -m "Initial Commit" -a #m表示message , -a 表示所有
#提交特定文件
git commit -m "Initial Commit" file #file表示特定文件
  1. 提交到github:
#建立远程分支:第一次需要做
git remote add origin https://github.com/wpeace1212/BlogSource.git 
#https://github.com/wpeace1212/BlogSource.git 为你的工程url
#查看远程分支:
git remote -v
#提交你的代码:第二次提交时只要执行这条语句:
git push origin master

5.解决冲突和创建分支:

#查看所有分支:
git branch -a
#新建新的分支 other
git branch other
#切换到other
git checkout -b other
#在分支上提交工作:
git commit -m "other file" other
#合并分支
git merge

6.常见问题解决:

  1. remote origin already exists.
git remote add origin https://github.com/wpeace1212/BlogSource.git
   错误提示:fatal: remote origin already exists.
#解决办法:
git remote rm origin
再重新执行
  1. ! [rejected] master -> master (non-fast-forward)
git push origin master
  错误提示:failed to push som refs to.......
解决办法1:
git pull origin master
git push origin master
解决办法2:强制解决;
git pull
git push --force origin master

来自一条小鲨鱼(rlovep.com)

7.觉得很有用的一张图

1

原文地址:https://www.cnblogs.com/onepeace/p/4989602.html