I am attempting to make use of this gist in my workflow as post-merge and post-checkout git hooks.
#!/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
# Example usage
# In this example it's used to run `npm install` if package.json changed
check_run package.json "npm install"
This claims to only run npm install if the package.json file is changed.
However on all the machines I have tried this on. The npm install command runs regardless of whether package.json has been changed or not.
To test this I have been creating a new branch at my current commit and then checking it out, thus triggering the post-checkout git hook. I would not expect npm install to run because the package.json is unchanged.
Visual Proof (note the npm warning text):
ORIG_HEAD should be replaced with HEAD#{1} as noted in this question ORIG_HEAD is an older, less reliable way to supposedly get the previous state of HEAD. In my case it was not being set.
TL;DR
Use a different post-checkout hook, that uses $1 instead of ORIG_HEAD. (Or, check the number of arguments to decide whether you are being invoked as the post-checkout or post-merge hook, to get the same effect. Or, if you know that reflogs are always enabled, use HEAD#{1} to get the previous value of HEAD.)
Discussion
Using ORIG_HEAD in a post-merge hook makes sense, because git merge sets ORIG_HEAD to the commit that was current before the merge. (If the merge was a true merge, rather than a fast-forward, the commit identified by MERGE_HEAD and the commit identified by HEAD^1 are necessarily identical. If the merge was a fast-forward, however, only MERGE_HEAD and the reflog will be able to locate the previous commit hash that was stored in HEAD before the merge.)
Using ORIG_HEAD in a post-checkout hook, however, is blatantly wrong, because git checkout does not set ORIG_HEAD. This means that if ORIG_HEAD even exists at all, it effectively points to some random commit. (Of course, it actually resolves to whatever commit was left in it by whatever command last updated it: git merge, git rebase, or any other command that writes to ORIG_HEAD. But the point here is that it does not have any relationship to the commit that was current before the checkout.) A post-checkout hook:
is given three parameters: the ref of the
previous HEAD, the ref of the new HEAD (which may or may not have
changed), and a flag indicating whether the checkout was a branch
checkout (changing branches, flag=1) or a file checkout (retrieving a
file from the index, flag=0). This hook cannot affect the outcome of
git checkout.
(That last sentence is not quite right. Although the post-checkout hook cannot stop checkout from having updated the index and work-tree, it can overwrite various work-tree or index contents, and if it produces a failure exit status, it causes git checkout itself to also produce a failure exit status.)
What this all means is that you need to take a different action in a post-checkout hook: use $1, the first parameter, to get the hash ID of the previous HEAD. Note that in exotic cases,1 the post-checkout hook is run on the initial git clone, so $1 can be the null-ref. (I'm now curious as to what it is when you use git checkout --orphan and then don't create the new branch, as well. It seems likely that $1 will be the null-ref here too.)
1The only way to get a post-checkout hook to run on git clone is to have git clone install the post-checkout hook. This is normally impossible, but can be done by pointing your Git to your own template directories that have actual hooks instead of just sample hooks.
Torek mentions:
This hook cannot affect the outcome of git checkout.
That last sentence is not quite right.
Although the post-checkout hook cannot stop checkout from having updated the index and work-tree, it can overwrite various work-tree or index contents, and if it produces a failure exit status, it causes git checkout itself to also produce a failure exit status.
That is now (Q4 2020, 3 years later) officially documented with With Git 2.29:
See commit 3100fd5 (27 Aug 2020) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 2f1757e, 03 Sep 2020)
doc: clarify how exit status of post-checkout hook is used
Because the hook runs after the main checkout operation finishes, it cannot affect what branch will be the current branch, what paths are updated in the working tree, etc., which was described as "cannot affect the outcome of 'checkout'".
However, the exit status of the hook is used as the exit status of the 'checkout' command and is observable by anybody who spawned the 'checkout', which was missing from the documentation.
Fix this.
githooks now includes in its man page:
This hook cannot affect the outcome of git switch or git checkout,
other than that the hook's exit status becomes the exit status of these two commands.
Related
I face a merge conflict when I would like to rebase the develop branch against my feature branch. I use IntelliJ Idea to execute individual Git commands. Let's say I have 3 pushed commits and I want to squash and rebase them using --interactive mode.
I select all three commits, I select squash, do some rewording, and hit Rebase.
The process fails and an Excel file that appears in the Git panel among Changes and on the diff panel, says the contents are the same. I can only abort or retry the rebase, but continuing doesn't make sense as the change always prevents me to continue.
Here is the console output:
12:50:42.385: [myapplication] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false -c core.commentChar= rebase --interactive develop
Rebasing (1/2)
error: Your local changes to the following files would be overwritten by merge:
excel-file.xlsx
Please commit your changes or stash them before you merge.
Aborting
hint: Could not execute the todo command
hint:
hint: fixup 37d1fc57dff9c7e9a312c88cf78c176cb71dbb47 CommitMessageIsHere
hint:
hint: It has been rescheduled; To edit the command before continuing, please
hint: edit the todo list first:
hint:
hint: git rebase --edit-todo
hint: git rebase --continue
Could not apply 37d1fc5... CommitMessageIsHere
Is there a trick to overcome this obstacle? Is possible to somehow force ignoring and skipping such merge conflict?
You started your rebase with un untracked copy of excel-file.xlsx, and when git met a commit which included the action "create a new excel-file.xlsx file", he stopped.
You have to somehow put aside this untracked version before proceeding.
The simplest way is:
rename that file on disk:
mv excel-file.xlsx excel-file.xlsx.tmp
run git rebase --continue:
either IntelliJ has a button which looks like "proceed with the rebase", or you can run this command from the command line,
once the rebase is complete:
you can compare the contents of both files, and decide what the correct action is -- update the tracked version, delete the tmp file ...
I am using the git mergetool command to fix conflicts. However I have thousands of conflicts, is there way to simplify this so I get everything from the remote?
I am asked to enter c, d or a in the command.
{local}: deleted
{remote}: created file
Use (c)reated or (d)eleted file, or (a)bort?
Since I have thousands of files, I don't want to keep sending c. Is there way to just do this in bulk?
You can solve this outside of git mergetool: run git status --porcelain to get a list of all unmerged files and their states in machine-readable format.
If your Git is new enough, it will support --porcelain=v2. See the git status documentation for details on the output formats. Output format v2 is generally superior for all purposes, but you should be able to make do with either one.
Next, you must write a program. Unfortunately Git has no supplied programs for this. Your program can be fairly simple depending on the specific cases you want to solve, and you can use shell scripting (sh or bash) as the programming language, to keep it easy.
Since you're concerned about the cases where git mergetool says:
Use (m)odified or (d)eleted file, or (a)bort?
you are interested in those cases where the file name is missing in the stage 1 ("base") version and also missing in the stage 2 ("local") version, but exists in the stage 3 ("remote") version. (See the git status documentation again and look at examples of your git status --porcelain=v2 output to see how to detect these cases. Two of the three modes will be zero.) For those particular path names, simply run git add on the path name to mark the file as resolved in favor of the created file.
Once you have marked all such files, you can go back to running git mergetool to resolve additional conflicts, if there are any.
Note that your "program" can consist of running:
git status --porcelain=v2 > /tmp/commands.sh
and then editing /tmp/commands.sh to delete all but the lines containing files that you want to git add. Then change all of those lines to read git add <filename> where <filename> is the name of the file. Exit the editor and run sh /tmp/commands.sh to execute all the git add commands. That's your program!
supposing you want their change and modified yours you can do a pull as like:
git pull -X theirs
Other stackOverflow answers
git pull -X
git merge strategies this link will help understand any other merge strategies for the futuro
If you want that all the change you did will be deleted and you will be sync with the remote.
You should do the following:
git stash
git pull
And if you want to restore the change you did you should type:
git stash pop
Basically 'git stash' is moving the change to a temp repository.
you can learn more in:
NDP software:: Git Cheatsheet
I noticed with git log origin/master that new a commit is up.
So I want to "see" this commit on my local repo.
I do this
$ git fetch -v
From xxx.xxx:proj/test
= [up to date] master -> origin/master
Everything seems fine... But nothing has changed on my local repo !?
To update your branches (as opposed to your Git's memory of some other Git's branches—git fetch updates this memory only), you must run a second Git command.
The second command to run is sometimes git merge and sometimes git rebase, depending on how you prefer to work. If you have no preference yet, use either one until you do have a preference.
There is a convenience command spelled git pull, that runs both git fetch and then the second command. The second command it runs is the one you tell it to. You must configure it based on your choice of second command to use. I recommend avoiding this until you really understand what the second command is and does, because eventually something will go wrong when running the second command. If you do not know that git pull is running this second command, you will not only not know what to do about this failure ... you won't even know that you need to look for how to fix problems with the other command!
I made a checkout to the previous commit and all local changes (neither staged nor commited) were gone. Is there a way to recover those local changes?
Sergey - there are only a handful of commands that can nuke your working directory changes. git checkout -f is one of them (git reset --hard is another). I would not recommend you ever use that form as your default. Use git checkout instead. If that fails (which it will if you have changes), then use git status and git diff to look at the changes you have, and either save them off (commit or stash), or explicitly throw them out.
How can I check if git has successfully cloned a repository, and based on that result, execute commands inside the bash script?
I was trying some combinations of grep checking the output of git status but I've only managed to confuse myself more.
I'm ruining timeout 60s git clone ... so I must make sure the repository has cloned fully, and if it has not to skip whatever it would have done with the cloned data.
Have a look at it.
I think You are expecting this code.
https://stackoverflow.com/a/13715406/2959196
Also Have a look at the conversation. It might help you better.
How to detect if a git clone failed in a bash script
Your timeout command will return a non zero exit code if it terminates the program. Use that rather than checking the repository to see if it's cloned.