How to squash and rebase in git

Leave a comment

How to create a pull request against branch on master

1 Comment

Say someone has created a branch for you off of master, and they want to create Pull Requests (PRs) against it. Here’s how you do it.

Get on your fork (make sure sync’d and clean) and then do

git fetch remote branch
git checkout branch

So for example

git fetch upstream imessage
git checkout imessage

Screen Shot 2017-03-20 at 2.56.48 PM.png

This grabs the upstream branch, pulls it local, and then when you checkout a branch of the same name, it automatically sets it to the upstream branch you just checked out.

Now you can create a new branch off this one, do your work, and merge it back by creating a PR via GHE.

git checkout -b foo`

You are now ready to work

Screen Shot 2017-03-20 at 2.58.33 PM.png


Permission denied (publickey)

Leave a comment

If you suddenly see this it’s because your the keys you use for ssh are either gone or not know to ssh. If you have them, but they just aren’t registered:

$ ssh-add -l (The agent has no identities.)
$ cd ~/.ssh (to see your keys)
$ ssh-add ~/.ssh/id_rsa (or whatever your key is called)

Also, make sure macOS (10.12+) uses your SSH key’s passphrase from the Keychain automatically

$ chmod 600 ~/.ssh/config # temporarily grant write access to the config
$ echo "Host *\n  UseKeychain yes\n  AddKeysToAgent yes" >> ~/.ssh/config
$ chmod 400 ~/.ssh/config

How to git rebase and squash commits

Leave a comment

Find the SHA of the commit you want squash to

Screen Shot 2017-03-02 at 9.23.34 AM.png

If we want to squash all our work down to Story X, we need to go one beyond and grab the SHA from the one after the one we want to squash down to.

Screen Shot 2017-03-02 at 9.24.44 AM.png

Cntl-v enter visual mode
Select text
c – to delete
Type what you want to appear (s)
Press ‘Esc’ x2
Add a ‘p’ to top commit (reverse order)
Save (:wq)

Screen Shot 2017-03-02 at 9.28.19 AM.png

Screen Shot 2017-03-02 at 9.29.07 AM.png

Screen Shot 2017-03-02 at 9.29.27 AM.png

If you have a conflict, fix the conflict then…

git add .
git rebase --continue

If you make a mistake

git rebase --abort

If you want to change a commit message

git commit --amend

If you want to add another author to a commit

git commit --amend --author "peter+paul " -C HEAD
git commit --amend
git push -f

Squash specific number of commits together

Screen Shot 2017-04-10 at 9.30.54 AM.png

If you want to squash say x3 commits together to this

git rebase --interactive HEAD~3

And that will produce this

Screen Shot 2017-04-10 at 9.34.49 AM.png

So when you go ~3 it squashes three down into one. But doesn’t affect the one before it.

Rebase master

Then when ready to run master onto of your changes on your local branch.

git rebase master

How to add a git remote to a heroku app


There are two concepts at play when adding a user friendly heroku name to a git repos.

There is the remote (git) and the app (heroku).

git remote

Git remotes are just aliases for git repositories. You can see what alias are currently setup for your git repos by typing:

> git remote -v

heroku (fetch)
heroku (push)
origin (fetch)
origin (push)

origin and heroku are the remotes – and their corresponding git repos.

origin is my git hub repos.
heroku is my remote name for my heroku git repos.

When I type:

> git push heroku

It pushes my code to the heroku git repos using the ‘heroku’ git remote name.

heroku apps

When you create an instance of your code base on Heroku, it gives your app a name. You can see your app names by typing:

> heroku apps

=== My Apps

And you can get more info (like it’s git url) by typing:

> heroku info -a secret-waters-xxxx

=== secret-waters-7537
Addons: heroku-postgresql:dev
Git URL:
Owner Email:
Region: us
Repo Size: 7M
Slug Size: 33M
Stack: cedar
Tier: Legacy
Web URL:

What we want to do is tie these things together.

Tying a remote to an app

Say I created a staging environment, and I want to replace the heroku generated name it gave me (like secret-water) with a nicer name like ‘staging’. Here is how I would do it.

> git remote add staging

Now when I list my apps I should see staging there:

> git remote -v

staging (fetch)
staging (push)

And I should be able to push to my staging environment with this:

> git push staging

If you make a mistake you can remove a remote with

> git remote rm staging

For more examples of these and other commands feel free to checkout my heroku cheat sheet:

How to change heroku git remote


If case you ever need to map your git remote back to an existing url do this:

> git remote -v

heroku (fetch)
heroku (push)
origin (fetch)
origin (push)

Right now heroku is pointed at protected-reef. But I want it to point to:

To do that is use this command:

> git remote add heroku

> git remote -v
heroku (fetch)
heroku (push)
origin (fetch)
origin (push)


Update: If you don’t have your heroku remote created, do this:

> heroku create
> git remote set-url heroku

How to revert to an earlier version in git

1 Comment

$ git log
$ git git reset --hard SHA1_HASH
$ git push --force

Links that help:

Git workflow


I’ve just started working with a team on git and I wanted a visual of what was going on every time I made a change and needed to checkin some code.

Abandon branch
$ git branch -D topic-branch

How to create a remote branch and have your friend get it

Here’s a graphic showing one way of creating a remote branch, checking it in locally, pushing it to the remote server, and then having your friend pick it up.

Pull requests

Here Mary developers a new feature locally, then pushes it to here remote branch for others to see.
She then issues a pull request allowing others to view.


Simple bash script to reset your git repository

1 Comment

Here’s a simple script I use whenever I screw up (often) and need to reset my git local git repository to a clean slate.

echo "Reseting git repository"
git reset --hard HEAD^
git clean -f
git merge origin/master 

Simple bash script for checking into git

Leave a comment

Having gotten tired of typing the same commands over and over again I created a simple script to conveniently check into my git repositories.

echo "Checking in..."

git add .

if [ -z "$1" ]
 git commit -a -m "cleanup"
 git commit -a -m "$1"

git push
echo "Done!"

Older Entries

%d bloggers like this: