Skip to content

Instantly share code, notes, and snippets.

@choldgraf
Created August 27, 2013 22:29
Show Gist options
  • Save choldgraf/6359961 to your computer and use it in GitHub Desktop.
Save choldgraf/6359961 to your computer and use it in GitHub Desktop.
Sample tutorial for dealing with a "git pull" error
{
"metadata": {
"name": "git_wont_pull"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"What to do when git won't pull"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Monday"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Say you've got a repository on your local computer that you sync to a remote repository that your whole lab shares.\n",
"\n",
"You do some work on Monday, and push it to the repository. "
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Here's your (correct) workflow for Monday."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, you sync with the remote repository"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"ls\n",
"git pull"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"README.md\n",
"Already up-to-date.\n"
]
}
],
"prompt_number": 34
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, you create a file and add it to be committed."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"touch nowimworking.txt\n",
"echo \"Look at me. Work work work\" > nowimworking.txt\n",
"\n",
"git add -A\n",
"git status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"# On branch master\n",
"# Changes to be committed:\n",
"# (use \"git reset HEAD <file>...\" to unstage)\n",
"#\n",
"#\tnew file: nowimworking.txt\n",
"#\n"
]
}
],
"prompt_number": 35
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, you commit and push this to the repository. This is the right way of doing things! Good job on Monday!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"git commit -m \"I've committed all of this work!\"\n",
"git push"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[master 6862b4b] I've committed all of this work!\n",
" 1 file changed, 1 insertion(+)\n",
" create mode 100644 nowimworking.txt\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"To https://github.com/choldgraf/git_wont_pull\n",
" 3d93d42..6862b4b master -> master\n"
]
}
],
"prompt_number": 36
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"While you were sleeping, your co-worker is busy at work!"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Tuesday morning"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, you arrive on Tuesday morning, and realize that you didn't name the file correctly..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"so you change it, and you also change the text"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"echo \"Whoops, I forgot to change something important\" > nowimworking.txt\n",
"cat nowimworking.txt"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Whoops, I forgot to change something important\n"
]
}
],
"prompt_number": 37
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"then you realize \"whoops, I forgot to synchronize my code to the repository\". \n",
"\n",
"This might be important because another lab member was changing some code the night before."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You stop writing code, move to the command line, and quickly try a git pull."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"git pull"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Updating 6862b4b..3984193\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"From https://github.com/choldgraf/git_wont_pull\n",
" 6862b4b..3984193 master -> origin/master\n",
"error: Your local changes to the following files would be overwritten by merge:\n",
"\tnowimworking.txt\n",
"Please, commit your changes or stash them before you can merge.\n",
"Aborting\n"
]
}
],
"prompt_number": 38
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whoops! You just broke git!"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"So, what just happened?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ultimately, git wants to make sure that nobody unintentionally overwrites their own work or the work of others. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" In this case, person A made changes to their local repository, but they didn't sync person B's changes first.\n",
" \n",
" So, if person A had sync'ed, it would have overwritten their own changes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Git assumes you don't want this to happen, so it returns an error rather than overwriting stuff for you."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"How do get around this?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's important to know that whenever you \"git pull\", git is really doing two things:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A \"git fetch\", which retrieves the latest copy of the remote repository and stores it somewhere to be used later"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A \"git merge\", which tries to merge that fetched repository with the branch you're currently on"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this case, the \"merge\" noted that there were conflicts between the remote repository and your own"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Luckily, fixing this problem is as easy as performing the abovementioned commands <em>independently</em> of one another"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"So here's how you fix this:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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\""
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"git fetch origin"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 39
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we need to commit our changes so that git can properly compare them"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git add -A\n",
"git commit -m \"I'm committing my changes so that I can merge with the remote repository\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"usage: git diff [--no-index] <path> <path>\n"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, try to merge...a conflict will come up, so we will use a mergetool"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git merge origin/master\n",
"git mergetool"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we have the merge completed, we need only commit the merge and push to the repository"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git commit -m \"Successfully merged\"\n",
"git push"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, your local repository is synced with the remote repo, and we can continue our work."
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"The End"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"To delete the git repository and re-initialize a new one!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"rm -r myRepo/\n",
"rm -r ../person2/myRepo2/"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 31
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Create my repo"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git clone https://github.com/choldgraf/git_wont_pull myRepo\n",
"cd myRepo\n",
"git status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Cloning into 'myRepo'...\n",
"# On branch master\n",
"nothing to commit (working directory clean)\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Create the second (your friend's) repo"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"git clone http://github.com/choldgraf/git_wont_pull ../person2/myRepo2\n",
"cd ../person2/myRepo2\n",
"git status"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Cloning into '../person2/myRepo2'...\n",
"# On branch master\n",
"nothing to commit (working directory clean)\n"
]
}
],
"prompt_number": 33
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"This totally resets the git repository back to its original commit"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%bash\n",
"cd myRepo/\n",
"git reset --hard 3d93d42d5230ac50677d847350c3c455fd792c94\n",
"git push -f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"HEAD is now at 3d93d42 first commit\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"To https://github.com/choldgraf/git_wont_pull\n",
" + c42e00a...3d93d42 master -> master (forced update)\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment