Git GitHub Gitee使用方法

作者

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 submodule update出错提示子模组未对路径注册】
Git修改提交的仓库地址
git 撤销远程的上次提交(撤销远程服务器如GitHub gitlab上的提交)
git生成patch和打patch
git出现Your branch and ‘origin/master’ have diverged解决方法
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**apple@me.com"

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

创建本地仓库

在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 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 <file>..." to unstage)
...
Untracked files:
  (use "git add <file>..." to include in what will be committed)
...

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

撤销操作

参考GitHub撤销修改git reset –soft/--hard head_id,使用git reset–soft(两个-)表示只是改变了HEAD的指向,本地代码不会变化,–hard直接回改变本地源码,一般直接使用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 git@github.com: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 <command> [<revision>...] -- [<file>...]'

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

或者采用,

$ git log
commit a2f9c9b1f6adc05a43961cecd9fdfb03b1232c84
Author: Mr.BangBangBabe <zhuce19940828@me.com>
Date:   Sat Apr 24 00:02:50 2021 +0800

    add rs485 code for c51

commit acd5a9ef5335e0733296947d06ff4e0d62e3cc68
Author: Mr.BangBangBabe <zhuce19940828@me.com>
Date:   Wed Apr 21 01:57:48 2021 +0800

    add rs485 proc
# 将代码强制切换到某个commit id所处的状态
$ git reset --hard a2f9c9b1f6adc05a43961cecd9fdfb03b1232c84

拉取操作的时候出现,

$ git pull -f
remote: Enumerating objects: 84, done.
remote: Counting objects: 100% (83/83), done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 49 (delta 43), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (49/49), done.
From gitee.com:qingermaker/eih
 + 54ef954...9fb45c7 master     -> origin/master  (forced update)
error: Your local changes to the following files would be overwritten by merg
        src_disp/main.c
Please commit your changes or stash them before you merge.
Aborting
$ git fetch origin
$ git reset --hard origin/master
HEAD is now at 9fb45c7 update adc code for LCD_LED

恢复

误删除文件,且没有提交,可以使用git checkout

$ git checkout -- c51/dcl.uvopt
$ git checkout -- c51/dcl.uvproj

中文乱码

右键菜单Options设置UTF-8,执行git config --global core.quotepath false

分支

$ git branch -a # 显示所有分支
$ git branch <分支名> # 切换到分支
$ git branch -d <分支名> # 删除分支
$ git checkout -b <本地分支名> <远程分支名origin/xxxx>

patch

git diff Test.java > test.patch
git format-patch <commit tag>
git apply xxx.patch # 对应git diff生成的patch
git am 0001-limit-log-function.patch # 对应git format-patch生成的patch
# .git/rebase-apply/patch:370: trailing whitespace.
# warning: 1 line adds whitespace errors.
git am --whitespace=fix *.path

配置代理

http
$ git config --global http.proxy http://110.110.1.70:1080
$ git config --global https.proxy http://110.110.1.70:1080
$ git clone https://github.com/Xilinx/linux-xlnx.git
socks5
git config --global http.proxy ‘socks5://127.0.0.1:1080’ (这条即可)
git config --global https.proxy ‘socks5://127.0.0.1:1080’

GitHub

新建GitHub仓库

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

配置

本地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**apple@me.com"
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文件的内容。
这里写图片描述
操作完之后。
这里写图片描述
测试一下,

$ ssh -T git@github.com
Hi zhuzhu2009! You've successfully authenticated, but GitHub does not provide shell access.

同步git到本地

使用git clone

aj@aj-PC MINGW64 /f/xilinxlinux
$ git clone git@github.com: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.

对于有submodule的,

$ git submodule update --init --recursive

上传到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
...

撤销操作

参考Git撤销操作,恢复本地代码,如果本地没有执行任何操作,使用,

$ git revert HEAD
$ git push origin master

如果本地已经reset了,并重新commit了,使用,

git push origin master -f

删除GitHub仓库

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

Raw配置

需手动配置raw IP,打开网站ipaddress,搜索raw.githubusercontent.com,配置host文件,

# C:\Windows\System32\drivers\etc\hosts
# GitHub raw & imag
199.232.68.133 raw.githubusercontent.com

在Raw按钮上邮件链接另存为即可下载单个文件。

修改仓储地址

修改.git目录下的config文件。

强制更新到远程

$ git fetch --all
$ git reset --hard origin/master
$ git pull

Gitee

配置

添加SSH,C:\Users\**\.ssh\id_isa.pub
150新建仓库,选择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 git@gitee.com:qingermaker/recorder.git
git push -u origin master
已有仓库?

cd existing_git_repo
git remote add origin git@gitee.com:qing***/re***.git
git push -u origin master

clone需要用户名和密码,

$ git clone https://gitee.com/qing***/re***.git
正克隆到 'recorder'...
Username for 'https://gitee.com': qing***
Password for 'https://qing***@gitee.com': 
remote: Enumerating objects: 408, done.
remote: Counting objects: 100% (408/408), done.
remote: Compressing objects: 100% (403/403), done.
remote: Total 408 (delta 184), reused 123 (delta 4), pack-reused 0
接收对象中: 100% (408/408), 7.62 MiB | 1.07 MiB/s, 完成.
处理 delta 中: 100% (184/184), 完成.

多账户设置

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

更换远程仓库

报错# fatal: remote origin already exists,需要先rm,

$ git remote rm origin
$ git remote add origin git@gitee.com:***/blog.git

常见错误

readme无法换行

空格两个及以上,再回车

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 submodule update出错提示子模组未对路径注册或者请确认您有正确的访问权限并且仓库存在

github克隆项目中的子模块submodule时遇到的问题

vi .gitmodules,统一url格式是https还是git,然后执行git submodule sync再继续之前的操作。

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 git@github.com:****.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 <remote> <branch>

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

    git branch --set-upstream-to=origin/<branch> 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的哪些节点都消失了。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要同时配置gitgithubgitee,您需要按照以下步骤操作: 1. 在githubgitee上创建账户并创建仓库。 2. 在本地计算机上安装git。 3. 打开终端或命令行窗口,输入以下命令来配置gitgit config --global user.name "Your Name" git config --global user.email "youremail@example.com" 这将设置您的用户名和电子邮件地址,以便在提交代码时进行身份验证。 4. 生成SSH密钥并将其添加到您的githubgitee账户中。在终端或命令行窗口中输入以下命令: ssh-keygen -t rsa -C "youremail@example.com" 然后按照提示操作,将生成的公钥添加到您的githubgitee账户中。 5. 在本地计算机上创建一个新的git仓库,并将其与githubgitee仓库关联。在终端或命令行窗口中输入以下命令: git init git remote add origin git@github.com:yourusername/yourrepository.git git remote set-url --add origin git@gitee.com:yourusername/yourrepository.git 这将创建一个新的git仓库,并将其与您的githubgitee仓库关联。 6. 将您的代码添加到git仓库中,并将其推送到githubgitee仓库中。在终端或命令行窗口中输入以下命令: git add . git commit -m "Initial commit" git push -u origin master 这将将您的代码添加到git仓库中,并将其推送到您的githubgitee仓库中。 现在,您已经成功地配置了gitgithubgitee,并可以开始使用它们来管理您的代码了。 ### 回答2: Git是一款非常好用的版本管理系统,我们可以在其中配置多个远程仓库。这意味着我们可以在同一个本地Git仓库中,同时管理多个远程仓库,以实现不同平台之间的代码同步。 当我们使用Git时,首先需要在本地配置git的全局用户信息,如下所示: ``` $ git config --global user.name "Your Name" $ git config --global user.email "youremail@example.com" ``` 之后,我们需要在本地Git仓库中配置远程仓库的地址。假设我们要同时配置GitHubGitee,那么我们需要执行以下命令: ``` $ git remote add github https://github.com/yourusername/repo.git $ git remote add gitee https://gitee.com/yourusername/repo.git ``` 其中,"yourusername"是你在对应平台上的用户名,"repo.git"是你的仓库名称。执行以上命令后,Git会在本地仓库中添加两个远程仓库:githubgitee。 接下来,我们可以使用push命令将本地代码同步到远程仓库。如果我们要将代码同步到GitHubGitee,可以使用以下命令: ``` $ git push github master $ git push gitee master ``` 其中,"master"是分支名称,你也可以更改为其他分支名称。 除了push命令,我们还可以使用pull、fetch等命令从远程仓库获取代码或将远程仓库的代码合并到本地仓库中。 需要注意的是,当同一个本地仓库同时配置多个远程仓库时,你需要决定将哪一个仓库设为默认仓库,以防止误操作。你可以使用以下命令来设置默认仓库: ``` $ git push -u github master ``` 以上命令中的"-u"选项表示将github设置为默认仓库,以后在执行push命令时,就可以不用指定远程仓库的名称了。 综上所述,Git同时配置GitHubGitee非常简单,只需要执行几个简单的命令即可。只要你掌握了以上知识,就可以轻松实现多平台代码同步。 ### 回答3: Git 是一款强大的版本控制工具,可以为项目的开发和管理带来很多便利。在开发过程中,我们通常会将代码托管到代码托管平台上,比如 GithubGitee。为了更好地管理Git项目,我们需要配置 Git,以便同时连接 GithubGitee 两个平台。 首先,我们需要在GithubGitee上分别创建账号并登录。然后,我们需要生成 SSH key 并将其添加到 GithubGitee 上,以便在上传代码时进行身份验证。我们可以通过以下命令来生成 SSH key : ``` ssh-keygen -t rsa -C "your_email@example.com" ``` 生成成功后,我们需要在 GithubGitee 上的个人设置页面中添加 SSH key。 接下来,我们需要在本地以 Git Bash 的形式打开 Git ,在控制台中输入如下命令,来配置 GithubGitee : ``` git config --global user.name "用户名" git config --global user.email "邮箱地址" ``` 具体的用户名和邮箱地址需要根据自己的账号信息进行设置。这些信息会在用 Git 上传代码时作为身份信息,所以一定要输入正确的信息。 在 GithubGitee 的 repository 页面上,我们可以找到 Clone or download 按钮,在弹出的框中复制仓库的 SSH 地址。然后,我们可以使用如下命令克隆代码到本地: ``` git clone git@github.com:yourname/repository.git git clone git@gitee.com:yourname/repository.git ``` 其中,yourname 和 repository 分别是 GithubGitee 账号中相应的用户和仓库名。 当我们需要 Push 代码时,需要指定远程仓库的名字。我们可以使用如下命令添加远程仓库: ``` git remote add gitee git@gitee.com:yourname/repository.git git remote add github git@github.com:yourname/repository.git ``` 其中,giteegithub 分别是远程仓库的名字。我们可以使用如下命令将代码 Push 到对应的平台上: ``` git push gitee master git push github master ``` 以上就是同时配置 GithubGitee 的全部过程。在开发过程中,我们应当及时 Pull 远程仓库的代码,避免产生代码冲突。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值