Git命令

作者: zlf_j | 来源:发表于2020-08-31 15:56 被阅读0次

一、提交

检查文件状态

git status

提交某一个文件

1、git add -u '文件路径' ( 若要提交全部文件,则执行 git add . )
2、 git commit -m 'aa'
3、git pull
4、git push

提交文件命令

git commit -am '备注'
即 git add . 跟 git commit -m '备注'(修改的文件全部提交)

git add . 撤回(将之前添加到暂存区的内容从暂存区移出到工作区)

git reset HEAD + file路径(可以撤回单个文件)
https://www.cnblogs.com/xy-ouyang/p/12205467.html

git add . 报错:
 warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory

解决办法:
git config --global core.autocrlf true
https://www.jianshu.com/p/eb8737547c9a

查看提交记录

gitk

本地忽略

忽略提交
git update-index --assume-unchanged xxx/xx/x/logback.xml
例:git update-index --assume-unchanged src/assets/js/http.js
取消忽略
git update-index --no-assume-unchanged xxx/xx/xlogback.xml
https://blog.csdn.net/qq_34581118/article/details/78437992

忽略提交(.DS_Store文件)

.gitignore文件中加:
*/.DS_Store
.DS_Store
https://yalv.me/mac-rm-ds_store/

二、分支

查看分支

git branch // 查看本地分支

 * dev     // 当前在dev分支(星号表示当前所在分支)
   master

git branch -a // 查看本地远程分支,白色字体为本地分支,红色字体为远程分支

切换分支

git checkout dev // 切换到dev分支

  • 必须把所有修改的文件都提交完成,即 git status 执行为空时,再进行切换。
提交(push)

git push origin dev // 将本地代码提交到远程分支dev上
git push // 提交到当前本地分支,已绑定的远程分支上

  • 要提交到某一分支,最好先切换到该分支,再修改,否则容易发生冲突。

拉取其他分支代码
git pull origin dev (dev为其他分支名)

新建本地分支并绑定远程分支

git checkout -b web_dev // 新建本地分支web_dev,并切换到该分支

  • git pull 报错如下
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> web_dev
  • git push 报错如下
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use
    git push origin HEAD:master
To push to the branch of the same name on the remote, use
    git push origin hotfix-dev
To choose either option permanently, see push.default in 'git help config'.
  • 无论哪种报错,都需要执行以下命令

git branch --set-upstream-to=origin/new_dev new_dev // 本地分支与远程分支建立联系
第一个new_dev为远程分支名(远程得有这个分支),第二个为本地分支名

删除分支
  • 删除本地分支

git branch -D 分支名(*** 需要先切换到其他分支上)

  • 删除远程分支

git push origin --delete 分支名
https://www.cnblogs.com/smallredness/p/11205466.html

修改分支名称
  • 本地分支

git branch -m oldName newName
https://blog.csdn.net/qq_37982823/article/details/87370493

  • 远程分支

git branch -m oldName newName
git push --delete origin oldName
git push origin newName // 新分支推上去
git branch --set-upstream-to origin newName // 本地与远程关联
https://www.cnblogs.com/huting-front/p/12106578.html

  • git push失败,报错
git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.
  • 原因:本地分支是从远程分支拉取的,在执行git push命令时,不知道应该与远程哪个分支进行同步
  • 解决办法:重新指定与远程同名的分支(执行后,可直接用git push提交)
git push -u origin 分支名

https://blog.csdn.net/ysl19910806/article/details/89497620

三、代码修改(需谨慎)

放弃本地修改(所有文件都回到修改之前)

git reset --hard HEAD

远程分支代码覆盖本地(远程代码替换本地代码)

git fetch --all
git reset --hard origin/master (这里master要修改为对应的分支名)
git pull

https://blog.csdn.net/zoulonglong/article/details/79562922

本地代码恢复到某次提交

git reset --hard commitId

https://blog.csdn.net/qq_31648761/article/details/80983481

强制将本地代码覆盖远程代码

git push -f
https://blog.csdn.net/lcr_happy/article/details/113824699

将某个分支的代码完全覆盖另一个分支
  • 将test分支上的代码完全覆盖dev分支
git checkout dev // 切换到dev分支
git reset --hard origin/test // dev分支上的代码就完全被test分支上的代码覆盖(本地分支)
git push -f // 将本地分支强行推到远程分支

https://www.cnblogs.com/justdoyou/p/11389623.html

四、git 相关操作

git下载

https://git-scm.com/download/win

输入用户名密码认证失败(windows git身份验证失败清除密码缓存)

git config --system --unset credential.helper
之后再进行git操作时,弹出用户名密码窗口,输入即可
https://blog.csdn.net/mrhaoxiaojun/article/details/90482125

代码上传到github

https://www.cnblogs.com/ximiaomiao/p/7140456.html

代码上传到码云

五、新增 20200727

一、git 基于某个分支,创建分支

1、拷贝源代码
git clone git@git地址
cd 项目目录
2、根据已有分支创建新的分支
git checkout -b yourbranchname origin/oldbranchname
3、推送到git

git push origin yourbranchname
参考:
https://blog.csdn.net/weixin_34319374/article/details/86004265

二、本地分支绑定远程分支错误(重新执行一次,绑定另一分支即可)

若不小心建立关联错误,如执行如下
git branch --set-upstream-to=origin/other-branch
每次提交会提交到other-branch分支
想恢复则重新执行
git branch --set-upstream-to=origin/test2 // 重新执行一次即可
https://blog.csdn.net/second_boy/article/details/50832725

六、新增 2020.12.09

一、本地开发分支,用错了
  • 本地开发用了master分支(与远程master分支关联)
    想提交到远程的feature上,而不提交到master上

git add .
git commit -m 'debug' // 不push就不会推送到远程master上
git log // 查看commit记录,保存这次commit的id
git checkout feature // 切换到想提交的分支feature上
git cherry-pick commitId(master上commit的id)// 将这次commit的内容拉到feature分支上
git push // 提交到远程的feature上
git branch -D master // 删掉本地的master分支
git checkout master // 新建master分支,与远程master分支的代码一致,就将上次的commit给删啦

二、本地看不到新建的远程分支

git fetch
https://www.cnblogs.com/jiu0821/p/9796267.html

三、代码合并(merge)
  • master(正式分支),feature(开发分支)

git checkout master // 切换到正式
git merge feature // 合并开发分支上的代码
git push // 提交上去

四、git rebase 回退

git rebase --abort
https://blog.csdn.net/orangefly0214/article/details/81712993

五、git tag(给某次上线提交打标签,区分提交记录,方便代码回退)

git checkout master // 上线分支
git merge feature // 合并开发分支
git tag v20210121.1 // 打标签
git push origin v20210121.1 // 提交tag到远程
git push // 提交代码

  • 代码回退到版本v20210121.1

git checkout v20210121.1

  • 删除tag

删除本地tag
git tag -d v20210426.1
删除远程tag
git push origin :refs/tags/v20210426.1
https://blog.csdn.net/i_dont_know_a/article/details/90202674
https://www.cnblogs.com/pansidong/p/7773873.html

六、git clone报错
warning: remote HEAD refers to nonexistent ref, unable to checkout.
  • 原因:.git目录下.git/refs/heads不存在HEAD指向的文件
  • 解决办法:

git branch -a //输出 remotes/origin/branch_qc_origin
git checkout remotes/origin/branch_qc_origin // checkout的是git branch -a输出的内容
git checkout -b remotes/origin/branch_qc_origin // 创建分支
git branch -m remotes/origin/branch_qc_origin master //重命名分支叫master
https://blog.csdn.net/whu_zhangmin/article/details/12040493

七、强制本地代码覆盖远程代码(谨慎)

git push -f
https://blog.csdn.net/lcr_happy/article/details/113824699

八、切换回某次commit(谨慎)

git reset --hard commitId
https://blog.csdn.net/luxiaoruo/article/details/106637291

九、git merge,交换文件.MERGE_MSG.swp已经存在的问题
  • 原因:非正常退出情况下, VIM 不会删除 ,.swp 文件会作为文件编辑状态的内容备份
  • 解决办法:删掉该文件

七、其他命令

查看目录:

ls

删除某个文件

rm -rf 文件路径

git init 撤回

rm -rf .git
git init后会生成一个.git 的文件夹,只要删除这个文件夹就好了。

查看vue打包后各个文件大小 --report

npm run build --report
https://blog.csdn.net/yunchong_zhao/article/details/104374295/

git branch命令 了解

https://www.cnblogs.com/jianguo221/p/12574645.html

Mac下查看node等的安装路径(命令行)

在命令行中输入命令
which node
结果如下:
/usr/local/bin/node
https://blog.csdn.net/qq_33833327/article/details/78181427

git报错

git config --global user.name "yourName" //注意,--和global之间没有空格
git config --global user.email "yourName@gmail.com"

相关文章

  • Github学习文档-3

    目录 1.Git 的基本的命令git init命令git status命令git add命令git commit命...

  • git实用命令

    git实用命令 1 git init 命令 初始化 git 仓库 repository 2 git add 命令 ...

  • git管理工具

    git分支命令 git提交命令

  • git 多条命令一次执行

    上代码git命令 && git命令

  • git入门

    这里对git init、git add、git commit命令进行一个总结。 $ git init 命令,该命令...

  • GIT 常用命令总结

    GIT 常用命令总结 GIT 初始化命令 命令描述git init初始化本地 git 仓库git config -...

  • Git命令整理

    Git命令 ———————————————— git配置: git基本步骤: git分支管理: 创建分支命令: 切...

  • git 分支

    git 显示所有分支(包含本地和远程)命令: git 删除本地分支命令: git 显示远程分支命令: git 删除...

  • Git命令集合

    Git基础命令 git远程关联 git远程关联移除 git命令简单简介 常用命令集合:git init创建版本库 ...

  • git使用指南

    常用命令 帮助类: git --help:显示git命令帮助信息git help -a:显示所有命令git hel...

网友评论

      本文标题:Git命令

      本文链接:https://www.haomeiwen.com/subject/lkausktx.html