git常用命令(linux和windows通用)

本文的命令已可满足日常需求

配置用户信息

git config --global user.name “github用户名”
git config --global user.email “github绑定邮箱"

查看配置信息

git config --global user.name
git config --global user.email

~/.bashrc文件介绍

~指当前用户的根目录,.bashrc文件是当前用户的配置文件

例如,为常用指令配置别名:

#用于输出git提交日志 
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit' 
#用于输出当前目录所有文件及基本信息 
alias ll='ls -al'

最后source ~/.bashrc更新一下配置文件

git init
git add .
git commit -m "提交注释"
#查看提交状态
git status
#查看提交日志,自定义git-log,git log --pretty=oneline --all --graph --abbrev-commit
git log
#版本回退,commitID 可以使用 git-log 或 git log 指令查看,git reflog查看已经删除的提交记录
git reset --hard commitID

分支操作

查看本地分支

git branch

创建本地分支

git branch 分支名
git branch -m oldBranchName newBranchName

切换分支

git checkout 分支名

创建并切换分支

git checkout -b 分支名

合并分支

git merge 分支名称

删除分支,不能删除当前分支,只能删除其他分支

git branch -d b1 #删除分支时,需要做各种检查
git branch -D b1 #不做任何检查,强制删除

冲突发生后查看冲突

git diff

放弃合并,回滚到合并之前

git merge --abort

远程仓库

添加远程仓库

git remote add <远端名称(一般为origin)> <仓库路径>
git remote remove origin
  • 远端名称,默认是origin,取决于远端服务器设置
  • 仓库路径,从远端服务器获取此URL

查看远端仓库

git remote

推送到远程仓库

git push origin master
  • 如果远程分支名和本地分支名称相同,则可以只写本地分支
  • -f 表示强制覆盖
  • –set-upstream 推送到远端的同时并且建立起和远端分支的关联关系
git push -f --set-upstream origin master

从远程克隆到本地

 git clone <仓库路径> [本地目录]

查看本地分支与远程分支的关系

git branch -vv

操作远程分支

抓取,不进行合并,如果不抓取指定分支名称,则抓取全部分支

git fetch [remote name] [branch name]

拉取,等同于fetch+merge

git pull [remote name] [branch name]

git报错

  1. error: Your local changes to the following files would be overwritten by merge:

image-20221012135951216

git reset --hard
git pull origin master
  1. 大文件报错fatal: sha1 file ‘’ write error: Broken pipe

image-20221012155320263

#500MB=1024*1024*500
# 方法一:全局配置
git config --global http.postBuffer 524288000

或者

# 方法二:当前仓库配置
git config http.postBuffer 524288000

猜你喜欢

转载自blog.csdn.net/weixin_44092851/article/details/128242174