Check if pull needed in Git

后端 未结 24 1293
忘了有多久
忘了有多久 2020-11-22 13:34

How do I check whether the remote repository has changed and I need to pull?

Now I use this simple script:

git pull --dry-run | grep -q -v \'Already          


        
24条回答
  •  清酒与你
    2020-11-22 14:11

    If you have an upstream branch

    git fetch 
    git status
    

    If you don't have an upstream branch

    Compare the two branches:

    git fetch 
    git log .. --oneline
    

    For example:

    git fetch origin
    
    # See if there are any incoming changes
    git log HEAD..origin/master --oneline
    

    (I'm assuming origin/master is your remote tracking branch)

    If any commits are listed in the output above, then you have incoming changes -- you need to merge. If no commits are listed by git log then there is nothing to merge.

    Note that this will work even if you are on a feature branch -- that does not have a tracking remote, since if explicitly refers to origin/master instead of implicitly using the upstream branch remembered by Git.

提交回复
热议问题