git 的使用 二(git的常用命令汇总)

1、代码提交的相关命令

git clone 远程仓库地址 从远程仓库下载代码
git status . # 查看当前工作区的修改
git add * # 跟踪新文件
git add -u [path] # 添加[指定路径下]已跟踪文件

rm *&git rm * # 移除文件
git rm -f * # 移除文件
git rm --cached * # 停止追踪指定文件,但该文件会保留在工作区
git mv file_from file_to # 重命名跟踪文件

git log # 查看提交记录

git commit # 提交更新    
git commit [file1] [file2] ... # 提交指定文件    
git commit -m 'message'
git commit -a # 跳过使用暂存区域,把所有已经跟踪过的文件暂存起来一并提交
git commit --amend#修改最后一次提交
git commit -v # 提交时显示所有diff信息

git reset HEAD *#取消已经暂存的文件
git reset --mixed HEAD *#同上
git reset --soft HEAD *#重置到指定状态,不会修改索引区和工作树
git reset --hard HEAD *#重置到指定状态,会修改索引区和工作树
git reset -- files#重置index区文件

git revert HEAD #撤销前一次操作
git revert HEAD~ #撤销前前一次操作
git revert commit ## 撤销指定操作

git checkout -- file#取消对文件的修改(从暂存区——覆盖worktree file)
git checkout branch|tag|commit -- file_name#从仓库取出file覆盖当前分支
git checkout -- .#从暂存区取出文件覆盖工作区

git diff file #查看指定文件的差异
git diff --stat #查看简单的diff结果
git diff #比较Worktree和Index之间的差异
git diff --cached #比较Index和HEAD之间的差异
git diff HEAD #比较Worktree和HEAD之间的差异
git diff branch #比较Worktree和branch之间的差异
git diff branch1 branch2 #比较两次分支之间的差异
git diff commit commit #比较两次提交之间的差异

git log --grep=aplog #查找关键字aplog

git log #查看最近的提交日志
git log --pretty=oneline #单行显示提交日志
git log --graph # 图形化显示
git log --abbrev-commit # 显示log id的缩写
git log -num #显示第几条log(倒数)
git log --stat # 显示commit历史,以及每次commit发生变更的文件
git log --follow [file] # 显示某个文件的版本历史,包括文件改名
git log -p [file] # 显示指定文件相关的每一次diff
git log --oneline #只用一行显示每次的提交包含哈希号和commit内容 

git stash #将工作区现场(已跟踪文件)储藏起来,等以后恢复后继续工作。
git stash list #查看保存的工作现场
git stash apply #恢复工作现场
git stash drop #删除stash内容
git stash pop #恢复的同时直接删除stash内容 最好不要这么干,除非你下次确认不会再使用缓存数据
git stash apply stash@{0} #恢复指定的工作现场,当你保存了不只一份工作现场时。

2.分支操作的相关命令

git branch #列出本地分支
git branch -r#列出远端分支 为了方便查看,前面会有一个*号显示
git branch -a#列出所有分支
git branch -v#查看各个分支最后一个提交对象的信息
git branch --merge#查看已经合并到当前分支的分支
git branch --no-merge#查看为合并到当前分支的分支
git branch test#新建test分支
git branch branch [branch|commit|tag] # 从指定位置出新建分支
git branch --track branch remote-branch # 新建一个分支,与指定的远程分支建立追踪关系
git branch -m old new #重命名分支
git branch -d test #删除test分支
git branch -D test #强制删除test分支 可以多分支同时删除
git branch --set-upstream dev origin/dev #将本地dev分支与远程dev分支之间建立链接

git checkout test#切换到test分支
git checkout -b test#新建+切换到test分支
git checkout -b test dev#基于dev新建test分支,并切换

git merge test#将test分支合并到当前分支
git merge --squash test ## 合并压缩,将test上的commit压缩为一条

git cherry-pick commit #拣选合并,将commit合并到当前分支,比如我们之前在dev分支下做了一个修改,然后切换到master下面来,同步了最新的代码,可以通过cherry-pick把之前的提交同步过来
git cherry-pick -n commit #拣选多个提交,合并完后可以继续拣选下一个提交

git rebase master#将master分之上超前的提交,变基到当前分支
git rebase --onto master 169a6 #限制回滚范围,rebase当前分支从169a6以后的提交
git rebase --interactive #交互模式    
git rebase --continue# 处理完冲突继续合并    
git rebase --skip# 跳过    
git rebase --abort# 取消合并

3、远程分支的相关命令

git fetch origin remotebranch[:localbranch]# 从远端拉去分支[到本地指定分支]
git merge origin/branch#合并远端上指定分支
git pull origin remotebranch:localbranch# 拉去远端分支到本地分支
git push origin branch#将当前分支,推送到远端上指定分支
git push origin localbranch:remotebranch#推送本地指定分支,到远端上指定分支
git push origin :remotebranch # 删除远端指定分支
git push origin remotebranch --delete # 删除远程分支
git branch -dr branch # 删除本地和远程分支
git checkout -b [--track] test origin/dev#基于远端dev分支,新建本地test分支[同时设置跟踪] 

4、git查看历史以及版本回退

git log

git reset --hard 之前提交的某个版本号(版本号即为下图的标记的东西)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值