Git 常用操作及技巧

Git 常用操作及技巧

Git下载传送门

官网下载太慢解决方法

https://npm.taobao.org/mirrors/git-for-windows/

一、Git 分支命令

  • 创建name的分支,并切换过去
git checkout -b name
  • 拉取远程分支,创建并切换到这个分支
git checkout -b name origin/remote-branch
git checkout -b 本地分支名 origin/远程分支名
  • 查看所有分支
git branch -v
  • 查看远程分支
git branch -r
  • 关联远程分支
git branch --set-upstream name origin/name
  • 删除分支
git branch -d name
git branch -D name
  • 关联远程仓库
git remote add origin git@github.com:YotrolZ/helloTest.git

//修改远程仓库地址 方法有三种:
// 1.修改命令
git remote origin set-url [url]

//2.先删后加
git remote rm origin
git remote add origin [url]

//3.直接修改config文件
  • Git 推本地 master 到远端非 master 分支
git push origin master:fix_branch

//强制提交
git push -f origin dev
合并分支
git checkout master
git merge name [-m "msg"]

git merge --no-ff branchname [-m "msg"]

git merge --no-ff 在每次合并都会产生一个新的合并记录;
git merge 的话只有解决冲突的时候才会产生一个新的合并记录。

  • 合并多个提交为一条 git merge --squash branchname(得到一个干净的分支)

    $ git merge --squash another
    $ git commit -m "message here"
    
分支推送
//本地新建分支后,可直接使用下面命令在远程仓库新建分支并将本地分支推送至新建分支
git push --set-upstream origin 分支名
修改分支名

默认配置下,当使用git push命令而没有明确的指名本地分支和远程参考分支的情况下,会有如下的提示**

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
//一般来说我们使用simple就可以进行正常的使用,如果严格一点儿可以用nothing
git config --global push.default simple

1.nothing
不推送任何东西并有错误提示,除非明确指定分支引用规格。强制使用分支引用规格来避免可能潜在的错误。
2.current
推送当前分支到接收端名字相同的分支。
3.upstream
推送当前分支到上游@{upstream}。这个模式只适用于推送到与拉取数据相同的仓库,比如中央工作仓库流程模式。
4.simple
​ 在中央仓库工作流程模式下,拒绝推送到上游与本地分支名字不同的分支。也就是只有本地分支名和上游分支名字一致才可以推送,就算是推送到不是拉取数据的远程仓库,只要名字相同也是可以的。在GIT2.0中,simple将会是push.defau认值。
simple只会推送本地当前分支。
5.matching
推送本地仓库和远程仓库所有名字相同的分支。

git pull 失败
如果远端库比当前库新,可选择强制推送
git push -f origin master
git pull 失败 ,提示:fatal: refusing to merge unrelated histories

其实这个问题是因为 两个 根本不相干的 git 库, 一个是本地库, 一个是远端库, 然后本地要去推送到远端, 远端觉得这个本地库跟自己不相干, 所以告知无法合并

具体的方法, 一个种方法: 是 从远端库拉下来代码 , 本地要加入的代码放到远端库下载到本地的库, 然后提交上去 , 因为这样的话, 你基于的库就是远端的库, 这是一次update了

第二种方法:
使用这个强制的方法

git pull origin master --allow-unrelated-histories

后面加上 --allow-unrelated-histories , 把两段不相干的 分支进行强行合并

使用第二种方法可能push失败
Your branch and 'origin/master' have diverged, and have 1 and 1 different commits each, respectively

解决方法:
使用如下命令:
git rebase origin/master 
然后使用
git pull --rebase 
最后使用
git push origin master 
把内容提交到远程仓库上。

二、Git 版本回退

找到要回退的版本的commit id:
git reflog 

回退
git reset --hard HEAD~1
git reset --mixed HEAD~2

三、config配置

1. Git 查看config配置

git config --list

2. Git 设置用户名密码

git config --global user.name [username]
git config --global user.email [email]

3. Git 中设置代理

git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

git config --global --unset http.proxy
git config --global --unset https.proxy

4.修改远程仓库地址

方法有三种:

1.修改命令

git remote origin set-url [url]

2.先删后加

git remote rm origin
git remote add origin [url]

3.直接修改config文件

四、子模块仓库submodule

https://www.jianshu.com/p/1598211659bc

1.添加 submodule

为当前工程添加submodule,命令如下:

git submodule add repo_url path

其中,repo_url 是指子模块仓库地址,path 指将子模块放置在当前工程下的路径。
注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能顺利 Clone)。

命令执行完成,会在当前工程根路径下生成一个名为“.gitmodules”的文件,其中记录了子模块的信息。添加完成以后,再将子模块所在的文件夹添加到工程中即可。

强制添加可以使用 --force 参数。

2.更新 submodule

更新当前仓库中的 submodule,命令如下:

git submodule update --init --recursive

参数 --recursive 的意思是递归更新子模块,也就是会更新子模块的子模块,如果不加这个参数,则只会更新第一级的子模块。

3.删除 submodule

删除当前仓库中的 submodule,方法如下:

  • 删除 .gitsubmodule 文件中对应的 submodule。

  • 删除 .git/config 中对应的 submodule 项(这一步我测试的时候如果不做也可以,后面再次添加子模块的时候加上 --force 参数也可以再次添加)。

  • 执行命令 git rm --cached <submodule_path>,执行命令前,请确保该 Git 仓库没有未提交的内容,否则会执行失败,示例如下:

git rm --cached hello

五、git与远程REPOSITORY同步TAG和BRANCH

参考如下地址:http://smilejay.com/2013/04/git-sync-tag-and-branch-with-remote/

六、 缓存区stash

  • 暂存所有更改到本地分支
git stash
  • 恢复
git stash pop
git stash apply stash@{2}
  • 查看现有stash
git stash list
  • 移除stash
#移除单个
git stash drop

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

#清除所有
git stash clear
  • 查看指定stash的diff
git stash show 

七、 忽略规则.gitignore

查看当前纳入版本库管理的文件
git ls-files
Git忽略规则:

在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如果没有这个文件,则需自己手工建立此文件)。这个文件每一行保存了一个匹配的规则例如:

# 此为注释 – 将被 Git 忽略

*.sample    # 忽略所有 .sample 结尾的文件
!lib.sample    # 但 lib.sample 除外
/TODO    # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt   # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
.gitignore规则不生效的解决办法

把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效,原因是.gitignore只能忽略那些原来没有被追踪的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未被追踪状态),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

八、clone从远程仓库获取所有分支

git clone ***
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

九、ssh-key生成

  • 打开终端,输入:
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"
  • 然后输入:cat ~/.ssh/id_rsa.pub 来查看你的SSH公钥,并把公钥复制下来

  • 打开gitee网站,在设置中找到安全设置下面找到SSH公钥,右边公钥里面粘贴刚才复制的value即可

  • 回到Bash,输入 “ssh -T git@gitee.com” 测试

操作失败Host key verification failed ,解決方法

提示

ey fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.Are you sure you want to continue connecting (yes/no)?

这句时,不能直接按"回车"键过去,要手动输入"yes"才可以

十、删除未跟踪文件

# 删除 untracked files
git clean -f
# 连 untracked 的目录也一起删掉
git clean -fd
# 连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的)
git clean -xfd
# 在用上述 git clean 前,墙裂建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删
git clean -nxfd
git clean -nf
git clean -nfd

十一、提交commit

  1. git commit --amend

    我们可以将新的改动提交到当前最近的提交上,比如你发现少改了什么,但是又不想多出一个提交时会很有用。
    如果我们认为我们的提交信息写的并不好,我要修改修改,这也是一种办法,但是并不是最好的办法。
    这个操作会更改先前的提交,并为其提供新的hash值。

  2. git rebase -i HEAD~13
    这个命令非常强大,可以说是git提交管理的神器,此命令含义是我们可以针对之前的13次的提交在VI环境中进行重新修改设计:

  • 操作选项 p 意味着保持原样什么都不做,我们可以通过vim中编辑提交的顺序,使其在提交树上生效

  • 操作选项 r: 我们可以修改提交信息,这种方式比commit --amend要好的多,因为不会新生成一个commit

  • 操作选项 e: 我们可以修改commit,比如新增或者删除某些文件改动

  • 操作选项 s: 我们可以将这个提交与其上一次的提交进行合并,并重新编辑提交信息

十二、tag

//创建标签
git tag -a v1.1 -m "注解"
    
//查看已有标签
git tag
git tag v1.1
    
//删除标签
git tag -d v1.1
    
//查看此版本所修改的内容
git show v1.1
    
//本地tag推到远程
git push --tags

十三、打包

参考连接:https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E6%89%93%E5%8C%85

bundle 命令会将 git push 命令所传输的所有内容打包成一个二进制文件, 你可以将这个文件通过邮件或者闪存传给其他人,然后解包到其他的仓库中

  • 打包
打包(origin/master, master] 提交区间
git bundle create commits.bundle master ^origin/master
  • 验证bundle包
    可以在导入到仓库之前查看这个包里包含了什么内容。 bundle verify 命令可以检查这个文件是否是一个合法的 Git 包,是否拥有共同的祖先来导入。
$ git bundle verify ../commits.bundle
The bundle contains 1 ref
71b84daaf49abed142a373b6e5c59a22dc6560dc refs/heads/master
The bundle requires these 1 ref
9a466c572fe88b195efd356c3f2bbeccdb504102 second commit
../commits.bundle is okay
  • 从bundle包中拉取合并
git pull ../commits.bundle

十四、仓库代码转移


Git global setup

git config --global user.name "xxx"
git config --global user.email "xxxx"

Create a new repository

git clone git@192.168.10.221:hardware-project-group/temper-controller/temper-controller.git
cd temper-controller
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 git@192.168.10.221:hardware-project-group/temper-controller/temper-controller.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 git@192.168.10.221:hardware-project-group/temper-controller/temper-controller.git
git push -u origin --all
git push -u origin --tags

十五、查看分支提交信息

# 单行查看当前分支
git log --oneline

# 单行查看,所有分支
git log --oneline --decorate --graph --all

十五、绘图工具Mermaid

https://blog.gitee.com/2022/06/29/mermaid/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值