IT박스

로컬 git 브랜치를 원격 브랜치와 비교하는 방법은 무엇입니까?

itboxs 2020. 9. 28. 08:32
반응형

로컬 git 브랜치를 원격 브랜치와 비교하는 방법은 무엇입니까?


diff로컬 브랜치와 원격 브랜치 사이를 어떻게 볼 수 있습니까?


원격 추적 분기를 업데이트하려면 git fetch먼저 입력 한 다음 다음 을 입력해야합니다 .

git diff <masterbranch_path> <remotebranch_path>

당신이 할 수있는 git branch -a다음 목록에서 지점 이름을 선택 (로컬 및 원격) 모든 지점을 나열 (단지 제거 remotes/원격 지사 이름에서.

예 : git diff master origin/master(여기서 "master"는 로컬 마스터 분기이고 "origin / master"는 원격 즉, 원본 및 마스터 분기입니다.)


git diff <local branch> <remote>/<remote branch>

예 : git diff master origin/master또는git diff featureA origin/next

물론 할 수 있습니다 말했다 지점 원격 추적 당신이해야 할 git fetch첫 번째; 원격 저장소의 브랜치에 대한 최신 정보가 필요합니다.


첫 번째 유형

git branch -a

사용 가능한 분기 목록을 가져옵니다. 출력에서 다음과 같은 것을 볼 수 있습니다.

* master
  remotes/main/master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/mt
  remotes/upstream/master
  remotes/upstream/mt

그런 다음 차이를 보여

git diff --stat --color remotes/main/master..origin/master
git diff remotes/main/master..origin/master

특정 지점에 있고 추적중인 업스트림 지점과 비교하려면 다음을 사용하십시오.

git diff @{upstream}

이 답변의 의례로 개정판지정 하는 git 문서 에는 다음이 있습니다.

<branchname>@{upstream}예를 들어 master@{upstream}, 브랜치 이름 @{u}
의 접미사 @{upstream}(축약 형 <branchname>@{u})는에 의해 지정된 브랜치 branchname가 위에 빌드되도록 설정된 브랜치를 참조합니다 ( branch.<name>.remote및로 구성됨 branch.<name>.merge). 누락 된 branchname기본값은 현재 값입니다.


나는 다음의 결과를 훨씬 더 잘 이해합니다.

git diff <remote-tracking branch> <local branch>

그것은 무엇을 떨어 뜨릴 것인지, 그리고 로컬 브랜치를 푸시하면 무엇이 추가 될 것인지를 보여줍니다. 물론 그것은 똑같습니다. 단지 그 반대입니다.하지만 저에게는 더 읽기 쉬우 며 앞으로 일어날 일을 보는 것이 더 편합니다.


쉬운 방법 :

git fetch
git log -p HEAD..FETCH_HEAD

이것은 먼저 기본 원격 (원본)에서 변경 사항을 가져옵니다. 리포지토리를 복제하면 자동으로 생성됩니다. 명시적일 수도 있습니다 git fetch origin master..

그런 다음 git log를 사용하여 현재 브랜치를 방금 가져온 브랜치와 비교합니다. ( -p(패치 생성) 옵션은 차이점을 보여줍니다 .)


이것이 내가하는 방법이다.

#To update your local.
git fetch --all

이것은 리모트에서 모든 것을 가져 오므로 차이를 확인할 때 리모트 브랜치와 차이를 비교합니다.

#to list all branches
git branch -a

위의 명령은 모든 분기를 표시합니다.

#to go to the branch you want to check difference
git checkout <branch_name>
#to check on which branch you are in, use
git branch
    (or)
git status

이제 다음과 같이 차이점을 확인할 수 있습니다.

git diff origin/<branch_name>

이것은 로컬 브랜치를 원격 브랜치와 비교할 것입니다.


경우, 구문은 다음과 같이해야한다고, 당신의 작업 지점이 개발이다하자 지역 개발 분기 및 원격 개발 지점을 구별 할 git diff remotes/origin/development..development
또는

git fetch origin git diff origin/development


tl; dr :git diff <local branch> <remote branch>

쉘에서 git을 사용할 때 먼저 주변을 둘러 보면서 방향을 잡는 것을 좋아합니다. 모든 분기표시 하는 명령은 다음과 같습니다.

$ git branch -a  # (or git branch --all) 
* my-branch
  master
  remotes/origin/some-branch
  remotes/origin/HEAD -> origin/master
  remotes/origin/my-branch
  remotes/origin/some-other-branch
  remotes/origin/master

여기서 I는 두 로컬 브랜치 (가지고 my-branchmaster원격) 및도 4 ( some-branch, some-other-branch, master, 및 my-branch).

또한 옆에있는 별표 my-branch는 내가 현재 해당 분기에 있다는 사실을 나타냅니다 ( git status출력 되는 명령 사용하여 알 수 있음 On branch my-branch.).

참고 : git bash 셸의 원격 분기는 빨간색으로 표시되고 로컬 분기는 녹색으로 표시됩니다.

원격 분기표시 하려면 다음을 수행하십시오.

$ git branch -r # (or git branch --remotes)
  origin/some-branch
  origin/HEAD -> origin/master
  origin/my-branch
  origin/some-other-branch
  origin/master

로컬 브랜치 만 표시하려면 사용하고 싶겠지 git branch -l만 완전히 다른 명령입니다. 옵션없이 로컬 지점 사용 표시 하려면git branch

$ git branch
* my-branch 
  master

기본 브랜치 옵션에 대한 검토를 완료하려면 필터링--list 을 허용 하는 것과 반대되는 부분이 있습니다 . 다음과 같은 패턴으로 사용하십시오.

$ git branch --list 'my*'
* my-branch

또한 --list옵션 -a결합 할 수 -r있지만 그에 따라 패턴을 조정해야합니다 ( 기억하십시오 : 원격 분기는 "remotes"로 시작 함 ). 예:

# this will show all branches (local & remote) that start with my
$ git branch --list 'my*' -a
* my-branch

# better: the pattern includes the remote
$ git branch --list '*my*' -a
* my-branch
  remotes/origin/my-branch

문서 : https://git-scm.com/docs/git-branch

이제 사용 가능한 모든 분기 에서 두 분기비교할 수 있습니다 (두 개의 로컬 또는 두 개의 원격을 비교할 수도 있습니다).

여기에서 로컬과 원격을 비교하고 있는데 my-branch동기화되어 출력이 발생하지 않습니다.

$ git diff my-branch remotes/origin/my-branch

참고 : 따옴표없이 분기의 전체 이름을 제공해야합니다.

또한 로컬 my-branch과 원격을 비교할 수 있습니다 master. 리모컨 my-branch이 마스터 브랜치에 병합되지 않았기 때문에 여기에 출력 이 표시됩니다.

$ git diff my-branch remotes/origin/master
diff --git a/src/controllers/call.controller.js b/src/controllers/call.controller.js
index fd79b98..df3d798 100644
--- a/src/controllers/call.controller.js
+++ b/src/controllers/call.controller.js
@@ -261,7 +261,7 @@ function callController() {
   /*
    *  Function: doCall
[ . . . ]

현재 브랜치와 원하는 것을 비교하는 경우 여기에 속기 대답이 있습니다 git pull.

git fetch
git diff FETCH_HEAD

첫 번째 명령은 현재 분기에 해당하는 원격 분기를 파악합니다. FETCH_HEAD참조 에서 해당 계산의 아티팩트입니다 . 그런 다음 두 번째 명령은 해당 참조 비교와 현재 분기의 비교를 사용합니다.


파일 이름 만 변경된 경우 차이점을 확인하려면 다음을 사용하십시오 :

git diff --name-status <remote-branch> <local-branch>,

그렇지 않으면 두 분기 간의 모든 차이점이 표시됩니다.

git diff <remote-branch> <local-branch>


이 질문에 대한 몇 가지 답변이 이미 있다는 것을 알고 있지만 대부분을 시도 할 때 이상한 오류가 발생했습니다.

내 경우에는 내가이 두 번째 원격 호출 heroku입니다 하지origin 과 내가를 실행하려고이 오류가 발생했습니다 동기화되지 않았기 때문에 git diff master heroku/master:

fatal: ambiguous argument 'heroku/master': unknown revision or path not in the working tree.

또는 이것은 다른 접근 방식을 시도 할 때 git diff master..heroku/master:

fatal: bad revision 'master..heroku/master'

The solution was explicitly mentioning the remote name on git fetch before running git diff, in my case:

$ git fetch heroku
$ git diff master heroku/master

Hope that helps others with this same issue.


git difftool <commit> .

This will compare the commit you want with your local files. Don't forget the dot in the end (for local).

For example, to compare your local files with some commit:

git difftool 1db1ef2490733c1877ad0fb5e8536d2935566341 .

(and you don't need git fetch, unless comparing to new commits is needed)


Example

git diff 'master' 'testlocalBranch'

If you are using editor like webstorm, you can right click on file select compare with branch and type/select your branch.

enter image description here

enter image description here


I wonder about is there any change in my master branch...

  1. Firstly, you need to change your branch (If you are already under this branch, you do not need to do this!)

git checkout master

  1. You can see which file has been modified under your master branch by this command

git status

  1. List the branches

git branch -a

  • master
    remotes/origin/master
  1. Find the differences

git diff origin/master


Setup

git config alias.udiff 'diff @{u}'

Diffing HEAD with HEAD@{upstream}

git fetch  # Do this if you want to compare with the network state of upstream; if the current local state is enough, you can skip this
git udiff

Diffing with an Arbitrary Remote Branch

This answers the question in your heading ("its remote"); if you want to diff against "a remote" (that isn't configured as the upstream for the branch), you need to target it directly. You can see all remote branches with the following:

git branch -r

You can see all configured remotes with the following:

git remote show

You can see the branch/tracking configuration for a single remote (e.g. origin) as follows:

git remote show origin

Once you determine the appropriate origin branch, just do a normal diff :)

git diff [MY_LOCAL] MY_REMOTE_BRANCH


This is quite simple. You can use: git diff remote/my_topic_branch my_topic_branch

Where my_topic_branch is your topic branch.


If you use TortoiseGit (it provides GUI for Git), you can right click your Git repo folder then click Git Sync.

You can select your branches to compare if not selected. Than you can view differences commit. You can also right click any commit then Compare with previous revision to view differences side by side.tortoise git sync to compare remote and local branch


Let's say you have already set up your origin as the remote repository. Then,

git diff <local branch> <origin>/<remote branch name>

참고URL : https://stackoverflow.com/questions/1800783/how-to-compare-a-local-git-branch-with-its-remote-branch

반응형