【Github】Github命令行快速使用教程及常见问题解决

[toc]

Git环境配置

第一步:

生成ssh key,使用命令 “ssh-keygen -t rsa -C “your_email@youremail.com“”,your_email是你的email默认在用户的家目录下.ssh/id_rsa.pub文件里面

第二步:

回到github,进入Account Settings,左边选择SSH Keys,Add SSH Key,title随便填,粘贴key。

第三步:

测试ssh key是否成功,使用命令“ssh -T git@github.com”,如果出现You’ve successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。

如果出现“Agent admitted failure to sign using the key.Permission denied (publickey).”这个错误的话,在命令行下执行”ssh-add”,这样就可以了。

第四步:

配置Git的配置文件,username和email

1
2
git config --global user.name "your name"   //配置用户名
git config --global user.email "your email" //配置email

clone至本地以及push到Github

clone

1
git clone https://github.com/chenguolin/scrapy.git
1
git pull origin master

push

1
git add xxx
1
git commit -m
1
git push origin master

代码冲突常见解决方法

如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候,

在发布这个配置文件的时候,会发生代码冲突:

error: Your local changes to the following files would be overwritten by merge:
protected/config/main.php
Please, commit your changes or stash them before you can merge.

如果希望保留生产服务器上所做的改动,仅仅并入新配置项, 处理方法如下:

git stash
git pull
git stash pop
然后可以使用Git diff -w +文件名 来确认代码自动合并的情况.

反过来,如果希望用代码库中的文件完全覆盖本地工作版本. 方法如下:

git reset –hard
git pull
其中git reset是针对版本,如果想针对文件回退本地修改,使用

1
git checkout HEAD file/to/restore  

英文版pull和push教程

In below section, I am writing step by step how I pushed the code in Github repo successfully
Follow the given below steps from where(Desktop/Server) you want to push the code into Github repo :

Step 1:

Clone the github repo into your system. (You should first create a repo in Github)

Get repo url at right hand sidebar of your repo page. Replace your github repo’s HTTPS url with https://github.com/USER-NAME/REPO-NAME.git

1
git clone https://github.com/USER-NAME/REPO-NAME.git

For EXAMPLE:

1
git clone https://github.com/sharadchhetri/testrepo.git

After cloning will be finished, you will find the directory name matching with the name of Github repo name you just cloned.

Step 2 : (Unnecessary)

Copy your code or files to REPO-NAME directory

For example, I have saved my codes in my desktop at /home/sharad/MyCode directory. Here, I am copying all codes and child directories into REPO-NAME directory .

1
cp -rvf /home/sharad/MyCode/* /home/sharad/testrepo

Step 3 :

Change to git cloned directory i.e REPO-NAME.

1
cd REPO-NAME

Step 4 :

Set Github account username and email id for authentication

1
2
git config --global user.email "your-email-id@example.com"
git config --global user.name "Your-Github-UserName"

Step 5:

Set Github URL. (This section is actually solution of this problem)

1
git remote set-url origin https://Your-Github-UserNAme@github.com/Your-Github-UserNAme/REPO-NAME

For EXAMPLE.

1
git remote set-url origin https://sharadchhetri@github.com/sharadchhetri/testrepo.git

Step 6:

Now add all files and directory by using below given command inside REPO-NAME directory
(There is a dot in command, can you see that!)

1
git add .

Step 7:

Now write comment on commit

1
git commit -m 'my first commit'

Step 8:

Now push the code in Github repo.
Be ready with your github account password because it will ask.

1
git push -u origin master

I hope the solution also work for you.