How to detect differences between local repo and remote repo in git?

Let's say I made a git clone from the url for the repository. I made some changes to the file, made a git commit.

When I do a git pull, I see that it says "Already updated"

Doesn't he show something that says I'm not in the know?

My question is:

  • Let's say I made this change above my local repo, but I did not commit it for 2 days, but before 2 days ended, someone changed the remote repo. What steps should I take to ensure that I do not redefine the changes in the remote repo, or at least cannot make the last changes before committing?

  • Is there any way of the difference between my local repo and the remote repo to check what differences exist? (in case I just want to remember what I had before?)

+8
git
source share
2 answers

My first tip is not git pull . Do a git fetch and then git merge .

To answer the null question: in fact, you are updated. You have all the commits that the remote repository has. Thus, there is nothing left to extract or combine 1 .

To answer your first question:

  • git commit : commit your changes to your branch, completely unrelated to what is happening in the remote repositories.
  • git fetch origin : get the contents of the remote repository ( origin ), but keep them under the origin/branch . At the moment, your own code is not changed.
  • git merge origin/master : merge origin/master , which is the master branch of the remote origin repository (which you have selected right now) with your current branch.
  • git push origin : push commit and merge to remote repository

To answer the second question:

  • git fetch origin : update origin/branch .
  • git diff origin/master : get the difference between the current branch and the origin/master branch.

1 Suppose that this is what initially appears in your repository, on the master branch:

 A -> B -> C -> D -> E | |\- master | \- origin/master 

This is right after you cloned the repository. Now you say that you made a new commit on your local master branch:

 A -> B -> C -> D -> E -> F | | | \- master | \- origin/master 

So there are two things.

  • Assuming that someone else will not be deleted in the remote origin , there is nothing new to retrieve. So git fetch origin master tells you that there is nothing new.

  • If you do git merge origin/master , there is nothing to merge again. origin/master is the prefix of master . In other words, master already contains all the commits that origin/master has, so there is nothing new to merge.

If you used fetch and merge instead of pull , you could easily figure out which part of the double pull command is the one that leads to unexpected (in your opinion) behavior.

Of course, after git push origin master you will get:

 A -> B -> C -> D -> E -> F | |\- master | \- origin/master 
+11
source share

When you pull, it pulls the story from the server and automatically tries to merge everything that you changed with things that other people changed. If he can do this automatically, then it will be successful, and you will not see anything, and if this does not succeed, you will be asked to fix any conflicts. You can never silently undo changes made by other people.

If you want to see that someone has changed something, you can run this git fetch and then git status . The status will print out which files have changes locally, but it will also say something like "Your branch is 2 commits ahead of the start / master," that is, you have 2 commits that have not been transferred to the server. If someone else clicked on the server, he will say something like “Your local branch, and the source / master has diverged. This means that you and someone else have mastered the master, and when you pull it, try to combine them as described above.

+4
source share

All Articles