Skip to content

Instantly share code, notes, and snippets.

@esausilva
Last active May 15, 2023 18:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save esausilva/00ac20c3639aaf62758366e26791536d to your computer and use it in GitHub Desktop.
Save esausilva/00ac20c3639aaf62758366e26791536d to your computer and use it in GitHub Desktop.

Get Latest Code Version - 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]
  • Solution 3: This will nuke any changes you've made locally so use carefully. Think about rm -Rf when doing this
git reset --hard HEAD
git clean -xffd
git pull

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.

Source: StackOverflow

@MarshallARoss
Copy link

This is so helpful! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment