git 命令

mac2025-10-08  1

基本命令

查看文件状态 git status将文件添加到暂存区 git add <file-name> git add <path> git add -a将暂存区中的文件提交到版本库 git commit -m 'message'将本次提交合并到上次提交,修改提交信息 git commit --amend将本次提交合并到上次提交,不修改提交信息 git commit --amend --no-edit查看提交历史 git log丢弃工作区的修改(回到版本库的版本) git checkout -- <file-name>将暂存区的文件丢弃到工作区 git reset HEAD <file-name>删除untracked的文件(不会删除.gitignore中的忽略的文件) 删除当前目录下untracked文件,不会删除文件夹 git clean -f删除当前目录下的untracked文件及文件夹 git clean -df使用交互式删除当前目录下的untracked文件 git clean -if 暂存当前分支所在的分支现场 git stash恢复现场并删除暂存 git stash pop查看暂存信息 git stash list

分支操作

创建本地分支 git branch <branch-name>查看本地所有分支 git branch查看所有分支,包括远程跟踪分支 git branch -a切换分支 git checkout <branch-name>创建并切换分支 git checkout -b <branch-name>合并分支到当前分支 git merge <branch-name>合并指定的提交到当前的分支 git cherry-pick <commit-id...>删除已被合并分支 git branch -d <branch-name>删除未被合并分支 git branch -D <branch-name>

远程仓库

克隆远程仓库到本地 git clone <url>创建远程分支(同时也会创建本地的远程跟踪分支) git push origin <local-branch>:<remote-branch>删除远程分支(同时也会删除本地的远程跟踪分支) git push origin :<remote-branch> git push origin --delete <remote-branch>查看本地及远程跟踪的分支 git branch -vv推送跟踪分支的提交到远程仓库 git push origin <local-branch>拉取所有分支的更新(同时创建远程跟踪分支) git fetch拉取指定远程分支的更新 git fetch origin <remote-branch>git pull git fetchgit merge git pull --rebase git fetchgit rebase
最新回复(0)