git使用

紧跟时代,学会使用git托管代码。

1、git安装

sudo apt-get install git

然后使用git命令查看各种命令。

2、在本机创建git仓库

在根目录下创建learngit仓库:mkdir learngit pwd:/home/lb/learngit

初始化:git init

创建一个txt文件:vi readme.txt 然后写一点东西进去

把文件添加到仓库:git add readme.txt    注意这里如果要添加很多个文件要调用很多次这个语句

把文件提交到仓库:git commit -m "wrote a readme file"   注意这里-m "wrote a readme file"的意思是给这次提交的文件打上一个说明,便于以后维护。同时commit可以一次提交很多个文件。

3、远程仓库

创建SSH Key:先进入 cd ~/.ssh,如果为空,输入命令:ssh-keygen -t -rsa -C "627136104@qq.com" 后面的邮箱就是github账号绑定的邮箱。这一步完成之后会生成id_rsa和id_rsa.pub两个文件。其中id_rsa是私钥,id_rsa.pub是公钥

在guthub上使用公钥:点击github-settings-SSH and GPG keys-New SSH key,将公钥添(id_rsa.pub中的字符串)加进去。

添加远程库:在github上新建一个repositories;在本地learngit仓库下输入:git remote add origin git@github.com:linbang/myfirstproject.git。

将本地库中内容推送到远程库:git push -u origin master,就可以了。

  • 注意这个步骤会报错: 

    To git@github.com:linbang/myfirstproject.git
    ! [rejected] master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:linbang/myfirstproject.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

          这个时候需要先执行:git pull --rebase origin master,然后git push -u origin master就可以了。或者直接 git push -f origin master都可以。推荐第一种。

参考:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

原文地址:https://www.cnblogs.com/boris1221/p/9498508.html