Git: Compare All Local Commits to Remote Repo Version

后端 未结 6 766
礼貌的吻别
礼貌的吻别 2020-12-14 06:40

I\'m somewhat new to Git and what I\'m trying to do seems like it should be possible. Basically I\'ve been working off of clone of a repo and have made quite a few local com

相关标签:
6条回答
  • 2020-12-14 06:43

    the difference can be viewed with git diff A B, it will compare the code in A to B:

    git diff origin/master master
    

    origin/master is the state of the remote master branch when you last fetched (or cloned) from it, master is the local state of the code – unless you switched branches when working locally.

    0 讨论(0)
  • 2020-12-14 06:45

    The most ready answer is

     git show-branch
    

    What you can do for more control is is use git log annex git rev-list:

     git log --left-right --graph --cherry-pick \
          --oneline branchname...remote/branchname
    

    This is my preferred method and will result in something like

    > | fff6bda remote fix
    < | c8903ee local fix
    < |   724373c Merge branch 'bla' into bla
    |\ \  
    | < | 2faf547 details
    | < | abbdc47 ....
    |/ /  
    < | befc181 some tagged commit
    

    Include --decorate and you'll get something close to gitk, git-gui and gitweb:

    > | fff6bda remote fix
    < | c8903ee local fix
    < |   724373c (tag_4) Merge branch 'bla' into bla
    |\ \  
    | < | 2faf547 details
    | < | abbdc47 ....
    |/ /  
    < | befc181 (tag_3) some tagged commit
    

    PRO TIP 1: Use 'git config alias.lr log --long-option1 --long-option2' for convenient use

    PRO TIP 2: Use 'git config color.ui auto' for immediate eye-relief

    If you wanted all local heads (on all local branches) versus all remote commits (on ditto branches):

    git log --decorate --pretty=oneline --all --not --glob=refs/remotes --no-walk
    

    Leave off the no-walk to get all individual revisions. In this case I prefer to use the switches shown earlier (--graph --left-right)

    Merges

    If you want to see merges clearly, include --boundary

    Various advanced queries:

    Filtering the results

    Git log and rev-list support a whole slew of cunning filtering ability, see the man page

    --after '2001-01-01'
    --until 'last week'
    --author 'martin'
    -E -i  --grep='fixes #[0123456789]+'
    -S 'new_debug_function'
    

    and many, many others. This should give you plenty of leverage to get exactly at the info you want with almost zero effort

    What's stashed locally?

    What resides in stashes, but not on remotes (note there is no way to refer to stashes on remote braches because stashes reside in reflogs, and the reflogs (even for remote branches) always reflect local history[1]):

    git log $(git rev-list -g stash) --not --glob=refs/remotes 
    

    All (other) unreachable commits...

    Notes

    • on my workflow these constitue rebased/amended commits and dropped stashes only
    • also generating these will take some time depending on the size of your history graph
    • this will include any dropped stashes, but not the current stashes

      git log $(git fsck --unreachable --full --lost-found | grep ' commit ' | cut -d' ' -f3) \ --no-walk --not --glob=refs/remotes --oneline --decorate

    Scripting

    For scripting purposes, replace the use of git log with git rev-list and you'll get just the hashes (and some more script-prrof robustness)

    [1] See also my prior answer(s) on how to transfer stashes between repos:

    • Is it possible to push a git stash to a remote repository?
    • Can I fetch a stash from a remote repo into a local branch?
    0 讨论(0)
  • 2020-12-14 06:55

    There are three ways ( two others from other answers given here )

    1) git diff origin/master master
    2) git diff origin/master..master
    3) git diff origin/master...master
    

    First one and second one are same and show changes between the tips of the master and remote master.

    Third one shows changes that occurred on the master since branch last push and I think this is the most appropriate one you are looking for

    0 讨论(0)
  • 2020-12-14 06:59
    git diff origin/master..master
    
    0 讨论(0)
  • 2020-12-14 07:07

    The simplest and certainly easiest to remember command that (usually) does what you want is this:

    git diff origin
    

    This shows the diff between what you originally pulled (the origin) and the current branch you're working on, which defaults to master.

    0 讨论(0)
  • 2020-12-14 07:07

    for everything git diff HEAD origin/"nameofyourbranch"

    for specific file git diff HEAD:"filename" origin/"nameofbranch":"filename"

    0 讨论(0)
提交回复
热议问题