git常用命令

1.checkout

$ git checkout -b hotfix
Switched to a new branch 'hotfix'

2.reset

reset Reset current HEAD to the special state.
git reset 可以将提交的内容重置。--mixed 是默认操作。 --soft 重置之后不会让本地的代码消失。 --hard 重置之后会让本地代码消失。
git reset --hard HEAD^^ 小角个数代表重置次数。执行reset操作之后,HEAD和远程仓库不一致,需要解决冲突才能push。
以上命令已经无用,以下命令才有用。2021-06-21

git reset --soft HEAD~1

3.fetch

git fetch origin 远程分支xxx:本地分支xxx   
git fetch [<options>] [<repository> [<refspec>…]]
git log FETCH_HEAD   //查看fetch日志

4.rebase和merge

rebase和merge都是用来合并分支的命令。
rebase只是合并内容,不会合并提交历史。merge会把提交历史都合并。

git checkout featureA
git merge master
git checkout featureA
git rebase master

它会把整个featureA的分支直接移到现在master分支的后面。

git rebase origin/dev

合并远程分支到当前分支。

5.stash

stash将本地的变化缓存起来。stash pop将代码从缓存中弹出来。
可以有效利用该命令,合并远程代码。

refusing to merge unrelated histories

QQ20190611-1@2x.png

需要执行git pull origin master --allow-unrelated-histories将两个分支强行合并

有时候,git pull的时候提示失败,可以用git fetch之后,再git rebase

6.push

git push origin feature-branch:feature-branch    //推送本地的feature-branch(冒号前面的)分支到远程origin的feature-branch(冒号后面的)分支(没有会自动创建)

查看本地分支:

git branch

查看远程分支:

git branch -a

7.更新代码

以下命令的效果,相当于fetch+rebase,-r的意思是rebase
git pull -r origin oa-cloud

没事的时候可以看看:https://www.cnblogs.com/qcloud1001/p/9796750.html

存储密码,避免多次输入

You can use the git-credential-store via

git config credential.helper store

which stores your password unencrypted in the file system

8.关联GitHub仓库

1033738-20180704211141760-1394139259.png

9.开发流程

开发流程.png

10.不merge某个文件(有问题)

https://www.jianshu.com/p/09b546b936a7

11.delete branch

// delete branch locally
git branch -d localBranchName
// delete branch remotely
git push origin --delete remoteBranchName

12.拉取远程分支并创建本地分支

方法一
使用如下命令:

git checkout -b 本地分支名x origin/远程分支名x

使用该方式会在本地新建分支x,并自动切换到该本地分支x。

采用此种方法建立的本地分支会和远程分支建立映射关系。

方式二
使用如下命令:

git fetch origin 远程分支名x:本地分支名x

使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动checkout。

采用此种方法建立的本地分支不会和远程分支建立映射关系。

参考链接:https://juejin.cn/post/6844903539324485639
https://www.cnblogs.com/qyf404/p/git_push_local_branch_to_remote.html
https://www.cnblogs.com/springbarley/archive/2012/11/03/2752984.html
https://www.huaweicloud.com/articles/e295e93d1a0f08102fe61426ef4997f3.html
https://www.cnblogs.com/qcloud1001/p/9796750.html
https://www.freecodecamp.org/news/how-to-delete-a-git-branch-both-locally-and-remotely/
https://devconnected.com/how-to-undo-last-git-commit/#:~:text=The%20easiest%20way%20to%20undo,removed%20from%20your%20Git%20history.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. GIT命令 git init在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个re...
    江边一蓑烟阅读 825评论 0 0
  • Add & Commit git init 初始化一个 Git 仓库(repository),即把当前所在目录变成...
    冬絮阅读 4,880评论 0 9
  • 简介 Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。 Git 与常用的版本控制工具 ...
    闽越布衣阅读 2,771评论 0 18
  • git 使用笔记 git原理: 文件(blob)对象,树(tree)对象,提交(commit)对象 tree对象 ...
    神刀阅读 3,794评论 0 10
  • 首先说一下我不是打广告,就是正面的略客观的评价一下这个APP。 因为过年时候各种不熟悉的亲戚来到面前...
    我是采晨啊阅读 1,151评论 0 5