What is the difference between GIT and SVN?

The difference between GIT and SVN is

a) Git is less preferred for handling extremely large files or frequently changing binary files while SVN can handle multiple projects stored in the same repository.

b) GIT does not support ‘commits’ across multiple branches or tags. Subversion allows the creation of folders at any location in the repository layout.

c) Gits are unchangeable, while Subversion allows committers to treat a tag as a branch and to create multiple revisions under a tag root.

How to resolve GIT conflicts ? Step By Step Explanation

There are 2 ways in which git merge or a git pull (which is a git fetch and then a git merge) can fail

1. Git can fail to start the merge

This occurs because git knows there are changes in either your working directory or staging area that could be written over by the files that you are merging in. If this happens, there are no merge conflicts in individual files. You need to modify or stash the files it lists and then try to do a git pull again. The error messages are as follows:

error: Entry '' not uptodate. Cannot merge. (Changes in working directory)

or

error: Entry '' would be overwritten by merge. Cannot merge. (Changes in staging area)

2. Git can fail during the merge

This occurs because you have committed changes that are in conflict with someone else’s committed changes. Git will do its best to merge the files and will leave things for you to resolve manually in the files it lists. The error message is as follows:

CONFLICT (content): Merge conflict in 
Automatic merge failed; fix conflicts and then commit the result.

How do I know which files have conflicts in them?

If your merge failed to even start, there will be no conflicts in files.

If git finds conflicts during the merge, it will list all files that have conflicts after the error message.

You can also check on which files have merge conflicts by doing a ‘git status’.

git status

It will show you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example:

  # Changes to be committed:
  #   (use "git reset HEAD ..." to unstage)
  #
  #	modified:   
  #
  # Changed but not updated:
  #   (use "git add ..." to update what will be committed)
  #   (use "git checkout -- ..." to discard changes in working directory)
  #
  #	unmerged:   
  #

“Changes to be committed”: All committed changes to files that are not affected by the conflict are staged.
“Changed but not updated … unmerged”: All files that have conflicts that must be resolved before repository will be back to working order.

How do I find conflicts within the file itself?

Conflicts are marked in a file with clear line breaks:

<<<<<<< HEAD:mergetest
This is my third line
=======
This is a fourth line I am adding
>>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest

<<<<<<<: Indicates the start of the lines that had a merge conflict. The first set of lines are the lines from the file that you were trying to merge the changes into.
=======: Indicates the break point used for comparison. Breaks up changes that user has committed (above) to changes coming from merge (below) to visually see the differences.
>>>>>>>: Indicates the end of the lines that had a merge conflict.

How do I resolve a merge conflict in a file?


You resolve a conflict by editing the file to manually merge the parts of the file that git had trouble merging. This may mean discarding either your changes or someone else’s or doing a mix of the two. You will also need to delete the ‘<<<<<<>>>>>>’ in the file.

What do I do after I’ve resolved conflicts in all affected files?

git add the file(s), git commit and git push (Push only for branches tracked.)
(Note added by Chin – need to commit everything, not just the resolved conflict file.)

General commands to Identify Merge conflicts

git status

Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example:

  # Changes to be committed:
  #   (use "git reset HEAD ..." to unstage)
  #
  #	modified:   
  #
  # Changed but not updated:
  #   (use "git add ..." to update what will be committed)
  #   (use "git checkout -- ..." to discard changes in working directory)
  #
  #	unmerged:   
  #

“Changes to be committed”: All committed changes to files that are not affected by the conflict are staged.
“Changed but not updated … unmerged”: All files that have conflicts that must be resolved before repository will be back to working order.

git diff

You can run four different types of diff commands,

git diff HEAD
Show what has changed since the last commit.

git diff HEAD^
Show what has changed since the commit before the latest commit.

git diff –cached or the synonym git diff –staged
Show what has been added to the index via git add but not yet committed.

git diff
Show what has changed but hasn’t been added to the index yet via git add.

Example

diff --git a/mergetest b/mergetest
index 9be56b9..0aeffac 100644
--- a/mergetest
+++ b/mergetest
@@ -1,3 +1,4 @@
 hello
+I am also editing this line
 This is a test
-This is my third line
+This is a fourth line I am adding

Changes coming from origin/master are marked with +, while changes that are in your local repository (HEAD) are marked with -. This syntax does not notify which lines are added or deleted but just which lines originate in which state of the file.

Commands specifically for when git refuses to start merge

git stash

IMPORTANT: Do not use git stash if git went through with the merge and there were merge conflicts! Only use git stash if git refused to merge because it foresees there being conflicts.

git stash: stashes away any changes in your staging area and working directory. This command is useful in saving all changes not ready to be committed and the user wants to have an updated repository.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

git stash save “”: Save changes to files in working directory and staging area that git is aware of

 git stash save "Saved changes for stash example"
 Saved working directory and index state "On master: Saved changes for stash example"
 HEAD is now at 4e2b407 Added second file for example.

git stash pop: Removes the most recent stash or any stash specified and applies changes as a merge. If merge fails the stash is not removed from the list and must be removed manually.

git checkout

git checkout : Can be used to trash changes in the working directory so as to allow a git pull.

git reset –mixed

git reset –mixed: Can be used to unstage files so as to allow a git pull.

Commands specifically for when git conflicts arise during a merge

git reset

git reset –hard: reset repository in order to back out of merge conflict situation. git reset, particularly with the –hard option can be used to back out of merge conflict

IMPORTANT: Do not use any other options other than –hard for reset when resolving a situation where git failed during the merge, as they will leave conflict line markers in file and you can end up committing files with conflict markers still present.

Git refuses to start a merge/pull

Error Messages:

error: Entry '' not uptodate. Cannot merge. (Changes in working directory)
error: Entry '' would be overwritten by merge. Cannot merge. (Changes staged, but not commited)

Steps toward Resolution:

  1. git stash save “” (Stashes away any changes in your staging area and working directory in a separate index.) OR git checkout (throws out your changes so that you can do a merge). This command is useful in saving all changes not ready to be committed and the user wants to have an updated repository.
  2. git status (Verify all changes are staged)
  3. git pull or git merge (Bring in changes from central repository or another branch)
  4. Only if did a ‘git stash’ in step 1: git stash pop (Will repopulate your changes into your working directory, you may have to resolve merge conflicts)

Git is unable to resolve a merge/pull

Error Message:

CONFLICT (content): Merge conflict in 
Automatic merge failed; fix conflicts and then commit the result.

Steps toward Resolution:

  1. git status (Shows all files that are in conflict as unmerged changed in working directory.)
  2. Resolve merge conflicts
  3. git add
  4. git commit -m “message”

How to resolve conflicts in git merge in brief

When Git refuses to start a merge/pull

Error Messages:

error: Entry '' not uptodate. Cannot merge. (Changes in working directory)
error: Entry '' would be overwritten by merge. Cannot merge. (Changes staged, but not commited)

Steps toward Resolution:

git stash save “” (Stashes away any changes in your staging area and working directory in a separate index.) OR git checkout (throws out your changes so that you can do a merge). This command is useful in saving all changes not ready to be committed and the user wants to have an updated repository.
git status (Verify all changes are staged)
git pull or git merge (Bring in changes from central repository or another branch)
Only if did a ‘git stash’ in step 1: git stash pop (Will repopulate your changes into your working directory, you may have to resolve merge conflicts)

When Git is unable to resolve a merge/pull

Error Message:

CONFLICT (content): Merge conflict in 
Automatic merge failed; fix conflicts and then commit the result.

Steps toward Resolution:

git status (Shows all files that are in conflict as unmerged changed in working directory.)
Resolve merge conflicts
git add
git commit -m “”

Some GIT commands used specifically for when git refuses to start merge ?

git stash

IMPORTANT: Do not use git stash if git went through with the merge and there were merge conflicts! Only use git stash if git refused to merge because it foresees there being conflicts.

git stash: stashes away any changes in your staging area and working directory. This command is useful in saving all changes not ready to be committed and the user wants to have an updated repository.

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

git stash save “”: Save changes to files in working directory and staging area that git is aware of

 git stash save "Saved changes for stash example"
 Saved working directory and index state "On master: Saved changes for stash example"
 HEAD is now at 4e2b407 Added second file for example.

git stash pop: Removes the most recent stash or any stash specified and applies changes as a merge. If merge fails the stash is not removed from the list and must be removed manually.

git checkout

git checkout : Can be used to trash changes in the working directory so as to allow a git pull.

git reset –mixed

git reset –mixed: Can be used to unstage files so as to allow a git pull.

General commands to Identify Merge conflicts

git status

Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example:

  # Changes to be committed:
  #   (use "git reset HEAD ..." to unstage)
  #
  #	modified:   
  #
  # Changed but not updated:
  #   (use "git add ..." to update what will be committed)
  #   (use "git checkout -- ..." to discard changes in working directory)
  #
  #	unmerged:   
  #

“Changes to be committed”: All committed changes to files that are not affected by the conflict are staged.
“Changed but not updated … unmerged”: All files that have conflicts that must be resolved before repository will be back to working order.

git diff

You can run four different types of diff commands,

git diff HEAD
Show what has changed since the last commit.

git diff HEAD^
Show what has changed since the commit before the latest commit.

git diff –cached or the synonym git diff –staged
Show what has been added to the index via git add but not yet committed.

git diff
Show what has changed but hasn’t been added to the index yet via git add.

Example

diff --git a/mergetest b/mergetest
index 9be56b9..0aeffac 100644
--- a/mergetest
+++ b/mergetest
@@ -1,3 +1,4 @@
 hello
+I am also editing this line
 This is a test
-This is my third line
+This is a fourth line I am adding

Changes coming from origin/master are marked with +, while changes that are in your local repository (HEAD) are marked with -. This syntax does not notify which lines are added or deleted but just which lines originate in which state of the file.

How to find GIT conflict within the file & resolve it ?

If your merge failed to even start, there will be no conflicts in files.

If git finds conflicts during the merge, it will list all files that have conflicts after the error message.

Conflicts are marked in a file with clear line breaks:

<<<<<<< HEAD:mergetest
This is my third line
=======
This is a fourth line I am adding
>>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergetest

<<<<<<<: Indicates the start of the lines that had a merge conflict. The first set of lines are the lines from the file that you were trying to merge the changes into.
=======: Indicates the break point used for comparison. Breaks up changes that user has committed (above) to changes coming from merge (below) to visually see the differences.
>>>>>>>: Indicates the end of the lines that had a merge conflict.

How do I resolve a merge conflict in a file?

You resolve a conflict by editing the file to manually merge the parts of the file that git had trouble merging. This may mean discarding either your changes or someone else’s or doing a mix of the two. You will also need to delete the ‘<<<<<<<‘, ‘=======’, and ‘>>>>>>>’ in the file.

What do I do after I’ve resolved conflicts in all affected files?

git add the file(s), git commit and git push (Push only for branches tracked.)
(Note added by Chin – need to commit everything, not just the resolved conflict file.)

How to identify git conflict files during git merge or git pull ?

If your merge failed to even start, there will be no conflicts in files.

If git finds conflicts during the merge, it will list all files that have conflicts after the error message.

You can also check on which files have merge conflicts by doing a ‘git status’.

git status

It will show you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Example:

  # Changes to be committed:
  #   (use "git reset HEAD ..." to unstage)
  #
  #	modified:   
  #
  # Changed but not updated:
  #   (use "git add ..." to update what will be committed)
  #   (use "git checkout -- ..." to discard changes in working directory)
  #
  #	unmerged:   
  #

“Changes to be committed”: All committed changes to files that are not affected by the conflict are staged.
“Changed but not updated … unmerged”: All files that have conflicts that must be resolved before repository will be back to working order.