2

Let's say I have two local repositorys, repo1 and repo2 both containing file1.txt with lines:

line1
line2
line3
line4

and that both are up to the latest branch. Repo1 does a a modification:

line3
line4 

commits and pushes and repo2 does a pull without comitting (It didn't made any changes). When repo2 does the pull, shouldn't there be any comparison between the newly fetched file and the file in the local storage? I am using Sourcetree as a git GUI. This is what I observed from using it. Can anyone please clarify this for me please?

5
  • 1
    Please clarify your question. What exactly are you asking? Commented Feb 1, 2014 at 1:17
  • Well, when I do a pull on files that are not the same like I mentioned in the example(file 1 from repo1 was altered and pushed and file1 from repo2 is the same as in the previous branch). When I pull from repo2, shouldn't there be a comparison and a merge between the two file after I pull? The file, in my case simply just get's overwritten. I hope you understand what I'm referring to Commented Feb 1, 2014 at 1:20
  • @patentul merging happens when there's two sets of changes, but there's only one set of changes here. Commented Feb 1, 2014 at 1:42
  • so how can I be sure that my files won't get altered or deleted by accident before when I do a pull? Commented Feb 1, 2014 at 1:48
  • 1
    It wouldn't be by "accident." That file was very intentionally modified in Repo1. If you don't trust that repo, don't pull from it without doing a diff first. Commented Feb 1, 2014 at 4:48

2 Answers 2

2

When repo2 does the pull, shouldn't there be any comparison between the newly fetched file and the file in the local storage?

A pull happens in two phases: The first phase is just fetching the changes from the remote repository. This is a completely non-destructive action. If anything, it will add unique objects to your repository, expanding the object database. It will also update the remote branches which are essentially branches of the remote you are fetching from.

The second phase is a merge. Git will first check what changes have been made locally. As you didn’t commit anything else, there are no diverging changes and no merge is required. As such, Git can just update your local branch’s history to match the remote’s. At this stage, Git will also attempt to check out the changes so that your working directory will match the just pulled state. In case of a clean working directory, that means that there are no local changes of tracked files, there is no problem: All changes that might have been done before have been committed and won’t get lost. So Git will just update the local branch and check the new files out. In case there are local changes, your working directory is empty. Git will then check which files actually would need to be updated. If it can do that easily, it will do it and keep your changes the way they are. In doubt, it will raise an error that your working directory is not clean and abort everything.

So regardless of what’s the case, you will never lose anything.

Sign up to request clarification or add additional context in comments.

6 Comments

So in my example, after I do the pull from repo2, which content would I have in file1.txt? is it line3 and lin4? should't there be a merge option so that I can choose which version to choose?
The merge commit is skipped by default if possible with git. This is called fast forwarding.
So that means I can accidentally delete my files if I'm not carefull and somebody did some new modifications to an older file I worked on, am I right?
You can always preview changes by looking at the log of your remote. And you can also revert your local repository to a previous commit before you did your pull if you don't like what happened.
The big thing is that there is always a perfect history of every change to every file that git ever knew about in your repository. So you are at all times safe to try something and then change your mind.
|
0

Unless your configure your repository differently or tell git to explicitly create a merge commit when you issue the pull or merge commands, git usually "fast forwards" certain types of merges that can be applied cleanly.

This prevents you from gunking up your history with "useless" merge commits.


Edit:

If you would like to always have a merge commit, you can specify --no-ff when you do a git pull. There is probably also an option to do this in your GUI preferences window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.