Best way to clone a github repository - ruby-on-rails-3

Ok so here's the scenario.
I made a pretty bad newbie mistake. I was working on some code and didn't create a dev branch to checkout and test in development. So I've made all of these changes to the master branch on my machine that are still broken (in development) and my boss wants me to make some changes to the app in production.
My thoughts on how to do this was:
Rename my project's directory so I have a backup of it
git clone the last remote version back onto my machine
Make the slight changes he wants and re-deploy/commit
Sort out what I've changed in my old version of the app and bring it into a dev branch into the newly cloned master branch.
Any thoughts on this? I really don't want to screw up production right now and I've made too many changes to keep track of to revert to where the code was stable.

No need to go through that much trouble:
git checkout master # get the 'bad' version
git branch bad-master # make a new branch called 'bad-master', cut off master's current state
git fetch # make sure you're up to date with the remote
git reset --hard origin/master # reset your master branch to origin/master's state
<work work work>
git checkout bad-master # when you're ready to work on bad-master again
Note that git reset --hard origin/master will discard any work in your working tree that you haven't checked in... so if you've got work you haven't committed yet, be sure to git stash it.

This is a completely normal situation to wind up in, and one you can easily recover from.
All you need to do is:
Create a new branch pointing to your current version of master
git checkout -b my-feature-branch
Reset your master to the same thing as origin/master
git checkout master
git reset --hard origin/master
That's it. Now your master is the same as it was before you did your feature development, and you have a feature branch set aside which you can later checkout to resume development.

Related

Detached head branch after SVN-CLONE

In my company, we have moved from svn to git. We did not use any third party tools for this. We ran git svn clone --stdlayout --no-metadata --authors-file=authors.txt temp_name. This seemed suffucient to get what we wanted. We then pushed the repos to bitbucket. However what we noticed after pushing is, in one of the repos, a branch was missing in bitbucket. So, I did a checkout of the branch, then I got the message
"You are in detached HEAD state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout." Then I googled for resolving the detached head problem but none of the posts talks about the detached head after git svn clone. The main thing I want to achieve is I just want the detached branch pushed to remote master immediately after git svn clone. I do not know what I'm missing
Don't worry much about the detached HEAD message itself. As long as you get the needed revisions in your working copy, simply push it where you want them to be:
git push <your_bit_bucket_remote> <revision>:refs/heads/<branch_name_at_bitbucket>
where <revision> can be something like HEAD, svn/branch_name or an explicit SHA1.

Git merge with overwriting local changes [duplicate]

In Git, during a merge, is there a way that we can tell git to discard local changes in case of a conflict and apply the changes from the merged branch?
I mean if there is a way, then we can do merges like branch merges without conflicts.
Before trying to merge, you can discard the local changes yourself git reset --hard HEAD.
You can replace HEAD by whatever commit hash you want.
This will bring you the the clean state of the commit you're actually on, and you'd lose all your changes.
If you want to keep them, you can stash them before with git stash, or move them to another branch:
git checkout -b new_branch
git add .
git commit -m "My awesome commit"
git checkout - # will bring you back to the last branch you were in
If you want to ignore all local changes, and an additional merge commit you want to just move your branch to the remote HEAD.
git log --oneline origin/master
# assume the first sha is bbdfa17
git reset --hard bbdfa17
Now you are at the tip of the tree with no merge commits.
It sounds like you want to read about the merge strategies 'theirs' and 'ours'. When merging you can specify that either your current branch (ours) or the remote branch (theirs) is the correct one.

Which branch should I run dcommit on when using git-svn?

I have just started using TortoiseGIT to work with my SVN repository at work (I have to jump between branches quite a bit so this saves on quite a bit of down time). However, I've only used Git or SVN, but not git-svn. Should I merge my local branch with remotes/trunk first or is just doing a dcommit from the master sufficient enough to get my changes into the trunk?
I always rebase it so there will only be fast forward movement for the trunk ref. I wonder if git-svn works correctly for merging. As I remember, git-svn, after dcommit, will perform a fetch from svn, and reset the head to position of new trunk. It gotta make your revision graph a mess, even git-svn can pick up correct revision to dcommit.

git-svn branch - How to keep branch in sync with trunk?

There are plenty of questions about git-svn workflow, but I haven't been able to figure this one out:
This section of the svn book talks about a common practice with SVN: you make a branch, and you keep merging changes from the trunk as the trunk gets updated, so that the branch always includes the latest changes.
I did git svn branch to create a branch on svn and then set up a tracking branch to work on it. These questions cover the process pretty well.
Now suppose there were changes made to the trunk, which I now want to merge into the branch. What is my best option? Note that I need to keep git-svn happy, and not mess up the work of people using the branch with subversion, so just doing a rebase would probably not work.
This question seems to talk about a similar situation, although it's pretty old, and I'm not sure what the bottom line there was - it seems to suggest I should git checkout master and then git rebase mybranch, but that can't be right.
I suspect the the answer should be something that has the effect of svn merge, preferably with setting the mergeinfo property, but alas, there is no git svn merge...
I don't really understand how this simple question was left unanswered for more than a week, with only 13 views so far. I guess it was my fault, bad question writing.
Anyway, I figured it out myself. Short version: just use git merge instead of git rebase.
My confusion came from using git rebase when syncing a branch with the changes in master. When working on local branches, this usually works great, and keeps the history clean. However, you should not rebase commits that you have pushed to a public repository, and the subversion repository is (apparenly) public enough. git merge, on the other hand, works beautifully, and doesn't have any problem.
So, the long answer is: when you want to merge the latest changes in the trunk into the svn branch you're tracking, just do:
git merge master
# Handle conflicts, git add when you're done
git commit
git svn dcommit
This will keep your branch in sync with trunk, but will not set mergeinfo, so you probably should not mix svn merge with this kind of practice.

How to dcommit only selected patches with git svn?

I have a number of locally committed patches in my git-svn repo which I haven't yet commited to our svn repo. A normal "git svn dcommit" will commit all of these patches to svn. I would like to commit only some of my patches (simple bug fixes), but not others (untested major changes). How can I do this with git svn?
I've been following the procedure here:
http://fredericiana.com/2009/12/31/partial-svn-dcommit-with-git/
If you're comfortable rebasing, it works pretty well.
Here's what I ended up doing. The starting point is the "master" branch synced with svn, with all of my local patches on top.
Create a new branch (wip = Work In Progress).
git branch wip
This makes a copy of the current branch, including all patches not yet committed to svn. The current branch will stay as "master" and will not be changed.
Remove the unwanted local patches from "master" with a rebase:
git rebase -i HEAD~10
Now the "master" branch has patches you can safely commit:
git svn dcommit
The "wip" branch now has the major changes which aren't yet ready for sharing. Actually, I want them to stay there and this is where I would stop. It's possible to do the svn dcommit from the "wip" branch once everything is finalized. But for completess' sake, and to answer the original question, there's a final step:
Pull the uncommitted changes back to the "master" branch using git cherry-pick and finally remove the useless branch with git branch -d wip.
With git, you're not actually supposed to operate on single changesets. The best approach I know is to create local branches for any non-trivial work. This way, your untested major changes will end up in different branches of your git repository, and you'll be able to differ them quite easily.
If this is the problem you have at the moment you can probably create create a new branch from the point you last updated from svn and then use git-cherry-pick to transfer your simple bug fixes to this new branch, from which you can then dcommit to svn.
From a more long-term point of view it's best to have your own "master" branch made from subversion trunk, and then either:
Rebase all your branches every time you update from svn, then merge those you want to get to svn to your master and dcommit from there.
Merge stuff from svn using regular git-merge, and then merge stuff to your master for dcommits by git diff ..my_branch | patch -p1, which will eliminate history that git-svn can't handle. This approach is more complicated for the final merging but allows you to merge stuff between branches (and possibly other people) in git itself.