git 常用命令


layout: post # 使用的布局(不需要改)

title: git # 标题

subtitle: git 命令 #副标题

date: 2023-11-03 # 时间

author: BY ThreeStones1029 # 作者

header-img: img/about_bg.jpg #这篇文章标题背景图片

catalog: true # 是否归档

tags: 工具 #标签


文章目录

一、前言

本文主要记录一些常用的git命令,为了方便以后使用。

二、git常用命令

2.1.初始化本地仓库并首次提交

bash 复制代码
git init # 初始化本地仓库
git add . # 添加所有文件到待提交区
git commit -m "first commit" # 双引号写提交记录
git branch -M main # 新建分支
git remote add origin git git@github.com:github用户名/仓库名.git
git push -u origin main # 提交

2.2.初始化本地仓库git管理

bash 复制代码
git init

2.3.添加文件到暂存区

bash 复制代码
git add filename
git add . #提交项目里面所有文件

2.4.添加提交信息

bash 复制代码
git commit -m '提交信息'

2.5.查看状态

bash 复制代码
git status

2.6.提交到远程仓库

bash 复制代码
# 选一个就行,看自己想提交到哪个分支
git push -u origin master #提交到master分支
git push -u origin main #提交到main分支

2.7.重命名

修改的是已经git管理的文件,还没有用git管理的直接修改文件名即可。

2.7.1.修改文件名

bash 复制代码
git mv old_file_name new_file_name

2.7.2.添加提交信息

bash 复制代码
git commit -m "提交信息"

2.7.3.提交到远程仓库

bash 复制代码
# 选一个就行,看自己想提交到哪个分支
git push -u origin master #提交到master分支
git push -u origin main #提交到main分支

报错

bash 复制代码
To https://github.com/ThreeStones1029/ThreeStones1029.github.io.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ThreeStones1029/ThreeStones1029.github.io.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

这应该是远程仓库和本地仓库没有同步报错,需要同步

2.8.更新远程仓库到本地仓库

bash 复制代码
git pull origin master
git pull origin main #根据需要二选一

运行后需要vim添加合并信息

输入i表示输入,Esc退出插入模式,:wq保存并退出

持续更新中...

可以访问我的博客查看git命令合集

相关推荐
i建模4 小时前
将远程 main 分支同步到 develop 分支的完整指南
git
即使再小的船也能远航14 小时前
【Git】实用Git操作指南:从入门到高效协作
git
<但凡.1 天前
Git 完全手册:从入门到团队协作实战(4)
git·bash
SugarPPig1 天前
Git 创建一个完全没有提交历史的 master 分支
git
lb29172 天前
git的使用,推送仓库github
git·github
躲在云朵里`2 天前
Git的使用
大数据·git·elasticsearch
悟能不能悟2 天前
在 IntelliJ IDEA 中打开这个用于设置 Git 用户名(Name)和邮箱(Email)的特定弹窗
java·git·intellij-idea
威威猫的栗子3 天前
Git 使用全指南:从配置到免密登录
大数据·git·vscode
Casia_Dominic3 天前
【tmux无法使用鼠标滚轮滚动页面的问题】解决方案
linux·git·github·tmux
Justice link4 天前
Nginx和Apache的区别
git