Git使用

Git配置

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

仓库操作

  • 克隆远程仓库: git clone url

  • 更新本地仓库:

    • git pull (所有分支)

    • git pull origin dev (某分支)

  • 创建本地仓库:

    • git init (在某个文件夹中)

    • rm -r .git folder (删除仓库)

  • 本地仓库连接到远程:

    • git remote add origin url

    • git remote remove origin (取消连接)

分支操作

  • 创建并切换到分支: git checkout -b branch

  • 切换分支: git checkout branch

  • 删除远程分支: git push origin --delete branch

  • 删除本地分支: git branch -d/-D branch

  • 查看本地和远程所有分支: git branch -a

  • 查看当前分支的状态: git status

  • 从远程分支创建分支: git checkout -b branch1 origin branch2

  • 本地分支推到远程: git push origin branch

  • 合并分支: git merge branch (当前分支与指定分支合并)

本地文件提交

  • 提交文件到远程:

    1. git add . (将该目录下所有未提交的文件加入暂存区, .处可以指定文件或文件夹,空文件夹不能提交)

      • git reset HEAD file (取消add)
    2. git commit -m 'info' (提交到HEAD)

    3. git push (提交到远程)

      • git push origin branch (提交到远程某分支)

      • git pull --rebase origin branch (当远程分支与本地分支不一致时,先拉取远程内容,再提交)

  • 删除远程文件:

    1. git rm --cached file

    2. git commit -m 'info'

    3. git push

  • 删除本地未提交的修改: git checkout file

*Windows Git下载地址: https://git-scm.com/downloads

Author: Giftbear
Link: https://giftbear.github.io/2021/11/29/Git使用/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.