常用的Git命令

常用

克隆一个仓库: git clone [url]
选中一个分支: git checkout [brachName]
从远程分支拉取最新的数据,不会合并到当前分支: git fetch
从远程分支拉取最新的数据,自动合并到当前分支: git pull
查看当前工作区状态: git status
把文件加入到暂存区: git add [fileName]
从暂存区把文件移除: git reset HEAD [fileName]
提交暂存区文件: git commit -m "[commit message]"
把工作区的更新推送到远程分支: git push
把远程分支合并到当前分支: git merge [origin/branchName]

偶尔用

列出最近的commit记录: git log git log --pretty=oneline
列出历史操作记录: git reflog
回退到某个版本: git reset --hard [commitId]
丢弃工作区中fileName文件的修改: git checkout -- [fileName]
把文件从git版本库中移除(不删除文件): git rm --cached [fileName]
把文件从git版本库中移除(删除文件): git rm --f [fileName]

Branch

列出本地分支: git branch git branch --list
列出远程分支: git branch -r
列出所有分支: git branch -a
基于当前分支创建名为branchName的分支: git branch [branchName]
基于branchName2创建名为branchName1的分支: git branch [branchName1] [branchName2]
删除名为branchName的分支: git branch -d [branchName]

Tag

列出现有的Tag列表: git tag git tag --list
新加一个名为tagName的Tag: git tag [tagName]
推送一个Tag到远程: git push [tagName]
推送所有的Tag到远程: git push --tags git push origin --tags
删除本地tag: git tag -d [tagName]
删除远程tag: git push origin :refs/tags/<tagname>

Remote

显示远程仓库地址: git remote -v
新增一个名为name的远程仓库地址: git remote add [name] [url]
当有多个远程地址时,可以选择一个仓库拉取数据: git fecth [remoteName]
把当前分支推送到远程的某个分支上: git push [remoteName] [branchName]
显示一个远程仓库状态: git remote show [remoteName]
重命名远程仓库: git remote rename [oldName] [newName]
删除远程仓库: git remote rm [remoteName]
删除本地存在但远程不存在的分支: git remote prune [remoteName] eg: git remote prune origin

Commit

修改前一次commit(比如commit message写错了): git commit --amend
如果需要修改的commit不是后面还有其他的commit, 需要如下操作:
git checkout -b [branchName] [fixedCommitId]
git cherry-pick [fixedCommitId]
git commit --amend
git cherry-pick [依次填入fixedCommitId后面的commitId]
ps:修改后的内容只能在新的分支上,原分支内容是修改不了的了

原文地址:https://www.cnblogs.com/Trainoo/p/8258200.html