重新设置git push [英] reset hard on git push

查看:561
本文介绍了重新设置git push的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个post-receive钩子脚本,坐在远程仓库上,我推着去做一个 git reset --hard

I have a post-receive hook script sitting on the remote repo I am pushing to that does a git reset --hard

类似这样:

Something like this:

$ git push opal
Counting objects: 74, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (45/45), done.
Writing objects: 100% (53/53), 16.68 KiB, done.
Total 53 (delta 20), reused 0 (delta 0)
remote: warning: updating the current branch
remote: HEAD is now at 88f1e35 tweak lavalamp styles

我在这里不理解的是 - 远程表示头现在在XXX处,但是当我登录到服务器时 - 远程工作副本根本没有更新!

What i don't understand here is - the remote says head is now at XXX but when i log into the server - the remote working copy is not updated at all!

有什么想法?

any idea?

推荐答案

首先,钩子脚本在其当前工作目录下运行,并设置为Git目录本身(即 .git / 非裸仓库的目录)。其次,钩子脚本使用GIT_DIR环境变量集运行,并指向Git仓库(同样,非裸仓库的 .git / 目录)。

First, hook scripts run with their current working directory set to the Git directory itself (i.e. the .git/ directory of a non-bare repository). Second, hook scripts run with the GIT_DIR environment variable set and pointing to the Git repository (again, the .git/ directory of a non-bare repository).

通常,如果您尝试从 .git / git reset --hard

Normally, if you try to run git reset --hard from the .git/ directory, it will die with the following message:

fatal: This operation must be run in a work tree

但是当GIT_DIR被设置时,Git命令会假定当前目录是工作树。由于钩子运行时的当前目录是 .git / 目录,因此您的 git reset --hard 实际上是直接检查你的工作树文件到 .git / 而不是它的父目录中(即你现在在你的中有你的版本化内容的副本。 git / 目录)。

But when GIT_DIR is set, Git commands assume that the current directory is the working tree. Since the current directory when the hook runs is the .git/ directory, your git reset --hard is actually "checking out" your working tree files directly into .git/ instead of its parent directory (i.e. you now have a copy of your versioned content in your .git/ directory).

希望版本库中没有版本化的内容的路径名与 Git在Git仓库中使用的路径名。如果它们一致,那么你的 git reset --hard 会覆盖你的仓库的一些内部结构,你可能想重新从其他仓库克隆它库。如果您确信所有版本化的内容都不会与Git的内部路径名冲突,那么您可以通过以下方式进行清理:

Hopefully none of versioned content in your repository has pathnames that coincide with pathnames that Git uses in Git repositories themselves. If they do coincide, then your git reset --hard will have overwritten some bit of the internal structure of your repository and you will probably want to re-clone it from some other repository. If you are confident that none of the versioned content conflicts with Git’s internal pathnames, then you may be able to clean it up with this:

# make a backup of your repository first!
(cd .git && GIT_DIR=$PWD git ls-files -cz | xargs -0 rm)

这只会删除当前被跟踪的文件(它会留下后面被移除的文件,但是一旦在断开的钩子处于活动状态时被推入提示提交中进行跟踪)。

This will only remove currently tracked files (it will leave behind files that have since been removed, but were once tracked in tip commits pushed while the broken hook was active).

一个解决方案是在调用Git命令之前将当前工作目录更改为正常工作树并取消设置GIT_DIR和GIT_WORK_TREE。

One solution is to change the current working directory to the normal working tree and unset GIT_DIR and GIT_WORK_TREE before calling Git commands.

⋮
test "${PWD%/.git}" != "$PWD" && cd .. 
unset GIT_DIR GIT_WORK_TREE
# you can now safely use Git commands
⋮

另一个解决方案是显式重置GIT_DIR,在那里设置GIT_WORK_TREE和chdir。 Git常见问题为什么我看不到变化在git push之后的远程回购?建议 post-update 脚本就可以做到这一点。链接的脚本也更加安全,因为如果索引或工作树在进行硬重置之前变脏,它会进行存储。

Another solution is to explicitly reset GIT_DIR, set GIT_WORK_TREE and chdir there. The Git FAQ "Why won't I see changes in the remote repo after "git push"?" recommends a post-update script that does just this. The linked script is also much safer since it makes a stash if the index or working tree is dirty before doing the hard reset.

这篇关于重新设置git push的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆