Git: Conflicts, Log and Reset

I don’t work with the Git that often, so decided to refresh my knowledge by making this post for reference.

A merge conflict occurs when two people attempt to change a file in a ways that conflict with each other (same lines of the code).

We will create a new repository on GitHub and use it for further testing:

Note the HTTPS url for cloning the repository:

Now it’s time to get this repository locally on the computer. (Make sure you have git installed)

Download the repository using “git clone” command:

git clone <repository url>

In this case <repository url>: https://github.com/dagolovach/super-duper-octo-enigma.git

We can get into the directory using “cd super-duper-octo-enigma” and check the “git status“:

$ cd super-duper-octo-enigma
$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

On branch main – currently there is just one main branch
Your branch is up to date with ‘origin/main’ – local copy is synced with remote verion

I am going to create a simple python file and use it as an example to demonstrate Merge Conflicts and Resets.

Merge/Pull/Push Conflicts

Initial version of the file:

Changing file locally:

if __name__ == "__main__":
    print("Hello World!!!")

Changing file on GitHub (mimic changes made by another person working on the same file):

Note: I am changing the same line of the code.

So now we have changed local file and changed GitHub file.

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   test_file.py

no changes added to commit (use "git add" and/or "git commit -a")

If we try to commit changes and push our version to the GitHub, here is what we got:

I am using “git pull” to get remote changes => Merge Conflict.

Here is how file looks like in case of Merge Conflict (it was modified by git itself):

Between “<<<<<<< HEAD” and “=======” – my local changes
Between “=======” and “>>>>>>> 470d032038c366c6b2b63af3b59ca2ee89033307” – remote version

470d032038c366c6b2b63af3b59ca2ee89033307 – commit hash

How to fix it: just decide which version should be as a final result, remove all git information and push changes again:

$ git commit -am 'fix merge conflicts'
[main 70e431a] fix merge conflicts

$ git push
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 16 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 586 bytes | 586.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To https://github.com/dagolovach/super-duper-octo-enigma.git
   470d032..70e431a  main -> main

As a result – all files are synced:

Git Log and Reset

Another useful git command is “git log“, which gives you a history of all of your commits on that repository:

Using the commit hash we can revert back to a previous commit using “git reset” command:

  • git reset –hard <commit hash> reverts your code to exactly how it was after the specified commit
  • git reset –hard origin/master reverts your code to the version currently stored online on Github
$ git reset --hard 8a56e06f9b8f3d9cdcbec95e476b6412d3651cca
HEAD is now at 8a56e06 Add 2 exclamation marks

$ cat test_file.py
if __name__ == "__main__":
  print("Hello World!!!")

$ git status
On branch main
Your branch is behind 'origin/main' by 7 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

If you want to push commits behind (not a good idea) use “git push -f

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *