git pull upstream code example
Example 1: update my fork repository at github
git remote add upstream https://github.com/whoever/whatever.git
git fetch upstream
git checkout master
git rebase upstream/master
Example 2: git fetch upstream
These steps update the master branch.
1. Make sure you are on the appropriate branch.
git checkout master
2. Fetch content from upstream
git fetch upstream
3. Merge upstream with the appropriate local branch
git merge upstream/master
4. Get help on Resolve merge conflicts if these occur.
5. If you also maintain a GitHub repository, push changes to
GitHub’s (origin) master branch
git push origin master
Example 3: git pull updates from fork
$ cd github-services
$ git remote add upstream git://github.com/pjhyett/github-services.git
$ git fetch upstream
$ git merge upstream/master master
$ git rebase upstream/master
Example 4: how to pull the latest changes from git
Case 1: Don’t care about local changes
Solution 1: Get the latest code and reset the code
git fetch origin
git reset --hard origin/[tag/branch/commit-id usually: master]
Solution 2: Delete the folder and clone again :D
rm -rf [project_folder]
git clone [remote_repo]
Case 2: Care about local changes
Solution 1: no conflicts with new-online version
git fetch origin
git status
will report something like:
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
Then get the latest version
git pull
Solution 2: conflicts with new-online version
git fetch origin
git status
will report something like:
error: Your local changes to the following files would be overwritten by merge:
file_name
Please, commit your changes or stash them before you can merge.
Aborting
Commit your local changes
git add .
git commit -m ‘Commit msg’
Try to get the changes (will fail)
git pull
will report something like:
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.
Open the conflict file and fix the conflict. Then:
git add .
git commit -m ‘Fix conflicts’
git pull
will report something like:
Already up-to-date.
Example 5: git fetch upstream from master
$ git rebase upstream/master
$ git checkout master
$ git fetch upstream
Example 6: git fetch upstream
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
$ git remote add upstream https://github.com/otheruser/repo.git
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
upstream https://github.com/otheruser/repo.git (fetch)
upstream https://github.com/otheruser/repo.git (push)