Git 基础

hefengbao 发布于 2023.10.25 ,最后更新于 2024.03.12

Git Command Explorer

先去使用基本的功能。

加入 git 管理

1.新项目

git init

git add .

git commit -m "init"

git branch -M main

git remote add origin 你的github仓库地址

git pull origin master --allow-unrelated-histories

git push -u origin master


git push

2.已有项目

git remote add 你的github仓库地址

git branch -M main

git push -u origin main

克隆(clone)

把远程服务器项目复制到本机

git clone git@github.com:hefengbao/jingmo.git

重新设置项目名称

git clone git@github.com:hefengbao/jingmo.git xx

拉取(pull)

获取远程服务器最新代码

git pull

指定分支

git pull origin develop

分支( branch)

  1. 创建分支
git branch feature_x

切换到 feature_x 分支:

git checkout feature_x

创建分支并切换到新建的分支:

git checkout -b feature_x
  1. 拉取远程分支并创建本地分支
git checkout -b feature_x origin/feature_x

这种方式需要远程分支 feature_x 存在才可以。

  1. 建立当前分支与远程分支的映射关系
git branch -u origin/feature_x

或者

git branch --set-upstream-to origin/feature_x
  1. 撤销本地分支与远程分支的映射关系
git branch --unset-upstream
  1. 查看本地分支
git branch
  1. 查看远程分支
git branch -r
  1. 查看本地和远程分支
git branch -a
  1. 切换本地分支
git checkout 分支名称
  1. 删除本地分支
git branch -d 分支名称
  1. 把其他分支合并到当前分支
git merge 其他分支名称
  1. 查看哪些分支已被并入当前分支
git branch --merged

一般前面没有标星的分支可以通过 git branch -d bootstrap-ui 命令删除。

  1. 查看哪些分支没有合并到当前分支
git branch --no-merged
  1. 查看各个分支最后一个提交对象的信息
git branch -v
  1. 重命名本地分支
git branch -m 当前的名称  想要的分支名
  1. 删除远程分支
git push origin -d 要删除的远程分支名称

状态(status)

git status

添加(add)

把当前所有修改添加到下次提交中:

git add .

把对某个文件(文件夹)的修改添加到下次提交中:

git add -p <file>
git add <file>
git add <folder>

Commit

把修改提交到本地仓库

git commit -m 'message here'

修改上次提交 请勿修改已发布的提交记录!

git commit --amend

Git 提交信息规范化

提交(push)

把自己最新代码提交到远程服务器

git push -u origin master

或者

git push --set-upstream origin master

Stash

把当前分支中未提交的修改移动到其他分支:

git stash
git checkout branch2
git stash pop

将 stashed changes 应用到当前分支:

git stash apply

删除最新一次的 stashed changes:

git stash drop

Fetch

Rebase

Reset

https://juejin.cn/post/7071780876501123085

远程服务器(remote)

查看远程服务器信息

git remote -v

设置(重置)远程服务器地址

git remote set-url origin git@github.com:hefengbao/jingmo.git

配置(config)

git config --list	
git config --local --list
git config --global --list
git config --system --list
git config --global user.name "your name"

或者只为当前项目配置

git config user.name "your name"
git config --global user.email "me@example.com"

或者只为当前项目配置

git config user.email "me@example.com"

Git 的一些概念

版本库.git

工作区

暂存区 (Index/Stage)

本地仓库(Repository)

远程仓库(Remote)

参考: https://juejin.cn/post/6869519303864123399

资料:

Learn Git Branching - 动画演示

stash, reset --soft, cherry-pick, revert, relog

Git 聊天入门

最常见的 Git 错误都有哪些,如何解决它们?

12 个 Git 的使用技巧!

Git忽略提交规则 - .gitignore配置运维总结

Git rebase 用法小结

腾讯是如何使用 Git 的 ?

Gitflow工作流程

Git    Git   Gitflow  

上一篇:使用 vscode 作为 Laravel 开发 IDE

下一篇:Git Commit 规范化工具

有 0 条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注