原文地址:http://nbviewer.ipython.org/gist/choldgraf/6359961
What to do when git won't pull
Monday
Say you've got a repository on your local computer that you sync to a remote repository that your whole lab shares.
You do some work on Monday, and push it to the repository.
Here's your (correct) workflow for Monday.
First, you sync with the remote repository
%%bash
cd myRepo/
ls
git pull
Then, you create a file and add it to be committed.
%%bash
cd myRepo/
touch nowimworking.txt
echo "Look at me. Work work work" > nowimworking.txt
git add -A
git status
Now, you commit and push this to the repository. This is the right way of doing things! Good job on Monday!
%%bash
cd myRepo/
git commit -m "I've committed all of this work!"
git push
While you were sleeping, your co-worker is busy at work!
Tuesday morning
Then, you arrive on Tuesday morning, and realize that you didn't name the file correctly...
so you change it, and you also change the text
%%bash
cd myRepo/
echo "Whoops, I forgot to change something important" > nowimworking.txt
cat nowimworking.txt
then you realize "whoops, I forgot to synchronize my code to the repository".
This might be important because another lab member was changing some code the night before.
You stop writing code, move to the command line, and quickly try a git pull.
%%bash
cd myRepo/
git pull
Whoops! You just broke git!
So, what just happened?
Ultimately, git wants to make sure that nobody unintentionally overwrites their own work or the work of others.
In this case, person A made changes to their local repository, but they didn't sync person B's changes first.
So, if person A had sync'ed, it would have overwritten their own changes.
Git assumes you don't want this to happen, so it returns an error rather than overwriting stuff for you.
How do get around this?
It's important to know that whenever you "git pull", git is really doing two things:
A "git fetch", which retrieves the latest copy of the remote repository and stores it somewhere to be used later
A "git merge", which tries to merge that fetched repository with the branch you're currently on
In this case, the "merge" noted that there were conflicts between the remote repository and your own
Luckily, fixing this problem is as easy as performing the abovementioned commands independently of one another
So here's how you fix this:
First we fetch the latest copy of the remote repository. Note that this copy will be called "origin/master", corresponding to the "master" branch of the remote repository referenced by "origin"
%%bash
cd myRepo/
git fetch origin
Now we need to commit our changes so that git can properly compare them
%%bash
git add -A
git commit -m "I'm committing my changes so that I can merge with the remote repository"
Now, try to merge...a conflict will come up, so we will use a mergetool
%%bash
git merge origin/master
git mergetool
Now, we have the merge completed, we need only commit the merge and push to the repository
%%bash
git commit -m "Successfully merged"
git push
Note - this often means you'll need to use something like a mergetool. I'm going to assume you know how to use one here.
Now, your local repository is synced with the remote repo, and we can continue our work.
The End
To delete the git repository and re-initialize a new one!
%%bash
rm -r myRepo/
rm -r ../person2/myRepo2/
Create my repo
%%bash
git clone https://github.com/choldgraf/git_wont_pull myRepo
cd myRepo
git status
Create the second (your friend's) repo
%%bash
git clone http://github.com/choldgraf/git_wont_pull ../person2/myRepo2
cd ../person2/myRepo2
git status
This totally resets the git repository back to its original commit
%%bash
cd myRepo/
git reset --hard 3d93d42d5230ac50677d847350c3c455fd792c94
git push -f
What to do when git won't pull
Monday
Say you've got a repository on your local computer that you sync to a remote repository that your whole lab shares.
You do some work on Monday, and push it to the repository.
Here's your (correct) workflow for Monday.
First, you sync with the remote repository
%%bash
cd myRepo/
ls
git pull
Then, you create a file and add it to be committed.
%%bash
cd myRepo/
touch nowimworking.txt
echo "Look at me. Work work work" > nowimworking.txt
git add -A
git status
Now, you commit and push this to the repository. This is the right way of doing things! Good job on Monday!
%%bash
cd myRepo/
git commit -m "I've committed all of this work!"
git push
While you were sleeping, your co-worker is busy at work!
Tuesday morning
Then, you arrive on Tuesday morning, and realize that you didn't name the file correctly...
so you change it, and you also change the text
%%bash
cd myRepo/
echo "Whoops, I forgot to change something important" > nowimworking.txt
cat nowimworking.txt
then you realize "whoops, I forgot to synchronize my code to the repository".
This might be important because another lab member was changing some code the night before.
You stop writing code, move to the command line, and quickly try a git pull.
%%bash
cd myRepo/
git pull
Whoops! You just broke git!
So, what just happened?
Ultimately, git wants to make sure that nobody unintentionally overwrites their own work or the work of others.
In this case, person A made changes to their local repository, but they didn't sync person B's changes first.
So, if person A had sync'ed, it would have overwritten their own changes.
Git assumes you don't want this to happen, so it returns an error rather than overwriting stuff for you.
How do get around this?
It's important to know that whenever you "git pull", git is really doing two things:
A "git fetch", which retrieves the latest copy of the remote repository and stores it somewhere to be used later
A "git merge", which tries to merge that fetched repository with the branch you're currently on
In this case, the "merge" noted that there were conflicts between the remote repository and your own
Luckily, fixing this problem is as easy as performing the abovementioned commands independently of one another
So here's how you fix this:
First we fetch the latest copy of the remote repository. Note that this copy will be called "origin/master", corresponding to the "master" branch of the remote repository referenced by "origin"
%%bash
cd myRepo/
git fetch origin
Now we need to commit our changes so that git can properly compare them
%%bash
git add -A
git commit -m "I'm committing my changes so that I can merge with the remote repository"
Now, try to merge...a conflict will come up, so we will use a mergetool
%%bash
git merge origin/master
git mergetool
Now, we have the merge completed, we need only commit the merge and push to the repository
%%bash
git commit -m "Successfully merged"
git push
Note - this often means you'll need to use something like a mergetool. I'm going to assume you know how to use one here.
Now, your local repository is synced with the remote repo, and we can continue our work.
The End
To delete the git repository and re-initialize a new one!
%%bash
rm -r myRepo/
rm -r ../person2/myRepo2/
Create my repo
%%bash
git clone https://github.com/choldgraf/git_wont_pull myRepo
cd myRepo
git status
Create the second (your friend's) repo
%%bash
git clone http://github.com/choldgraf/git_wont_pull ../person2/myRepo2
cd ../person2/myRepo2
git status
This totally resets the git repository back to its original commit
%%bash
cd myRepo/
git reset --hard 3d93d42d5230ac50677d847350c3c455fd792c94
git push -f