Let's set the scene. Say you are a web freelancer and are almost finished with a client's new website. Over the years, you have learned the hard way not to edit the files directly over FTP. It's too easy to make breaking changes with no record of what changed and who did what. Nowadays you are using Git to manage the version of the files. Let's cover that, and also the last mile: deploying only the files known to be changed to the server.
I got frustrated of not being able to tell the tabs apart as I was working on stuff. So this is my so-dumb-it's-smart solution.
The only hitch in my gittyup was that I had to add it to .gitignore, which untracked the file, which deletes it, and had to manually slip it back onto the server. Although it looks like there are smarter ways.
Wait, doesn't GitHub already have two desktop apps? If the same question crossed your mind when you first read that, then you are not alone. Yes, GitHub did have two desktop apps—one for Mac OS and one for Windows—but decided to unify them into a single app. Instead of GitHub for Mac and GitHub for Windows, we are now left with just GitHub Desktop.
I was planning to write up a full review of the app but decided that forming an opinion on it after only a few days of use (and not even on a real project) would be unfair. Instead, I decided that we could unbox it together and see what it's all about. (more…)
Most people wouldn't call version control their hobby. But in the last few years, people have also become aware of how valuable it is. Nowadays, you won't find a top programmer, web developer, or web designer who doesn't use version control. In part because it helps you produce better results and makes collaboration easy. But also because it can save your life when things go wrong.
When we started out creating Tower in 2010, we had a goal clearly in mind: make Git, the best version control available, easier to use. Since then we've made this true for over 35,000 customers in companies like Apple, Google, Amazon, and Ebay.
Everybody has a list of things they don't like: an appointment at the dentist, a traffic jam, or a canceled flight. When I was preparing my book, I wasn't surprised that a lot of designers and developers would add merge conflicts to this list without hesitation.
When using Git for version control, there is nothing to fear. Once you understand how merge conflicts work and how to deal with them, I'm sure you'll be able to cross them off this list.
You Cannot Break Things
The first thing that you should keep in mind is that you can always undo a merge and go back to the state before the conflict occurred. You're always able to undo and start fresh.
If you're coming from another version control system (e.g. Subversion) you might be traumatized: conflicts in Subversion have the (rightful) reputation of being incredibly complex and nasty. One reason for this is that Git, simply stated, works completely different in this regard than Subversion. As a consequence, Git is able to take care of most things during a merge - leaving you with comparatively simple scenarios to solve.
Also, a conflict will only ever handicap yourself. It will not bring your complete team to a halt or cripple your central repository. This is because, in Git, conflicts can only occur on a your local machine - and not on the server.
How a Merge Conflict Occurs
In Git, "merging" is the act of integrating another branch into your current working branch. You're taking changes from another context (that's what a branch effectively is: a context) and combine them with your current working files. Have a look at this introduction to branching if you're new to the concept in general.
A great thing about having Git as your version control system is that it makes merging extremely easy: in most cases, Git will figure out how to integrate new changes.
However, there's a handful of situations where you might have to step in and tell Git what to do. Most commonly, this is when there are changes to the same file on both branches. Even in this case, Git will most likely be able to figure it out on its own. But if two people changed the same lines in that same file, or if one person decided to delete it while the other person decided to modify it, Git simply cannot know what is correct. Git will then mark the file as having a conflict - which you'll have to solve before you can continue your work.
How to Solve a Merge Conflict
When faced with a merge conflict, the first step is to understand what happened. Did one of your colleagues edit the same file on the same lines as you? Did they delete a file that you modified? Did you both add a file with the same name?
Git will tell you that you have "unmerged paths" (which is just another way of telling you that you have one or more conflicts) via "git status":
Let's take an in-depth look on how to solve the most common case: when two changes affected the same file on the same lines.
Now is the time to have a look at the contents of the conflicted file. Literally open it in your code editor. Git is nice enough to mark the problematic area in the file by enclosing it in "<<<<<<< HEAD" and ">>>>>>> [other/branch/name]".
The contents after the first marker originate from your current working branch. After the angle brackets, Git tells us where (from which branch) the changes came from. A line with "=======" separates the two conflicting changes.
Our job is now to clean up these lines: when we're done, the file should look exactly as we want it to look. It can be necessary to consult the teammate who wrote the conflicting changes to decide which code is finally correct. Maybe it's yours, maybe it's theirs, or maybe a mixture between the two.
Opening the raw file in your editor and cleaning it up there is perfectly valid, but not very comfortable. Using a dedicated merge tool can make this job a great deal easier. You can configure your tool of choice using the "git config" command. Consult your tool's documentation for detailed instructions. Note that just because you have git installed doesn't mean you have a dedicated merge tool installed, these are separate, optional software tools.
Then, in case of a conflict, you can later invoke it by simply typing "git mergetool".
For this example, I've used Kaleidoscope on Mac:
The left and right panes stand for the conflicting changes; a far more elegant visualization than <<<<<<<" and ">>>>>>>".
You can now simply toggle which change shall be taken. The middle pane shows the resulting code; in good tools, you can even edit this further.
Now, after cleaning up the file with the final code, all that's left is to save it. To give Git a hint that you're done with this file, you should quit the merge tool to continue. Behind the scenes, this told Git to execute a "git add" command on the (now formerly) conflicted file. This marks the conflict as solved. Should you decide not to use a merge tool and instead clean up the file in your editor, you'll have to mark the file as resolved by hand (by executing "git add <filename>").
Finally, after resolving all conflicts, a merge conflict situation needs to be concluded by a regular commit.
How to Undo a Merge
As already said, you can return to the state before you started the merge at any time. This should give you the confidence that you can't break anything. On the command line, a simple "git merge --abort" will do this for you.
In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard <commit-hash>" and start over again.
Tools Can Make Things Easier
I've already mentioned it briefly: being equipped with good tools can make your life quite a bit easier. A proper merge tool will help you better understand a conflict scenario and make the right decision.
With all the safety nets in place that Git provides, there's really no need to be afraid of merge conflicts in any way. Once understood how they work and what your options are, you should be ready to merge like there's no tomorrow. Always keep in mind: you can't break anything!
Note from the Editor
Tobias modestly left out Tower as an excellent tool to help with merge conflicts. In fact one of the reasons I personally use Tower is because I like how it helps with merge conflicts.
It's very clear when you have a conflict:
And right-clicking on the conflicted file gives you some nice options:
So my process is usually:
- Is it very obvious to me whether my changes to this file or the other branches changes to this file should "win"? If so, select the relevant "Resolve <file> Using Mine" or "Resolve <file> Using Theirs"
- If I need a closer look, pop it open in a code editor and check it out. Then maybe still I can use one of those options.
- If it's more complicated, manually fix it up and then choose "Mark <file> as Manually Resolved" from Tower.
- Sometimes use a Merge Tool if there is a lot of complex fixes, which Tower also supports opening conflicts in via the "Open <file> in Merge Tool" option.
In this screencast I move my own personal website from my old live FTP editing ways to a proper version controlled system including deployment. I haven't had much experience with this, so forgive me if it's a bit rough.
We start by moving the live website local, including bringing all the files down and copying the database. Then we set up a Git repository in Beanstalk and push it all up to that. Then we give Beanstalk our FTP credentials and set up how we want deployment done. Then we make some local changes to our site, and commit/push them to Beanstalk, which does the deployment for us. By then end, it's all working perfectly.
If you've got questions/comments/suggestions on this workflow, I'd love to hear!
Links from the video:
- Plesk / phpMyAdmin
- ChrisCoyier.net / MediaTemple
- Sequel Pro
- Sublime Text 2
- MAMP / MAMP Screencast  
- Git Tower / GitHub Screencast
Mark Jaquith on Working with WordPress Locally (specifically, dealing with plugins and config).
You are probably pretty aware of why using version control is a good thing. In case you aren't, I quickly go through that in this video. Then we get into the most basic thing we can possibly do: put a project onto GitHub. If you are like me, you don't particularly enjoy "the command line", but between that and a Mac GUI app, we manage to get it done.
Links from the Video: