What to do when git won't pull

原文地址: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

In [34]:
%%bash
cd myRepo/
ls
git pull
README.md
Already up-to-date.

Then, you create a file and add it to be committed.

In [35]:
%%bash
cd myRepo/
touch nowimworking.txt
echo "Look at me.  Work work work" > nowimworking.txt

git add -A
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   nowimworking.txt
#

Now, you commit and push this to the repository. This is the right way of doing things! Good job on Monday!

In [36]:
%%bash
cd myRepo/
git commit -m "I've committed all of this work!"
git push
[master 6862b4b] I've committed all of this work!
 1 file changed, 1 insertion(+)
 create mode 100644 nowimworking.txt
To https://github.com/choldgraf/git_wont_pull
   3d93d42..6862b4b  master -> master

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

In [37]:
%%bash
cd myRepo/
echo "Whoops, I forgot to change something important" > nowimworking.txt
cat nowimworking.txt
Whoops, I forgot to change something important

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.

In [38]:
%%bash
cd myRepo/
git pull
Updating 6862b4b..3984193
From https://github.com/choldgraf/git_wont_pull
   6862b4b..3984193  master     -> origin/master
error: Your local changes to the following files would be overwritten by merge:
	nowimworking.txt
Please, commit your changes or stash them before you can merge.
Aborting

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"

In [39]:
%%bash
cd myRepo/
git fetch origin

Now we need to commit our changes so that git can properly compare them

In [27]:
%%bash
git add -A
git commit -m "I'm committing my changes so that I can merge with the remote repository"
usage: git diff [--no-index] <path> <path>

Now, try to merge...a conflict will come up, so we will use a mergetool

In []:
%%bash
git merge origin/master
git mergetool

Now, we have the merge completed, we need only commit the merge and push to the repository

In []:
%%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!

In [31]:
%%bash
rm -r myRepo/
rm -r ../person2/myRepo2/
Create my repo
In [32]:
%%bash
git clone https://github.com/choldgraf/git_wont_pull myRepo
cd myRepo
git status
Cloning into 'myRepo'...
# On branch master
nothing to commit (working directory clean)
Create the second (your friend's) repo
In [33]:
%%bash
git clone http://github.com/choldgraf/git_wont_pull ../person2/myRepo2
cd ../person2/myRepo2
git status
Cloning into '../person2/myRepo2'...
# On branch master
nothing to commit (working directory clean)

This totally resets the git repository back to its original commit

In [28]:
%%bash
cd myRepo/
git reset --hard 3d93d42d5230ac50677d847350c3c455fd792c94
git push -f
HEAD is now at 3d93d42 first commit
To https://github.com/choldgraf/git_wont_pull
 + c42e00a...3d93d42 master -> master (forced update)

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

In [34]:
%%bash
cd myRepo/
ls
git pull
README.md
Already up-to-date.

Then, you create a file and add it to be committed.

In [35]:
%%bash
cd myRepo/
touch nowimworking.txt
echo "Look at me.  Work work work" > nowimworking.txt

git add -A
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   nowimworking.txt
#

Now, you commit and push this to the repository. This is the right way of doing things! Good job on Monday!

In [36]:
%%bash
cd myRepo/
git commit -m "I've committed all of this work!"
git push
[master 6862b4b] I've committed all of this work!
 1 file changed, 1 insertion(+)
 create mode 100644 nowimworking.txt
To https://github.com/choldgraf/git_wont_pull
   3d93d42..6862b4b  master -> master

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

In [37]:
%%bash
cd myRepo/
echo "Whoops, I forgot to change something important" > nowimworking.txt
cat nowimworking.txt
Whoops, I forgot to change something important

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.

In [38]:
%%bash
cd myRepo/
git pull
Updating 6862b4b..3984193
From https://github.com/choldgraf/git_wont_pull
   6862b4b..3984193  master     -> origin/master
error: Your local changes to the following files would be overwritten by merge:
	nowimworking.txt
Please, commit your changes or stash them before you can merge.
Aborting

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"

In [39]:
%%bash
cd myRepo/
git fetch origin

Now we need to commit our changes so that git can properly compare them

In [27]:
%%bash
git add -A
git commit -m "I'm committing my changes so that I can merge with the remote repository"
usage: git diff [--no-index] <path> <path>

Now, try to merge...a conflict will come up, so we will use a mergetool

In []:
%%bash
git merge origin/master
git mergetool

Now, we have the merge completed, we need only commit the merge and push to the repository

In []:
%%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!

In [31]:
%%bash
rm -r myRepo/
rm -r ../person2/myRepo2/
Create my repo
In [32]:
%%bash
git clone https://github.com/choldgraf/git_wont_pull myRepo
cd myRepo
git status
Cloning into 'myRepo'...
# On branch master
nothing to commit (working directory clean)
Create the second (your friend's) repo
In [33]:
%%bash
git clone http://github.com/choldgraf/git_wont_pull ../person2/myRepo2
cd ../person2/myRepo2
git status
Cloning into '../person2/myRepo2'...
# On branch master
nothing to commit (working directory clean)

This totally resets the git repository back to its original commit

In [28]:
%%bash
cd myRepo/
git reset --hard 3d93d42d5230ac50677d847350c3c455fd792c94
git push -f
HEAD is now at 3d93d42 first commit
To https://github.com/choldgraf/git_wont_pull
 + c42e00a...3d93d42 master -> master (forced update)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值