I use the following commands to push local branch changes to remote branch (in some automated script):
#while being on main branch
git checkout -B new_local_branch_unix_timestamp
#optionally modify some files
# check if there are modified files
git diff --exit-code
# if there are changes commit and push
git commit -am 'commit message'
git push --set-upstream origin new_local_branch_unix_timestamp
# get latest local commit id
git rev-parse HEAD
# busy wait until latest remote commit id equal to latest local commit id
# latest remote commit id is extracted using
git rev-parse origin/new_local_branch_unix_timestamp
Busy wait means applying git rev-parse origin/new_local_branch_unix_timestamp fixed number of times until local last commit id equals remote last commit id.
Running git rev-parse origin/new_local_branch_unix_timestamp to get latest remote commit id occasionally returns below output and error.
stdout: origin/new_local_branch
sterr: fatal: ambiguous argument 'origin/new_local_branch_unix_timestamp': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
It's very rare and random.
I always mitigate it by removing local repository, recloning remote one. Then the above steps succeed until the next time.
Is there a reason why it happens and is there a better way to mitigate it?
There could be many remote branches created earlier with similar names (having similar first characters, because of unix timestamps similarity).
Could this be the reason for sterr: fatal: ambiguous argument git error?
Your local Git will create or update
origin/*names based on branch name existence (and hash IDs found) in the other Git repository over atorigin. If you setfetch.prunetotrueor usegit fetch --pruneorgit remote update --prune, your local Git will also deleteorigin/*names based on the other Git's branch names.The updating happens "opportunistically". That is, your Git gets, from their Git, a list of some or all of their branch names and hash IDs, on some operations: particular
git fetch(orgit remote update, which is largely the same code). If your Git has, or obtains, the appropriate commits at this time, your Git is then able to create or update your remote-tracking names at this time, and does so.In very old Git versions (predating 1.8.4), some updating does not happen even when the opportunity crops up and you must use an unrestricted
git fetchto force the update to occur.The error you are seeing occurs when your local Git repository has not created the
origin/*remote-tracking name corresponding to some branch name you expected your Git to have seen. In that case, runninggit fetch(with or without--prune) orgit remote updateshould fix the problem.You have a rather abstract comment:
What's not clear is how you implement this "busy wait": whether it uses
git fetch, and if so, whether it uses a restrictedgit fetch. If you are using a restrictedgit fetchand have a particularly ancient Git, you'll definitely see issues. Even if you're using an unrestrictedgit fetchand have a modern Git, I can envision races here, depending on how you've implemented this operation, which you have not described.