I need to do a subtree merge for a specific branch, if it exists on a given remote repository. The problem is that the remote repository is not checked out locally, so I can
Then no need to manually pass the repository name everytime.
git ls-remote origin <branch>
Instead of
git ls-remote <full repo url> <branch>
Example :
git ls-remote git@bitbucket.org:landmarkgroupme/in-store-application.git uat_21dec
OR
git ls-remote origin uat_21dec
Both will give same output :
More on Origin : Git has the concept of "remotes", which are simply URLs to other copies of your repository. When you clone another repository, Git automatically creates a remote named "origin" and points to it. You can see more information about the remote by typing git remote show origin .
git ls-remote --heads https://github.com/rails/rails.git
5b3f7563ae1b4a7160fda7fe34240d40c5777dcd refs/heads/1-2-stable
81d828a14c82b882e31612431a56f830bdc1076f refs/heads/2-0-stable
b5d759fd2848146f7ee7a4c1b1a4be39e2f1a2bc refs/heads/2-1-stable
c6cb5a5ab00ac9e857e5b2757d2bce6a5ad14b32 refs/heads/2-2-stable
e0774e47302a907319ed974ccf59b8b54d32bbde refs/heads/2-3-stable
13ad87971cc16ebc5c286b484821e2cb0fc3e3b1 refs/heads/3-0-stable
3df6c73f9edb3a99f0d51d827ef13a439f31743a refs/heads/3-1-stable
f4db3d72ea564c77d5a689b850751ce510500585 refs/heads/compressor
c5a809e29e9213102351def7e791c3a8a67d7371 refs/heads/deps_refactor
821e15e5f2d9ef2aa43918a16cbd00f40c221e95 refs/heads/encoding
8f57bf207ff4f28fa8da4544ebc573007b65439d refs/heads/master
c796d695909c8632b4074b7af69a1ef46c68289a refs/heads/sass-cleanup
afd7140b66e7cb32e1be58d9e44489e6bcbde0dc refs/heads/serializers
You can also use this:
git show-branch remotes/origin/<<remote-branch-name>>
returns latest commit and value of $? is 0 otherwise returns "fatal: bad sha1 reference remotes/origin/<>" and value of $? is 128
Will return all branches (remote or local) that contain the query in the name.
git branch --all | grep <query>
I'm combining some of the answers above in a script:
BRANCHES=(develop master 7.0 7.0-master)
ORIGIN=bitbucket
REMOTE=github
for BRANCH in "${BRANCHES[@]}"; do
BRANCH=$(git ls-remote --heads "${ORIGIN}" "${BRANCH}" \
| cut --delimiter=$'\t' --fields=2 \
| sed 's,refs/heads/,,' \
| grep --line-regexp "${BRANCH}")
if [ -n "${BRANCH}" ]
then
git branch --force "${BRANCH}" "${ORIGIN}"/"${BRANCH}"
git checkout "${BRANCH}"
git push "${REMOTE}" "${BRANCH}"
fi
done
git push github --tags
This script will get 4 branches from a remote bitbucket, and push them to a remote github, and will then push all tags to github.
I'm using this in a Jenkins job, that's why you don't see any git fetch
or git pull
, that is already done in the Jenkins job repository config.
I usually prefer long-form options in scripts. I could have combined git branch
and git checkout
by using git checkout -B
.
You can try
git diff --quiet @{u} @{0}
Here @{u}
refers to remote/upstream, and @{0}
refers to current local HEAD (with newer version of git, @{0}
can be shortened as @
). If remote does not exist, it errors out.
With git 2.16.2 (I am not sure which version is the first to have this functionality, for example, git 1.7.1 doesn't have it), you can do
git checkout
If a remote branch exists, there will be some output, like
Your branch is up to date with 'origin/master'
Otherwise there is no output.