git

git

  • Git是一种版本控制软件,是一个命令,是一种工具。

  • 在cmd中输入git -v查看git版本

image-20240511113152006

git使用流程

  • 分为三个区
    • 工作区(git init)
    • 暂存区(绿色 git add . )
    • 版本库(git commit -m '')

常用命令

  1. git init: 在当前目录初始化一个新的Git仓库。
  2. git clone 源地址: 克隆一个远程仓库到本地。
  3. git add <file>: 将文件、文件夹添加到暂存区。
  4. git commit -m "commit message": 将暂存区的文件提交到本地仓库。
  5. git push 源名 源地址: 将本地仓库的提交推送到远程仓库。
  6. git pull 源名 分支名: 从远程仓库拉取更新到本地仓库。
  7. git status: 查看工作区和暂存区的状态。
  8. git log: 查看提交历史记录。
  9. git reflog: 查看本地仓库中 HEAD 引用的变化历史。
  10. git branch: 列出本地分支,或创建新分支。
  11. git checkout <branch-name>: 切换到指定分支。
  12. git merge <branch-name>: 合并指定分支到当前分支。
  13. git remote remove 源名: 删除远程源。
  14. git remote -v: 查看远程仓库的地址。
  15. git remote add 源名 源地址: 添加远程源。
  16. git fetch: 从远程仓库下载对象和引用(但不合并或修改本地分支)。
  17. git reset --hard 版本号: 版本回退。
  18. git diff: 查看文件之间的差异。
  19. git config --global user.email: 设置邮箱。
  20. git config --global user.name: 设置用户名。

image-20240511113152006

git忽略文件

.gitignore

.idea
*.log
*.pyc
__pycache__
**/migrations/*.py
!**/migrations/__init__.py
.venv
scripts
db.sqlite3
.vscode

git多分支

  1. 创建分支:

    • git branch <branch-name>:在当前提交上创建一个新的分支。
    • git checkout -b <branch-name>:创建一个新的分支并立即切换到该分支。
    • git switch -c <branch-name>:创建一个新的分支并立即切换到该分支(Git 2.23+ 版本)。
  2. 切换分支:

    • git checkout <branch-name>:切换到指定的分支。
    • git switch <branch-name>:切换到指定的分支(Git 2.23+ 版本)。
  3. 查看分支:

    • git branch:列出本地所有分支。
    • git branch -r:列出远程所有分支。
    • git branch -a:列出所有分支(本地和远程)。
  4. 删除分支:

    • git branch -d <branch-name>:删除本地分支(注意:删除分支前确保该分支已经合并到其他分支)。
    • git branch -D <branch-name>:强制删除本地分支,即使它没有被合并。
    • git push origin --delete <branch-name>:删除远程分支。
  5. 合并分支:

    • git merge <branch-name>:将指定分支合并到当前分支。
    • git rebase <branch-name>:将当前分支的提交移动到指定分支的顶端,然后再将指定分支合并到当前分支。
  6. 查看分支合并历史:

    • git log --oneline --graph --all:以图形化的方式查看所有分支的合并历史。

image-20240511155347447

image-20240511154859269

git远程仓库

设置仓库地址

git remote add origin '仓库地址'

提交

git push origin master

ssh连接操作远端

1 生成公钥和私钥

ssh-keygen -t ed25519 -C '邮箱地址'

2 在操作系统用户-家路径.ssh文件夹看到公钥和私钥

image-20240512180511354

3 去gitee配置公钥

image-20240512180539122

image-20240512180613197

测试是否配置成功

ssh -T git@gitee.com # 输入yes

image-20240512180718275

4 以后就可以免密操作

5 增加一个远程仓库地址,指定是ssh链接

git remote add origin git@gitee.com:heart99999/luffy_api.git

6 把代码推送到远端

git push origin master

补充

提交的时候遇到报错

error: failed to push some refs to 'gitee.com:heart99999/luffy_api.git'

转载至:https://blog.csdn.net/weixin_45811256/article/details/125989719

git pull --rebase origin master
posted @ 2024-05-17 15:04  ssrheart  阅读(4)  评论(0编辑  收藏  举报