Home > database >  How to compare the difference between a new branch and the master branch in git?
How to compare the difference between a new branch and the master branch in git?

Time:03-15

I have a new branch named test_branch containing more than one commit and I want to merge it to the master branch. Before merging I need to get the difference between test_branch and master branch with GitLab CI.

I tried to use the command below but it failed:

# the variable $CI_COMMIT_MESSAGE is "Merge branch 'test_branch' into 'master'" so I can get the new branch name
git diff $(echo $CI_COMMIT_MESSAGE | awk -F "'" '{print $2}') master --name-status

and shows:

fatal: ambiguous argument 'test_branch': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Why is it failed and what is the correct way doing this?

CodePudding user response:

I ran into the same situation where commands such as git diff origin or git diff origin master produced the error reported in the question, namely Fatal: ambiguous argument...

To resolve the situation, I ran the command

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master

to set refs/remotes/origin/HEAD to point to the origin/master branch.

Before running this command, the output of git branch -a was:

* master
  remotes/origin/master

After running the command, the error no longer happened and the output of git branch -a was:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

(Other answers have already identified that the source of the error is HEAD not being set for origin. But I thought it helpful to provide a command which may be used to fix the error in question, although it may be obvious to some users.)

Additional information:

For anybody inclined to experiment and go back and forth between setting and unsetting refs/remotes/origin/HEAD, here are some examples.

To unset:

git remote set-head origin --delete

To set: (additional ways, besides the way shown at the start of this answer) git remote set-head origin master to set origin/head explicitly OR

git remote set-head origin --auto

to query the remote and automatically set origin/HEAD to the remote's current branch.

CodePudding user response:

You can :

  • use HEAD, which is a pointer to the active commit,
  • use other CI env variables, such as CI_COMMIT_BRANCH or CI_COMMIT_REF_NAME, which will hold the name of the branch that triggered the pipeline -- without needing to awk your way out of it.

Check if any of the following commands :

git diff --name-status origin/master HEAD

git diff --name-status origin/master $CI_COMMIT_BRANCH
git diff --name-status origin/master $CI_COMMIT_REF_NAME

displays the diff you are looking for.

  • Related