前言
在使用 Git 时,尤其是通过集成开发环境 (IDE) 的图形界面操作 Git 后,当我们又想使用 Shell 来管理的时后,我们可能会发现自己不太容易记住一些命令。
以下是我整理的一些关于Git命令的总结 (Summary of commonly used Git commands):
一、基本操作
-
初始化仓库
git init
-
克隆仓库
git clone <repository> // 克隆一个仓库 git clone https://github.com/Herbert0501/gitignore.git // 指定分支 git clone -b 分支名 https://github.com/Herbert0501/gitignore.git
-
查看状态(查看当前工作目录和暂存区的状态)
git status
-
添加文件到暂存区
git add <file> // 将所有变化添加到暂存区 git add .
-
提交修改
git commit -m "commit msg"
-
查看提交历史
git log
二、分支操作
-
查看分支
git branch
-
创建新分支
git branch <new-branch> // 例如为模块新增功能-删除功能 git branch 20240521-feature-delete
-
切换分支
git checkout <branch> // 切换到主分支 (main) git checkout main
-
创建并切换到新分支
git checkout -b <new-branch>
-
删除分支
git branch -d <branch>
-
合并分支
git merge <branch> // 1.切换到主分支(main) git checkout main // 2.将20240521-feature-delete分支合并到main git merge 20240521-feature-delete
三、远程仓库
-
查看远程仓库
git remote -v
-
添加远程仓库
git remote add <name> <url>
-
拉取远程仓库
git pull <remote> <branch>
-
推送到远程仓库
git push <remote> <branch> // 强制推送, 将本地覆盖到远程 git push -f <remote> <branch>
-
删除远程分支
git push <remote> --delete <branch>
四、撤销修改
-
撤销未暂存的修改
git checkout -- <file>
-
撤销已暂存的修改
git reset HEAD <file>
-
撤销提交
git revert <commit>
-
重置提交
git reset --soft <commit> git reset --hard <commit>
-
将文件从暂存区中删除,但保留工作目录中的文件
git rm --cached <file1> <file2> // 1. 将文件添加到.gitignore file1 file2 // 2. 使用命令删除 git rm --cached file1 file2 // 3. 提交更改(删除远程文件,保留本地文件) git add . git commit -m "something modify"
五、标签
-
列出标签
git tag
-
创建标签
git tag <tag>
-
删除标签
git tag -d <tag>
-
推送标签
git push <remote> <tag>
-
推送所有标签
git push <remote> --tags
参考 Git 官方文档
总结
如果您觉得今天的文章对您有帮助,我相信您一定会喜欢我的博客。
哈利の小屋 - Kang Yao Coding - 努力有时候战胜不了天分,但至少能让别人看得起你。
在那里,我会定期更新关于计算机类的文章,并与您分享更多实用的经验和知识。欢迎您来访问和留言交流。