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.
First, you sync with the remote repository
%%bash
cd myRepo/
ls
git pull
README.md Already up-to-date.
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
# 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!
%%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
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
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.
%%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!
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.
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
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"
usage: git diff [--no-index] <path> <path>
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.
%%bash
rm -r myRepo/
rm -r ../person2/myRepo2/
%%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)
%%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)
%%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)