git add/commit 撤销/回退操作
git checkout .|[file] # 撤销所有文件或指定文件修改
git reset HEAD [file] #(git add操作)取消文件暂存,回到工作区
git reset –-[hard|soft|mixed...] [commit-id] #(git commit操作)回退到暂存区,保留修改(git log查看commit-id,--hard彻底回退,不保留修改;--soft回退到暂存区,保留修改;--mixed回退到工作区,保留修改)
git pull 撤销/回退操作
git reflog # 查看历史变更记录
git reset --[hard|soft|mixed...] Hash|HEAD@{n} # 回退。(n是回退到的引用位置)
例如:git reset --hard 4911477 或 git reset --hard HEAD@{2}
git push 撤销/回退操作
方式一:
git reset –-hard commit_id
git push origin HEAD --f # 撤销commit-id之后的push,commit-id之后的内容不会保留
方式二:
git revert -n [commit-id] # 使用新的commit回滚之前的commit,之前的commit提交都会保留(迷惑脸?)
git push
git拉取远程分支并创建本地分支
方式一:
git checkout -b <localbranchname> origin/<branchname>
方式二:
git fetch origin <branchname>:<localbranchname>
git checkout <localbranchname>
git cherry-pick
命令的作用,就是将指定的提交(commit)应用于其他分支。
git cherry-pick commit-id|branchname # 可使用commit-id 或 分支名
git cherry-pick <HashA> <HashB> ... # 一次应用多个提交
git cherry-pick <HashA>..<HashB> # 应用A到B之间的连续多个提交
git cherry-pick <HashA>^..<HashB> # 应用A到B之间的连续多个提交,包含HashA的提交
git 导出文件
git archive --format tar.gz|tar|zip|tgz --output "./output.tar.gz" master
子模块
git submodule add <url> <Path> # url为字模块路径 path为字模块存储路径
#克隆项目后,默认子模块目录下无任何内容。需要在项目根目录执行如下命令完成子模块的下载:
git submodule update --init --recursive
其他的一些
git pull origin <branchname>:<branchname> # 将远程分支合并到当前本地分支,冒号后面表示本地分支
git commit --amend -m "msg" # 修改的文件已被git commit,但想再次修改不再产生新的Commit,使用新message提交
git commit --amend --no-edit # 同上,但使用上一次message提交
git push -f (修改已push过的message)
git merge <branchname>|commit-id # 合并某个分支或某次提交 (git log查看commit_id)
git merge --abort # 抛弃当前合并冲突的处理过程并尝试重建合并前的状态
git branch -D <branchname> # 删除本地分支
git push origin [-d <branchname>|:<branchname>] # 删除远程分支
git stash # 切换分支的时候,正在修改的东西不想提交或者被携带至新分支,可以使用stash存起来
git stash list # 显示保存的工作进度列表
git stash pop stash@{num} # 恢复工作进度到工作区 (会将不同分支的内容恢复出来)
git stash clear # 清除所有stash缓存
git stash drop stash@{num} # 清除指定stash缓存
git diff --cached [<path>...] # 比较暂存区与最新本地版本库(本地库中最近一次commit的内容)
git diff commit-id [<path>...] # 比较工作区与指定commit-id的差异
git diff --cached [<commit-id>] [<path>...] # 比较暂存区与指定commit-id的差异
git diff [<commit-id>] [<commit-id>] # 比较两个commit-id之间的差异
git log --online # 一行展示log信息,对信息量大时显示友好
git log --merges # 查看merge记录
git blame filename # 查看文件历史改动记录
Git命令行别名配置
配置: git config --global alias.st "status"
使用: git st 即 git status
bash别名配置: alias gst="git status"
使用: gst 即 git status
通过文件设置:
方式一:
vi ~/.gitconfig
[alias]
st = status
cmsg = commit -m
使用: git st
方式二:
vi ~/.bash_profile
alias gst="git status"
alias gcmsg="git commit -m"
source ~/.bash_profile
使用:gst
参考链接: