Marslo
Marslo

Reputation: 3261

How can I get changes in remote server conveniently

I know how to check the changes in local:
Get changes before commit:

git diff master

Get changes after commit && before push:

git diff origin/master
or
git diff remotes/origin/master 

What I want is:

  1. git clone the latest version from remote server, for example, the local version is v1
  2. Change the remote server from the aother side, for example, the remote version is v2, (local version is still v1)
  3. get the different from remote server

And, I know how to get changes in remote server by the following steps:

git remote foo [email protected]:user/foo.git
git fetch foo
git diff master foo/master

However, I want to alias one command to check the changes in remote server, for example:

git rdiff master
or something else

is that possible to get changes in remote server by one comment? Thanks.


By the way, I found that, there are an origin has been fetched in local repo, how can I use it without fetch it again?

$ git remote -v
origin  [email protected]:Marslo/VimConfig.git (fetch)
origin  [email protected]:Marslo/VimConfig.git (push)

Upvotes: 1

Views: 141

Answers (2)

Marslo
Marslo

Reputation: 3261

Thanks for @Shahbaz.

rdiff used to get the different between local repo and remote server, the alias is:

rdiff = "!bash -c 'git fetch && git diff master remotes/origin/master'"

It will looks like
rdiff

And, I made "rlog" and "rlogs" as well, it to get the log from remote server:

plog = log --max-count=3 --color --graph\n --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C( blue)<%an>%Creset'\n --abbrev-commit --date=relative
plogs = log --color --graph\n --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(blue)<%an>%Cr eset'\n --abbrev-commit --date=relative
rlog = "!bash -c 'git fetch && git plog remotes/origin/master'"
rlogs = "!bash -c 'git fetch && git plogs remotes/origin/master'"

It will show:
rlogs

Upvotes: 0

Shahbaz
Shahbaz

Reputation: 47563

First of all, there is a logical problem with this:

git rdiff master

Which remote are you going to compare against? So in the very list, it should be something like:

git rdiff remote_name branch_name

which should expand to:

git fetch remote_name && git diff remote_name/branch_name

You can create exactly that alias (in ~/.gitconfig for example):

[alias]
    rdiff = "!sh -c 'git fetch \"$0\" && git diff \"$0\"/\"$1\"'"

which tells shell to run the above command with the given parameters.


Some other notes:

  • Instead of git diff master, you can simply write git diff which takes the diff with the current branch you are on which is not necessarily master.
  • Instead of git diff master foo/master, you can simply write (if there are no local changes) git diff foo/master which does the same. It's easier to type since you don't repeat master twice.
  • You don't need to change your remotes every time. You can have multiple remotes in the same repository. Therefore, you can do (note that you missed add):

    git remote add foo [email protected]:user/foo.git
    

    and keep it there in your repository forever. Now you can still interact with origin, which is the remote automatically created when you cloned your repository. At the same time, you can also interact with the other remote you created, named foo. So in the same repository, you could do:

    # get updates from origin
    git fetch origin
    git merge origin/master
    
    # check them against foo
    git rdiff foo master
    

Upvotes: 1

Related Questions