[GIT] How to replace the master branch with another branch.

git checkout master
git reset --hard fixed-master-branch
git push origin master -f

Sometimes someone may pollute the ‘master’ branch and you may have noticed this only after you’ve fetched, rebased and pushed your commits.
So your history may end up like this (I’ll use decimal numbers on the commit IDs for illustrative purposes only):

commit: 87
Author: me
A good commit

commit: 86
Author: other guy
Merge: that brought back a lot of crap because they probably used some GUI based git client and they did a git pull without your permission

commit: 85
Author: other guy
Another commit that shouldn't be here

commit: 84
Author: me
Another the last good commit

Now, your master is all effed up and you’re about to have an anxiety attack. What you want is to have this in your git log.

commit: 87
Author: me
A good commit

commit: 84
Author: me
Another the last good commit

To get back to this state is pretty easy:

  1. Check out the last good commit

git checkout 84

Now you will be detached from master, let’s name this temporary branch ‘fixed-master-branch’

git checkout -b fixed-master-branch

Now the last commit you should see should be commit 84.

Let’s now bring our commit, and skip the 2 sucky commits

git cherry-pick 87

Now your ‘fixed-master-branch’ branch looks exactly how you want the master to look like.

But now, you want to replace the old master with the new one.

Go back to the broked master

git checkout master

And fix it, and fuck all that merge –strategy ours BS advice you will find on StackOverflow as it will leave the git log history really weird.

git reset --hard fixed-master-branch

Now when you look at your history, master will be the same as ‘fixed-master’, and to fix your remote repo, you will have to make a push –force and let everyone know that you did that because you needed to clean your repository’s history

git push origin master -f

You’re done.

[SOLVED] Sublime Text 2: Git binary could not be found in PATH

Got this annoying dialog popping up on Sublime Text 2?
Screen Shot 2014-07-22 at 4.31.03 PM

Go to Preferences > Browse Packages …

Screen Shot 2014-07-22 at 4.33.02 PM

a Finder window will open, go to the “Git” folder, open the file called “Git.sublime-settings”

Look for “git_command” and set it’s value to the path of your git executable

Screen Shot 2014-07-22 at 4.31.47 PM

(you can find the path of your git executable on the Terminal by typing “which git”)

Screen Shot 2014-07-22 at 4.35.24 PM

How to add an existing GIT repository to github.

Most of the times, it makes more sense to start working on something that slowly transforms into the beginning of a project that deserves to be on github. This post is about creating a local repository and putting it on github.

1. First we must convert the main local folder into a git repository. For this example let’s call the folder “my-new-project”. With your terminal go to that folder and type:
[bash]git init[/bash]

the repository will initialize but nothing will be added to it yet. If you type git status you will see all the things you can add to it, so use git add to add the folders you want to track, and then go ahead and do a git commit -m “initial commit”

2. Now go to github.com and create your repository “my-new-project”, and copy the clone url of the repo, I personally like to work with the one that starts with “ssh://” since I like to work with ssh keys and not have to deal with passwords.

You can easily configure your ssh certificates for multiple things, not just github but keys for many many servers working with the ~/.ssh/config file (no need to deal with effing ssh-agent).

If you created this github repository with some other account, make sure to give yourself contributor access on the github role settings, otherwise you won’t be able to pull/push.

3. Time to pull (fetch+merge) the remote repo and then push this baby up.
You do that by invoking the following commands (let’s suppose the remote url is git remote add origin git@github.com:myaccount/my-new-project.git):
[bash]git remote add git@github.com:myaccount/my-new-project.git
git pull origin master[/bash]

you should see something like below coming from the remote repo’s master branch:

[bash]warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From github.com:myaccount/my-new-project
* branch master -> FETCH_HEAD
Merge made by the ‘recursive’ strategy.
.gitignore | 6 ++++++
README.md | 2 ++
2 files changed, 8 insertions(+)
create mode 100644 .gitignore
create mode 100644 README.md[/bash]

and then just
[bash]git push[/bash]

and you’re done, you should see your initial commit on github now.

My Git Cheat-sheet

I don’t know if it’s the crazy syntax, but for the life of me, I always need to come back to this cheat sheet, maybe you will too:


fetch remote branch.
git fetch origin nameofbranch
“fetch” downloads the changes of the remote branch but doesn’t automatically merge them.
If you have commited local changes on that branch, you usually fetch and then rebase your changes at the end of the fetched updates.

push local branch to remote
git push origin (pushes current branch to default remote branch)
git push origin nameofbranch (pushes current branch to the remote nameofbranch branch)

delete remote branch (who the fuck thought of this syntax?)
git push origin :branchToDelete (deletes branch on remote repo)
git branch -D branchToDelete (deletes branch on local repo)

clean untracked files (not folders)
git clean -f -x

list your remote repository aliases and full paths
git remote -v

How to undo the last commit but keeping the changes
git reset HEAD~1

How to remove the last commit from the remote
git reset --hard HEAD~1
git push --force (careful with the –force if you’re working with somebody else)

While working on a feature branch

“Ideally, you do git pull –rebase or git rebase upstream/master instead of merges while working on a feature branch so that you don’t get merges.”

Discard local changes and replace with what’s on the remote repository for current branch.
git fetch origin
git reset --hard origin/myBranchHere

Rebase+Squash all changes of your feature branch (since you started that branch out of master.)

Standing on the branch…

git rebase -i master (the -i stands for interactive rebase)

All commits will shown.

Switch to Pull Request branch
git fetch origin pull/ID/head:BRANCHNAME
$ git checkout BRANCHNAME

Checking out a branch from someone’s fork of your project to test/fix pull request.
Say user ‘alice’ is submitting a pull request from a branch called ‘wonderland-feature’
You want to test that branch locally, this is how you get it.
#first add a remote for that repo.
git remote add alice https://github.com/alice/our_project
#download her branch
git fetch alice wonderland-feature
#create a local branch with her remote wonderland-feature branch
git checkout -b wonderland-feature alice/wonderland-feature

Now git log should show the same commits as on her repo.
Commit to the branch if you want to fix something for alice, and then push to your
fork (by convention your fork is called origin) of the repo, then send her a pull request
from your fork.
# uploads the branch to your fork, from there send her pull request.
git push origin wonderland-feature

Once she has merged your changes, if you feel her branch is good now, you can now merge hell pull request on the upstream repository.

[Poll/Bet] What version control system do you use?

About Version Control

Source: wikipedia.org

Revision Control (also known as version control (system) (VCS), source control or (source) code management (SCM)) is the management of multiple revisions of the same unit of information. It is most commonly used in engineering and software development to manage ongoing development of digital documents like application source code, art resources such as blueprints or electronic models, and other critical information that may be worked on by a team of people. Changes to these documents are usually identified by incrementing an associated number or letter code, termed the “revision number”, “revision level”, or simply “revision” and associated historically with the person making the change. A simple form of revision control, for example, has the initial issue of a drawing assigned the revision number “1”. When the first change is made, the revision number is incremented to “2” and so on.

Some Version Control systems
I’ve only mentioned the most popular ones I’ve seen during my career, but I know there’s tons of commercial revision control systems that are supposed to be great, however, these ones do the job, are free, and lots of developers know how to use them.

My favorite one is subversion (having used CVS) in the past.

Here are links to all these projects: