Git是一个分布式版本控制系统,广泛应用于软件开发项目中来追踪和控制代码的修改历史。
Git常用命令如下:
配置用户信息:
# 设置全局用户名和邮箱git config --global user.name "Your Name"git config --global user.email "[email protected]"初始化仓库:
# 在当前目录创建一个新的Git仓库git init克隆仓库:
# 克隆远程仓库至本地git clone https://github.com/user/repo.git添加文件到暂存区:
# 将某个文件添加到暂存区以准备提交git add <file_path># 或者添加所有改动git add .提交更改:
# 提交暂存区的内容到本地仓库,附带提交信息git commit -m "Initial commit or describe your changes here"查看状态:
# 检查工作区和暂存区的状态git status拉取远程更新:
# 获取远程仓库的最新改动并尝试自动合并到当前分支git pull origin <branch_name>推送更改:
# 将本地分支的更改推送到远程仓库的对应分支git push origin <branch_name>创建与切换分支:
# 创建并立即切换到新的分支git checkout -b new_branch# 切换回已有分支git checkout <existing_branch>查看分支:
# 显示所有本地分支git branch# 显示所有本地和远程分支git branch -a解决冲突与合并分支:
# 合并指定分支到当前分支git merge other_branchstash暂存未提交的更改:
# 暂存所有未提交的更改git stash# 恢复最近暂存的更改git stash pop查看提交历史:
# 显示提交历史记录git logCherry-pick:
# 将指定提交应用到当前分支git cherry-pick <commit_hash>撤销更改:- ##### 取消暂存区的更改:
git reset <file_path> # 将指定文件从暂存区移除,但保留工作区的更改git reset HEAD <file_path> # 类似于上述命令,取消暂存的同时恢复到HEAD版本- ##### 回滚工作区的更改:git checkout -- <file_path> # 抛弃工作区对指定文件的更改删除文件:- ##### 从版本库和工作区一起删除:
git rm <file_path>git commit -m "Remove file"- ##### 仅从版本库中删除(保留工作区文件):git rm --cached <file_path>git commit -m "Remove file from repository"重命名/移动文件:
git mv <old_file_path> <new_file_path>git commit -m "Rename/move file"查看不同版本间的差异:
git diff # 查看尚未暂存的改动git diff --staged # 查看已暂存但未提交的改动git diff <commit1> <commit2> # 查看两个提交之间的差异回退到以前的提交:
# 回退到某一提交,并且丢弃之后的所有更改(谨慎操作)git reset --hard <commit_hash>标签管理:- ##### 创建标签:
git tag <tag_name> <commit_hash> # 标记特定提交git tag <tag_name> # 标记当前HEAD指向的提交- ##### 推送标签到远程仓库:git push origin <tag_name>- ##### 删除标签:git tag -d <tag_name>git push origin :refs/tags/<tag_name> # 删除远程标签stash栈操作:- ##### 存储未提交的更改并清理工作区(保存现场):
git stash- ##### 列出stash列表:git stash list- ##### 应用stash中的某个更改:git stash apply <stash@{index}>- ##### 永久应用并删除stash:git stash pop子模块操作:- ##### 添加子模块:
git submodule add <repository_url> <path_to_submodule>- ##### 更新子模块:git submodule update --remote交互式暂存(添加部分更改):
git add -pRebase(变基):
# 将feature分支的更改重新应用到master分支上,保持线性历史git checkout featuregit rebase mastergit checkout mastergit merge feature
本文转载自: https://blog.csdn.net/Rcain_R/article/details/136665783
版权归原作者 Rcain_R 所有, 如有侵权,请联系我们删除。
版权归原作者 Rcain_R 所有, 如有侵权,请联系我们删除。