git使用中常用的命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NotesChapter/article/details/79627669

用到命令
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 branch -a // 查看所有分支
14. git branch -r //查看远程分支
15. git remote add project_name ssh://[email protected]:50022/procurement1601/supplier_app.git //添加远程库到本地
16. git fetch project-name // 下载远程库更新对象
17. git checkout master //切换到主分支
18. git rebase project-name //将远程分支的代码rebase到本地master分支上
19. git regest //回退提交本地库的文件
20. git checkout // 把当前修改的文件从HEAD中迁出并变成为修改的样子
21. git rebase –continue //继续合并
22. git rebase –skip //跳过这个冲突继续合并
23. git rebase –abort //
24. git reset –hard origin/master //主分支放弃这个修改
25. git stash //保存当前修改,恢复到上次提交的版本
26. git stash list //stash栈中stash列表
27. git stash pop stash@{id}
28. git stash apply stash@{id} //将制定版本取出来
29. git stash clear // 清除stash栈
30. git rm -r –cached . //清除本地缓存
31. git log -p master.. origin/master //比较本地仓库和远程仓库的区别
32. git reflog //查看提交日志
33.git reset commitId //回退某个版本
34. git reset filename //回退某个文件
35. git remote prune origin //删除本地无效的远程分支
36. git remote add remoteName https://xxxxxxxxx.git // 关联远程分支的源
37. git pull remoteName branchName // 更新源远程库代码
38. git pull origin dev //远程创建dev分支

博客备份:
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

猜你喜欢

转载自blog.csdn.net/NotesChapter/article/details/79627669