Git

Git

官网地址

https://git-scm.com/downloads

安装

apt install git
git --version

git config --global user.name "sweet"
git config --global user.email "root@xxx.net"

使用

git config --list
git remote rm origin

开始一个工作区(参见:git help tutorial)
clone 克隆一个仓库到一个新目录
init 创建一个空的 Git 仓库或重新初始化一个已存在的仓库

在当前变更上工作(参见:git help everyday)
add 添加文件内容至索引
mv 移动或重命名一个文件、目录或符号链接
reset 重置当前 HEAD 到指定状态
rm 从工作区和索引中删除文件

检查历史和状态(参见:git help revisions)
bisect 通过二分查找定位引入 bug 的提交
grep 输出和模式匹配的行
log 显示提交日志
show 显示各种类型的对象
status 显示工作区状态

扩展、标记和调校您的历史记录
branch 列出、创建或删除分支
checkout 切换分支或恢复工作区文件
commit 记录变更到仓库
diff 显示提交之间、提交和工作区之间等的差异
merge 合并两个或更多开发历史
rebase 在另一个分支上重新应用提交
tag 创建、列出、删除或校验一个 GPG 签名的标签对象

协同(参见:git help workflows)
fetch 从另外一个仓库下载对象和引用
pull 获取并整合另外的仓库或一个本地分支
push 更新远程引用和相关的对象

命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些概念帮助。
查看 'git help <命令>' 或 'git help <概念>' 以获取给定子命令或概念的帮助。

分支与合并

分支在本地完成,速度快。要创建一个新的分支,我们使用branch命令。
git branch test

branch命令不会将我们带入分支,只是创建一个新分支。所以我们使用checkout命令来更改分支。
git checkout test

第一个分支,或主分支,被称为"master"。
git checkout master

对其他分支的更改不会反映在主分支上。如果想将更改提交到主分支,则需切换回master分支,然后使用合并。
git checkout master
git merge test

如果您想删除分支,我们使用-d标识。
git branch -d test

刚创建的github版本库,在push代码时遇到以上错误:

Updates were rejected because the tip of your current branch is behind

有如下几种解决方法:
1,push前先将远程repository修改pull下来
$ git pull origin master
$ git push -u origin master

2,使用强制push的方法:
$ git push -u origin master -f
这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。

3,若不想merge远程和本地修改,可以先创建新的分支:
$ git branch [name] 创建新的分支
$ git push -u origin [name] 提交对分支

4.新版git
git pull origin master --allow-unrelated-histories
git push -u origin master

intellij idea 出现Push rejected的解决方法

这个问题主要是初次上传到远程仓库,远程仓库需要先拉下来跟本地合并,然后才能上传。
git pull
git pull origin master
git pull origin master –allow-unrelated-histories
查看状态信息
git status
检查远程仓库配置
git remote -v
如果远程仓库信息有误,则删除本地仓库配置,并且设置相关地址

git remote rm origin
git remote add origin XXXX

当执行git中的“git pull origin master –allow-unrelated-histories”命令时,会出现“ couldn’t find remote ref –allow-unrelated-histories”的错误,输入如下命令即可解决:
git pull –rebase origin master
强制覆盖已有的分支(可能会丢失改动)
git push -u origin master -f

如何从从detached HEAD状态解救出来

  • 基于本次提交创建临时分支
    输入 $git branch temp fef4501 使用git branch 分支名 操作ID 这句命令能够创建一个新的分支,但要注意此时我们还没有切换到这个分支上,这个分支上面代码跟我刚才提交完之后的一样.
  • 切换到工作分支并合并代码
    输入 $git checkout ask_11_16 意味着我已经切换到ask_11_16分支,这个分支是我之前想要提交的分支. 然后 $git merge temp 这行命令过后我们已经上次commit合并到ask_11_16上了,此时终端状态为 Your branch is ahead of ‘origin/ask_11_16’ by 1 commit. 我们只需要$git push即可把本次提交push到远程分支. 这时候检查代码,perfect!正式我们想要的状态.
  • 删除temp分支
    大功告成,至于temp分支已经没有了利用价值,本着过河拆桥的精神我不得不输入 $git branch -d temp 来删除temp分支.

分支管理与合并(merge与rebase)

gitee.com

创建 git 仓库:
mkdir eureka-client
cd eureka-client
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/xxx/eureka-client.git
git push -u origin master
已有仓库?
cd existing_git_repo
git remote add origin https://gitee.com/xxx/eureka-client.git
git pull origin master
git push -u origin master
git push -u origin master -f (强制上传会造成服务器的文件丟失,只适合初始化使用)

分支提交合并
在码云上先新建分支 test
git pull 本地获取同步服务器上的分支
git checkout test 切换支新的分支
git push  提交保存到新的分支
确认新功能开发完成后,切换回主分支,合并后再提交
git checkout master
git merge origin/test
git push

Gitlab Create a new repository

git clone http://xxx/root/test.git
cd test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Push an existing folder

cd existing_folder
git init
git remote add origin http://xxx/root/test.git
git add .
git commit -m "Initial commit"
git push -u origin master

Push an existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin http://xxx/root/test.git
git push -u origin --all
git push -u origin --tags

You must be logged in to post a comment