Git操作——常见问题

1.error: The following untracked working tree files would be overwritten by merge。可能在如下情况中碰到:(本地文件未被跟踪,不想添加,执行git pull操作以更新代码)

MyLib git:(master) ✗ git pull
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 11 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (11/11), done.
From http://127.0.0.1:8888/libs/MyLib
4c89081..6db8e1d master -> origin/master
Updating 4c89081..6db8e1d
error: The following untracked working tree files would be overwritten by merge:
MyLib/MyServiceHelper.h
MyLib/MyServiceHelper.m
Please move or remove them before you merge.
Aborting

解决方法如下:

git fetch --all  
git reset --hard origin/master

2.nothing added to commit but untracked files present
(How to remove local untracked files from the current Git branch?)
说明文件未被跟踪,如果想移除本地分支的未被跟踪的文件,可使用如下命令

git clean -f -d //remove directories
git clean -fd //remove directories
git clean -f -X //remove ignored files
git clean -fX //remove ignored files
git clean -f -x //remove ignored and non-ignored files
git clean -fx //remove ignored and non-ignored files

由这些命令可以看出,它使用了git的clean选项(不要尝试把未被跟踪的文件加入.gitignore中,以免以后需要添加这种类型的文件时导致未被添加成功的问题)

3.如果tag被删除,在其它commit上重新添加tag。如果其它用户之前已经拉取了分支,运行git fetch时不会更新tag的位置。他的做法应该是把本地tag删除,重新fetch,如:

git tag -d 1.0.0 //删除本地tag
git fetch //更新代码,连同tag也会更新

Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
error: The last gc run reported the following. Please correct the root cause
and remove .git/gc.log.
Automatic cleanup will not be performed until the file is removed.
warning: There are too many unreachable loose objects; run 'git prune' to remove them.

fatal: bad object refs/remotes/origin/HEAD
error: failed to run repack

运行git branch -a命令,提示

warning: ignoring broken ref refs/remotes/origin/HEAD

说明远程分支被删除了
编辑.git/refs/remotes/origin/HEAD文件中的指向分支即可

运行git fsck命令,检查仓库一致性,可发现有一坨悬空对象(dangling blob),这是之前add未及时提交的对象,如下

dangling blob 44139c221373873c7047196a6ebdf317cacbcfa9
dangling blob 6b1f485eea0714e2e32ceb56f277cb62ad087567
dangling blob 5039b06948d28c0beb4a6246a944018a5a1cc4d0
dangling blob 5339b0a54fa752b2b48ede4276291c7825cff151
dangling blob 4f5204040eb16dfb3d1bd20da03d4aeb9f635a35
dangling commit b855d814ef0a69ad5497a98bee0dacd4d9f61f83
dangling blob d2816405b83f9100197a7c21dd6c9568590f8ed5
dangling blob 528d448521440572a9824051881d5ab7e0518249
dangling blob dc9318982aff876e2ac06ef0a17c7d4c64a4e171
dangling blob 0b9a98b2d4e7481d4c939d2a2b3099fcbf3d3357
dangling blob 16a35cfe30d961963719f40b65f20849b464c9a1
dangling blob cda36c45d9b725618030caaa2b9a56220c1e373d
dangling blob 32a6483977bf44ceafe15cfee157637d83c6a43d
dangling blob 14ad142ff84890c4fd6f5c05033fe018d5163b91
dangling blob 75b36879649dc0be05edaef571e3ec4d276ce5a3
dangling blob f6d5bcbada8865913802594bca3b8626da6d4133

运行git prune命令即可,或者直接运行git gc --prune=now把所有的悬空对象都清空

参考文档:
1.http://ageekandhisblog.com/git-force-overwrite-of-untracked-working-tree-files/
2.https://koukia.ca/how-to-remove-local-untracked-files-from-the-current-git-branch-571c6ce9b6b1
3.http://www.01happy.com/git-remotes-origin-head-missing/?utm_source=tuicool

你可能感兴趣的:(Git操作——常见问题)