引言
Git作为当前最流行的版本控制系统,已经成为程序员日常工作中不可或缺的工具。掌握Git可以帮助我们高效地管理代码,实现团队协作,以及轻松应对各种开发挑战。本文将为您详细介绍50个核心的Git命令,助您成为Git高手。
初始化与配置
git init
:初始化一个新的Git仓库。
git config
:配置Git的全局或局部设置,如用户名、邮箱等。
代码管理
git add
:将修改的文件添加到暂存区。
git commit -m “commit message”
:将暂存区的文件提交到本地仓库。
git status
:查看当前代码状态,包括已修改、已暂存等。
git diff
:查看文件在工作区与暂存区或本地仓库的差异。
分支操作
git branch [branch-name]
:列出本地分支,或创建新分支。
git checkout [branch-name]
:切换到指定分支。
git merge [branch-name]
:合并指定分支到当前分支。
git rebase [branch-name]
:将指定分支的提交应用到当前分支上。
远程仓库
git remote
:管理远程仓库的操作,如添加、删除、重命名远程仓库。
git clone
:从远程仓库克隆项目。
git pull
:拉取远程仓库的最新代码。
git push
:推送本地代码到远程仓库。
撤销与回退
git revert [commit-hash]
:撤销指定提交并生成一个新的提交。
git reset –hard [commit-hash]
:回退到指定的提交状态。
git reset –hard HEAD
:回退到上一个版本。
git stash
:暂存当前的修改,以便切换分支或恢复。
历史记录
git log
:查看提交历史记录。
git blame [file]
:显示文件每一行的修改信息。
git show [commit-hash]
:查看某个提交的详细信息。
标签管理
git tag [tag-name]
:创建标签。
git tag -d [tag-name]
:删除标签。
git show [tag-name]
:查看标签信息。
其他常用命令
git checkout — [file]
:撤销对文件的改动,恢复到最新版本。
git rm [file]
:删除文件并加入到暂存区。
git mv [file] [new-file]
:重命名文件。
git cherry-pick [commit-hash]
:应用指定提交到当前分支。
git rebase -i [commit-hash]
:交互式重写历史提交。
git bisect
:二分查找问题提交。
git grep
:在提交历史中搜索内容。
git blame -L [start-line],[end-line] [file]
:显示文件指定行的修改信息。
git shortlog
:显示每个作者的提交记录。
git log –pretty=format:”%ad | %an | %s”
:以特定格式显示提交历史。
高级命令
git reflog
:查看所有分支的引用更改历史。
git remote show [remote-name]
:显示远程仓库信息。
git remote prune [remote-name]
:删除未跟踪的远程引用。
git ls-files –error-unmatch [pattern]
:列出匹配模式的文件。
git ls-tree [commit-hash]
:显示提交树。
git ls-tree -r [commit-hash]
:递归显示提交树。
代码审查
git request-pull [commit-hash]
:请求合并提交。
git review
:查看代码审查请求。
git review -s [commit-hash]
:提交代码审查。
git review -S [commit-hash]
:提交代码审查,包括所有修改。
工作流管理
git worktree add [path] [commit-hash]
:添加工作树。
git worktree list
:列出所有工作树。
git worktree remove [path]
:删除工作树。
git worktree prune
:删除未跟踪的工作树。
代码同步
git fetch
:获取远程仓库的分支和提交。
git pull –rebase
:拉取远程仓库的最新代码,并尝试合并。
通过掌握以上50个核心的Git命令,您将能够轻松地管理代码,实现团队协作,并应对各种开发挑战。祝您成为Git高手!