常用命令

git config credential.helper store 保存账号密码到本地

git init 初始化仓库

git add . 添加所有文件到版本控制中

git commit -m "init" 提交文件,提交信息为 “init”

git commit --amend 继上一次提交合并一起提交,提交信息为上一次提交信息

git remote add origin https://xxx.com 关联远程仓库地址

git push --set-upstream origin master 初次推送本地仓库至远程

git relog 查看 git 操作日志

git checkout -b commitId 已某个 commit 为基点创建 新分支

git apply commitId 应用某次提交的内容

git rebase

git pull

常见场景

初始化仓库

git init

关联远程仓库

如果是更改远程仓库地址,需要先删除远程仓库关联 git remote remove origin,然后再关联到新的远程仓库git remote add $url

切换分支

git switch other-branch 或者 git checkout other-branch ,两个命令都表示切换到 other-branch 分支

创建/删除分支

  1. git checkout -b branchName 基于当前 HEAD 创建新的分支 `branchName
  2. git branch -d branchName 删除本地分支
  3. git push origin --delete orgin/branchName 删除远程分支

对指定文件/文件夹 去除版本追踪

  1. git rm --cached filePath --cached 替换 -f 表示同时删除本地文件
  2. git add .
  3. git commit -m 'delete info'
  4. git push

恢复丢失的代码

  1. git reflog
  2. git apply $commitId

添加/删除远程仓库地址

  1. git remote add newOrigin remote_url
  2. git remove remote newOrigin
  3. git push --all newOrigin
  4. git push --tags newOrigin

查看未合并到主分支的分支

  1. git branch -r --no-merged origin/master 未合并到远程 origin/master 的所有分支
  2. git branch --no-merged main 未合并到 master 的所有分支

常见问题

控制台中文乱码

git config --global core.quotepath false

更改文件夹名字 大小写无效

比如说 将 TempDirectory 改为 tempDirectory, 先将 TempDirectory 改为 tempDirectory_bak 然后提交。再将 tempDirectory_bak 改为 tempDirectory 之后提交,就可以改变远程仓库的 TempDirectory 目录名了。