Git推动非裸回购,保存当前工作树 [英] Git pushing to non-bare repo, save current worktree

查看:114
本文介绍了Git推动非裸回购,保存当前工作树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何推送到非纯粹的git存储库,自动添加并提交工作树中的任何更改,然后重新签出当前分支以反映该推送的更改?



我在想这样的事情:

在远程(非裸回购)上添加一个钩子来运行 git add。 &安培;&安培; git commit -m自动提交&& git reset --hard



这种方法有什么缺点吗?还有另一种方法吗?






(声明:我知道这不是最理想的情况,但这是我们公司的事情,我希望尽可能精简它,而不需要每个人都完全改变他们的工作方式)。 >

解决方案

经过搞乱之后,我发现了一个相当不错的解决方案。


预接收钩子
检查工作目录中的任何更改,添加/提交它们,然后提醒用户他/她需要在推送前合并

 #!/ bin / sh 
cd ../
unset GIT_DIR
CHANGED = $(git diff-index --name-only HEAD - )
if [-n$ CHANGED];然后
git add -u。
git commit -m自动提交
echo工作目录中有未提交的更改...请将它们再次提交并再次进行更改
exit 1
fi

邮寄接收挂钩:强制检出当前分支,在工作目录中显示。这将覆盖工作目录中的所有更改,但这些更改将由预接收钩子添加/提交/合并。

 #!/ bin / sh 
cd ../
unset GIT_DIR
branch = $(git branch | sed -n -e's / ^ \ * \(。* \)/ \ 1 / p')

git checkout -f $ branch


echo更新推送到分支$分支并签出网站目录


How can I push to a non-bare git repository, automatically add and commit any changes in the working tree, and then re-checkout the current branch to reflect the changes from that push?

I was thinking of something like this:

Add a hook on the remote (the non-bare repo) to run git add . && git commit -m "Automated commit" && git reset --hard

Are there any drawbacks to that method? Is there another way of doing this?


(Disclaimer: I know that this isn't the most ideal situation, but this is what we have at my company and I want to make it as streamlined as possible without needing everyone to completely change the way they do things)

Thanks!

解决方案

After messing around I found a pretty good solution.

Pre-receive hook: Checks for any changes in working directory, adds/commits them, then alerts the user he/she needs to merge before pushing

#!/bin/sh
cd ../
unset GIT_DIR
CHANGED=$(git diff-index --name-only HEAD --)
if [ -n "$CHANGED" ]; then
    git add -u .
    git commit -m "Automated commit"
    echo "There were uncommitted changes in the working directory...please pull them and push your changes again"
    exit 1
fi 

Post-receive hook: Force checks-out the current branch so changes will be shown in working directory. This will overwrite any changes in the working directory, but those will have already been added/committed/merged by the pre-receive hook.

#!/bin/sh
cd ../
unset GIT_DIR
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')

git checkout -f $branch


echo "Update pushed to branch $branch and checked out in the website directory" 

这篇关于Git推动非裸回购,保存当前工作树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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