0

I am new to Git versioning tool. Previously I was using SVN.

If we have a file a.php which has some modifications on the 50th line number and there are changes in the 500th line number in another branch for the same file which has been committed. How do we merge the two branches without stashing the changes or using git checkout --patch ?

In SVN we just used to execute one command 'svn update a.php'. Whereas here we will have to execute three commands. git stash, git merge branchname, git stash apply.

Thanks in advance.

Pawan Rao
  • 59
  • 9

2 Answers2

2

In Git, you always commit before you merge. The idea is to never have any modified files in the workspace when you merge. This is one of the main features over SVN: In Subversion, your uncommitted changes would be merged with work by someone else. The result of that is sometimes unpredictable. The most predictable thing is that it's always a lot of work to sort out the mess which svn update leaves behind.

In Git, a branch is very, very cheap. It's so cheap in fact that you hardly notice when you create one.

So you always commit everything. Then you say "I want to merge with that commit over there" (usually by giving Git the name of the branch which contains the commit).

Something goes wrong? No problem, Git can restore the previous state without fail (unlike Subversion) since the previous state was committed to the repository.

Now there are people who believe that linear history is so valuable that they feel they have to sacrifice this feature. This is usually not true or causes more problems that it solves. It's especially dangerous to do when you're starting with Git. rebase is a complex and sometimes dangerous operation and not for beginners.

That's why I suggest to start with a simple workflow:

  • Commit your work.
  • Pull (which will automatically merge)
  • If something goes wrong: Undo (see below)
  • Run your tests
  • When everything looks good, push

Related:

Community
  • 1
  • 1
Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
1

If you currently are on one branch with a commit with changes to the 50th line, and you have a parallel branch with a commit with changes to the 500th line, and you want to merge those 2 branches, you run git merge <parallel branch>. This effectively merges the parallel branch into your current branch.

As the changes in the 2 commits are on separate lines in the file, then the merge will be automatically done by Git without any conflicts.

Jonas Bang Christensen
  • 1,021
  • 2
  • 10
  • 17