Git GitHub使用方法

作者

QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118

参考

GitHub Help
Git教程
使用Gitee
Git冲突:commit your changes or stash them before you can merge.
Git多账户切换配置
git 配置多个账户

安装Git

百度下载Git Windows安装包,安装即可,安装完打开Git Bash,配置Git。

zc@DESKTOP-KVKC06A MINGW64 ~
$ git config --global user.name "q***sina"

zc@DESKTOP-KVKC06A MINGW64 ~
$ git config --global user.email "z**[email protected]"

注意git config命令的–global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

创建Git本地仓库

在Git Bash中进入代码工程目录,使用cd命令。执行git init创建版本库。

zc@DESKTOP-KVKC06A MINGW64 ~
$ cd /c/project/****/

zc@DESKTOP-KVKC06A MINGW64 /c/project/****
$ ls
****/  ****.sdf

zc@DESKTOP-KVKC06A MINGW64 /c/project/****
$ git init
Initialized empty Git repository in C:/project/****/.git/

Git提交到本地仓库

git status查看文件状态,git add添加文件,git commit提交到本地仓库。

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git add *.h *.c *.cpp *.rc *.inf *.md sources
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory.

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git status
On branch master
...
Changes to be committed:
  (use "git rm --cached ..." to unstage)
...
Untracked files:
  (use "git add ..." to include in what will be committed)
...

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git commit -m "Initial commit"

新建GitHub仓库

我这里用的GitHub,GitHub首页自带英文教程,读一下指导,
Git GitHub使用方法_第1张图片
进入Guide界面,
Git GitHub使用方法_第2张图片
点击GitHub Help,
Git GitHub使用方法_第3张图片
有了这些帮助文档就好办了,点击Start a Project开始工程,或者右上角的加号。
Git GitHub使用方法_第4张图片
选择了README或license这样创建的仓库就不是空的了,有初始化文件。

GitHub

配置

本地Git仓库和GitHub之间的传输是通过SSH加密的,所以需要设置SSH key,如果不设置则报错Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.,一路回车默认值。

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ ssh-keygen -t rsa -C "z**[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zc/.ssh/id_rsa):
Created directory '/c/Users/zc/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/zc/.ssh/id_rsa.
Your public key has been saved in /c/Users/zc/.ssh/id_rsa.pub.
...

在用户主目录里找到.ssh目录,里面id_rsa是私钥,id_rsa.pub是公钥,在GitHub打开Account settings,SSH Keys页面,点Add SSH Key,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容。
Git GitHub使用方法_第5张图片
操作完之后。
Git GitHub使用方法_第6张图片
测试一下,

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

同步git到本地

使用git clone

aj@aj-PC MINGW64 /f/xilinxlinux
$ git clone [email protected]:xxxx/xxxx.git
Cloning into 'xxxx'...
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 12 (delta 3), pack-reused 0
Receiving objects: 100% (12/12), 4.39 KiB | 0 bytes/s, done.
Resolving deltas: 100% (3/3), done.

fatal: refusing to merge unrelated histories

由于新建GitHub仓库的时候,仓库不是空的,此时若是把本地仓库提交到GitHub就会有冲突,所以先把远程仓库同步到本地。中间出错参考git无法pull仓库refusing to merge unrelated histories和多人协作。出现错误:fatal: refusing to merge unrelated histories,执行git pull --allow-unrelated-histories,提交的时候出现vi编辑器,输入comment。

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git remote add origin [email protected]:****.git

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git pull
The authenticity of host 'github.com (13.250.177.223)' can't be established.
...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
warning: no common commits
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
...
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull  

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/ master


zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git pull origin master
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
...
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git pull
fatal: refusing to merge unrelated histories

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git pull --allow-unrelated-histories
Merge made by the 'recursive' strategy.
 LICENSE | 674 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 674 insertions(+)
 create mode 100644 LICENSE

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git push -u origin master
Counting objects: 23, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (23/23), done.
Writing objects: 100% (23/23), 36.07 KiB | 0 bytes/s, done.
Total 23 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
...

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git status
...

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git add *.md
...

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git commit -m "change README.md"
...

zc@DESKTOP-KVKC06A MINGW64 /c/project/**** (master)
$ git push origin master
...

commit your changes or stash them before you can merge

首先关闭占用的文件,强制更新到本地,之后可以把stash清空。

$ git stash
$ git pull
  • git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
  • git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
  • git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
  • git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

上传到GitHub

git三连:添加文件,提交文件,同步到github,

  1. git status
  2. git add xxx
  3. git commit -m “xxx”
  4. git push -u origin master/git push origin master
aj@aj-PC MINGW64 /f/qt(master)
$ git status
...
aj@aj-PC MINGW64 /f/qt(master)
$ git add i* m*
...
aj@aj-PC MINGW64 /f/qt(master)
$ git commit -m "list widget modify"
...
aj@aj-PC MINGW64 /f/qt(master)
$ git push -u origin master
...

撤销操作

参考GitHub撤销修改,git reset –soft/--hard head_id,使用git reset–soft(两个-)表示只是改变了HEAD的指向,本地代码不会变化,–hard直接回改变本地源码。

aj@aj-PC MINGW64 /f/xilinxlinux/shall (master)
$ git reflog
785d7a7 HEAD@{0}: reset: moving to 785d7a7b66ff6adcb5a2cf0cba8d5df8ca6ee10e
785d7a7 HEAD@{1}: commit: add cmem
f73755d HEAD@{2}: commit: add cmem
869bf6c HEAD@{3}: commit: add build-image.sh add cmem
1a67ab0 HEAD@{4}: commit: add hsi dt
7e773fe HEAD@{5}: commit: mmc ext4 rootfs
31b5ef9 HEAD@{6}: commit: add mwmstart.h
a81f4b3 HEAD@{7}: pull: Fast-forward
078f961 HEAD@{8}: reset: moving to HEAD
078f961 HEAD@{9}: pull: Fast-forward
982a90a HEAD@{10}: commit: update build-image.sh
46b4433 HEAD@{11}: commit (merge): nothing
391f18a HEAD@{12}: commit: update build-image.sh
343eabb HEAD@{13}: clone: from [email protected]:zhuzhu2009/shall.git

aj@aj-PC MINGW64 /f/xilinxlinux/shall (master)
$ git reset -soft HEAD-1
error: did you mean `--soft` (with two dashes ?)

aj@aj-PC MINGW64 /f/xilinxlinux/shall (master)
$ git reset --soft HEAD-1
fatal: ambiguous argument 'HEAD-1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git  [...] -- [...]'

aj@aj-PC MINGW64 /f/xilinxlinux/shall (master)
$ git reset --soft HEAD~1

删除GitHub仓库

点击进入Repositories(仓库),点击Settings,拖到网页最下方,点击删除,删除过程中会让你输入仓库名字确认删除。

Gitee

配置

添加SSH,C:\Users\**\.ssh\id_isa.pub
Git GitHub使用方法_第7张图片
新建仓库,选择SSH模式,不是HTTPS,

Git 全局设置:

git config --global user.name "Mr.Bang***"
git config --global user.email "zhu***@me.com"
创建 git 仓库:

mkdir recorder
cd recorder
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:qingermaker/recorder.git
git push -u origin master
已有仓库?

cd existing_git_repo
git remote add origin [email protected]:qingermaker/recorder.git
git push -u origin master

多账户设置

同时使用Github和Gitee的时候,有多个账号和邮箱,参考Git多账户切换配置,

常见错误

Please commit your changes or stash them before you merge.

参考博客Git冲突
参考博客Git:代码冲突常见解决方法

warning: LF will be replaced by CRLF

备份博客时发现所有文字显示成一行了。

git提示“warning: LF will be replaced by CRLF”的解决办法

你可能感兴趣的:(编程工具,git,GitHub)