将本地代码托管到github的流程总结(win和ubuntu系统同样使用)

版权声明:转载请标明出处,谢谢! https://blog.csdn.net/SimileciWH/article/details/83986575

                   !!!注意windows系统和ubuntu系统的使用方式是一样的,都是在命令行中执行一下操作!!!


1    注册你的github账号,并查看完教程

注册网址:https://github.com/

2    在github上添加一个新的库

如图1所示,

                                                                                                           图1 

                   

                                                                                                      图2

填写完成后,在Description下填写关于这个库的描述,对于Initialize this repository with a README这一栏,先不选,因为我习惯手动添加。

至此在github上的工作已经完成了。你会看到这样的界面,如图3所示,

                                                                                                 图3 

如图3所示,给出了3个提示,

  1. 在命令行创建一个新的库;
  2. 在命令行推送一个已经存在的库;
  3. 从其他库导入代码到这个库 ;

3    创建本地的ssh key,并在github上添加这个ssh key

使用过ssh的话,就知道为什么要创建这个ssh key了。因为是远程连接到github库,使用的是ssh工具,ssh远程登录有两种方式,一种是账号密码的方式,一种是ssh key的方式,ssh key的方式好处在于,当你申请连接远程的库时,github只需要检查你的你在github上的ssh key和你本地库中的ssh key是否匹配,这样就可以不需要输入账户密码这种繁琐的操作了。

所以,

3.1    在本地生成一个ssh key

$ ssh-keygen -t rsa -C "[email protected]"

这个过程中会提示:

$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/simileciwh/.ssh/id_rsa):     
Created directory '/home/simileciwh/.ssh'.    
Enter passphrase (empty for no passphrase):       
Enter same passphrase again:  
Your identification has been saved in /home/simileciwh/.ssh/id_rsa.  
Your public key has been saved in /home/simileciwh/.ssh/id_rsa.pub.  
The key fingerprint is:      
SHA256:d2NIXvUXvgAkghKBDjmN9rwQSjyRzSppTPfcSRVLBYQ [email protected]  
The key's randomart image is:     
+---[RSA 2048]----+  
|.=*oo ..+B=+  .. |  
|=X.= . Eo o .....| 
|OoB + o ... .. .o| 
|+* o o o o o  . o|  
|o . .   S + +  . | 
|   .     . o .   |  
|                 |  
|                 |
|                 | 
+----[SHA256]-----+  

此处,.ssh文件生成的位置,在命令行中会提,注意一下,找到他的文职就可。注意:.ssh文件夹时隐藏文件 

这是让你设置密码,如果直接回车的话,则密码为空,当然你也可以输入密码回车,系统会再次提醒你确认一次密码的

完成后,你会在  .ssh  文件夹下 找到如下文件

├── id_rsa
├── id_rsa.pub
└── known_hosts

0 directories, 3 files

打开id_rsa.pub文件,复制里面的所有内容。

3.2    在github上保存本地生成的ssh key

操作如下图4,5所示,

                                                                                                   图4

                                                                                                   图5 

在图4中选择setting,在图5中选择左边的 SSH and GPG keys 然后选择add new key,即可。

将id_rsa.pub中的全部内容复制到key下,title随便命名。店家Add SSH key。就完成了本地和github上的ssh key的验证。

可以使用如下命令,查看你是否连接到了github。

 $ ssh -T [email protected]
>>
Hi SimileciWH! You've successfully authenticated, but GitHub does not provide shell access.

第一次连接时,会提示如下信息,

The authenticity of host 'github.com (13.229.188.59)' can't be established.

RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.

Are you sure you want to continue connecting (yes/no)?

输入yes回车即可。

这样提示,说明连接成功。 

3.3    第一次使用git需要初始化git,你的账户名和注册的邮箱:

$ git config --global user.name "your name"
$ git config --global user.email "[email protected]"

4    创建本地的github库与之对应

在本地进入,你想要托管的代码所在的文件夹举个例子

cd stdr_move_base  ##你要托管的代码所在的文件夹
git init           #初始化该文件夹,会在该文件夹下生成一个.git/的文件夹
git status         #查看该文件夹下未git的文件有哪些
git add .          #这里 add .是将所有文件都添加上传,你可一指定上传的文件git add file_name
git commit -m "2018-11-12 to backup my move base PKG on stdr simulation."
git remote add origin [email protected]:SimileciWH/stdr_simulation_move_base.git #连接到你刚刚创建的远程库,在图3中可以找到
git push -u origin master  #将代码推送上去,并且是推送到,master分支

执行效果如下图所示,

$ cd stdr_move_base/

$ git init 
>>
Initialized empty Git repository in /home/simileciwh/catkin_ws/src/stdr_move_base/.git/
$ git status 
>>
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	CMakeLists.txt
	config/
	launch/
	package.xml

nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -m "2018-11-12 backup my move_base PKG"
>>
[master (root-commit) 7c24ebb] 2018-11-12 backup my move_base PKG
 10 files changed, 454 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 config/costmap_common_params.yaml
 create mode 100755 config/dwa_local_planner_params.yaml
 create mode 100644 config/global_costmap_params.yaml
 create mode 100755 config/global_planner_params.yaml
 create mode 100644 config/local_costmap_params.yaml
 create mode 100755 config/move_base_params.yaml
 create mode 100755 "config/move_base_param\345\217\202\346\225\260\350\257\264\346\230\216.yaml"
 create mode 100644 launch/stdr_move_base.launch
 create mode 100644 package.xml

$ git remote add origin [email protected]:SimileciWH/stdr_simulation_move_base.git

$ git push -u origin master 
>>
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (14/14), 6.61 KiB | 1.32 MiB/s, done.
Total 14 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/SimileciWH/stdr_simulation_move_base/pull/new/master
remote: 
To github.com:SimileciWH/stdr_simulation_move_base.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

至此,本地库已经推送到了github对应的库中,回到github刚刚创建的库中,刷新,就能看到刚刚上传的文件了。

如图6所示

                                                                                                 图6

5    你可能会遇到的问题

在执行git remote add origin ......时提示,fatal: remote origin already exists.如果你确定这个库之前是可空的库,只是你第一次推送的话,你可以执行删除命令,来解决这个问题。

git remote rm origin

6    当你对你的本地库完成修改后,想要提交到github远程库

执行以下指令

git status  #查看本地库和github中不同的部分或文件
git add .   #将这些修改的文件全部添加到github暂存区
git commit -m "msssages info"  #将暂存区文件上传到github仓库
git pull origin master  
git push origin master

7    怎么在github上删除不想要的仓库

如图7所示,点卡你的仓库,选择最右边的setting,

                                                                                             图7 

                                                                                                      图8 

点开后的页面一直拉到最下面,如图8所示,点击Delete this repository。会跳出如图9所示的窗口,

                                                    

                                                                                                      图9

在窗口内输入当前要删除的仓库的名字,然后点击 I understand the consequences, delete this repository。这样就会删除了! 

8    使用git log查看提交的历史信息

进入你本地的仓库,即带有.git文件夹的仓库,输入如下指令,可以看到,提交的历史信息。

$ git log
>>
commit 6a321fdd21c97d3eeb80db5459ec0f2a29f341b9 (HEAD -> master, origin/master)
Author: simileciWH <[email protected]>
Date:   Mon Nov 12 10:34:06 2018 +0800

    first commit

9    怎么从github库中download最新的修改到本地的库?

git fetch origin master
git log -p master..origin/master
git merge origin/master
或者直接
git pull  # 不推荐

git fetch origin master ----从远程的库的origin主分支下下载最新版本到本地的origin/master分支上

git log -p master ..origin/master ----查看本地的master和刚刚下载的origin/master有什么不同

git merge origin/master ----合并

猜你喜欢

转载自blog.csdn.net/SimileciWH/article/details/83986575
今日推荐