git使用中常用的命令

用到命令

  1. 克隆代码 git clone https://github.com/mrxiaoyu100001/Android-.git
  2. git add . // 添加本地缓存区
  3. git commit -m ‘备注’ //推送到本地库
  4. git push origin //推送到远程库
  5. git log -p -2 // 查看最近两次提交版本的日志
  6. git diff //比较工作区与暂缓区的不同
  7. git diff --cache //比较暂缓区与本地库的不同
  8. git diff – HEAD // 比较工作区与本地库的不同
  9. git diff commit-id //比较暂缓区与 commit-id的不同
  10. git rebase b //把b合并到当前分支
  11. git merge b //同样把b合并到当前分支
  12. git fetch project-name //从另一个项目下载对象和引用
  13. git fetch //同步远程远程分支
  14. git branch -a // 查看所有分支
  15. git branch -r //查看远程分支
  16. git remote add project_name ssh://git@git.sprucetec.com:50022/procurement1601/supplier_app.git //添加远程库到本地
  17. git fetch project-name // 下载远程库更新对象
  18. git checkout master //切换到主分支
  19. git rebase project-name //将远程分支的代码rebase到本地master分支上
  20. git regest //回退提交本地库的文件
  21. git checkout // 把当前修改的文件从HEAD中迁出并变成为修改的样子
  22. git rebase --continue //继续合并
  23. git rebase --skip //跳过这个冲突继续合并
  24. git rebase --abort //
  25. git reset --hard origin/master //主分支放弃这个修改
  26. git stash //保存当前修改,恢复到上次提交的版本
  27. git stash list //stash栈中stash列表
  28. git stash pop stash@{id}
  29. git stash apply stash@{id} //将制定版本取出来
  30. git stash clear // 清除stash栈
  31. git rm -r --cached . //清除本地缓存
  32. git log -p master… origin/master //比较本地仓库和远程仓库的区别
  33. git reflog //查看提交日志
  34. git reset commitId //回退某个版本
  35. git reset filename //回退某个文件
  36. git remote prune origin //删除本地无效的远程分支
  37. git branch -d [branch_name] //删除本地分支
  38. git push origin --delete [branch_name] //删除远程分支
  39. git remote add remoteName https://xxxxxxxxx.git // 关联远程分支的源
  40. git pull remoteName branchName // 更新源远程库代码
  41. git push origin dev //远程创建dev分支
  42. git log --author=“username” --pretty=tformat: --numstat | awk ‘{ add += $1; subs += $2; loc += $1 - $2 } END { printf “added lines: %s, removed lines: %s, total lines: %s\n”, add, subs, loc }’ - //查看代码量
  43. Git diff branch1 branch2 --stat //显示出所有有差异的文件列表
  44. Git diff branch1 branch2 文件名(带路径) //显示指定文件的详细差异
  45. Git diff branch1 branch2 //显示出所有有差异的文件的详细差异
  46. git fetch //同步远程分支到本地

博客备份:
http://blog.csdn.net/bdss58/article/details/50363830
https://blog.csdn.net/sinat_29774479/article/details/78599702
https://blog.csdn.net/wh_19910525/article/details/7784901
本地文件修改与远程库有冲突
http://yijiebuyi.com/blog/5b55eb51ad49ce41e2de9c85dd4513ca.html
https://blog.csdn.net/lhh1113/article/details/71038154


遇到问题
1. Pull is not possible because you have unmerged files.

  症状:pull的时候
  
  $ git pull

Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm ’
as appropriate to mark resolution, or use ‘git commit -a’

应该是因为local文件冲突了

解决方法:

引用――

1.pull会使用git merge导致冲突,需要将冲突的文件resolve掉 git add -u, git commit之后才能成功pull.

2.如果想放弃本地的文件修改,可以使用git reset --hard FETCH_HEAD,FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull.
注意:

git merge会形成MERGE-HEAD(FETCH-HEAD) 。git push会形成HEAD这样的引用。HEAD代表本地最近成功push后形成的引用。

就我的经验,有时候会莫名其妙地出现这种状况,而且Untracked files 还特别多(实际上自己可能只改了一两个文件),所以只好先保存好自己确定做出的local的修改,然后用git reset --hard FETCH_HEAD回到上次成功pull之后的点,然后再pull就没有问题了
2. You are not currently on a branch.

症状:有一次pull的时候又出现冲突,这回用“git reset --hard FETCH_HEAD”方法都不行了,出现:

$ git pull
You are not currently on a branch, so I cannot use any
‘branch..merge’ in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull ').
See git-pull(1) for details.
解决方法:

首先git checkout -b temp

其次git checkout master

即可恢复到master repository的状态,然后就可以pull了

3. error: The following untracked working tree files would be overwritten by merge:xxx/.idea/misc.xml
解决方案:将文件删除或者移到其他位置, 然后 在执行 git pull 更新

4. There is no tracking information for the current branch.Please specify which branch you want to merge with.See git-pull(1) for details. git pull If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/ dev

5. Nin9ty:xxxx xiaoyu$ git push fatal: The current branch dev has noupstream branch.To push the current branch and set the remote as upstream, use git push --set-upstream origin dev

原因: 没有将本地的分支与远程仓库的分支进行关联。
解决:
第一种如上提示:git push –set-upstream origin master。其中的origin是你在clone远程代码时,git为你创建的指向这个远程代码库的标签,它指向repository。为了能清楚了解你要指向的repository,可以用命令git remote -v进行查看。master为你远程的分支,可以用git branch -a进行查看。然后确定好这两个值后,将命令中标粗的参数换掉即可。
另一种方法是:git push -u origin master。同样根据自己的需要,替换标粗的参数。
6.error: Your local changes to the following files would be overwritten by merge:mn_supplier/app/src/main/java/com/supplier/ordermsg/OrderMsgFragment.ktPlease commit your changes or stash them before you can merge.
问题原因:远程库的代码根本地代码有冲突,有过修改
解决方案:有两种方法

  1. 将现在的修改的文件保存到 stash 栈 代码比对之后 作处理
    git stash
    git pull
    git stash list
    git stash pop stash@{id}
    git diff -w filename //查看两个项目合并情况
  2. 直接将代码覆盖
    git reset --head
    git pull
    其中git reset是针对版本,如果想针对文件回退本地修改,使用
    git checkout HEAD file/to/restore

3.保留你本地的修改

git merge --abort

git reset --merge

合并后记得一定要提交这个本地的合并

然后在获取线上仓库

git pull

**7 $ git add .
warning: LF will be replaced by CRLF in app/app.iml.
The file will have its original line endings in your working directory.
**
原因是路径中存在 / 的符号转义问题,false就是不转换符号默认是true,相当于把路径的 / 符号进行转义,这样添加的时候就有问题
解决方法:
git config --global core.autocrlf false
就可以解决了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值