Useful SSH commands for working with public/private keys

Below are a list of some useful commands for working with public/private keys on a server over ssh. All of these were pulled from the bitbucket documentation on setting up SSH for Git here.

Test what version of SSH you are running, if any:

$ ssh -v

List the contents of your ~/.ssh directory:

$ ls -a ~/.ssh

Generate a public/private key:

$ ssh-keygen

Test if an ssh-agent is running (for you to load your newly generated keys to):

$ ps -e | grep [s]sh-agent

If an agent is not running then start it manually:

$ ssh-agent /bin/bash

Load a new identity into the ssh-agent management program using the ssh-add command:

$ ssh-add ~/.ssh/id_rsa

List the keys that the agent is managing:

$ ssh-add -l

Show a key so you can copy and paste it on a LINUX server:

$ cat ~/.ssh/

Copy and paste a key on MAC OS:

$ pbcopy < ~/.ssh/

Reducing the size of a Git repository by removing a file or folder from its history

I had a Wordpress site what was being tracked with Git but I had neglected to correctly manage my .gitignore file to ignore the uploads folder that holds all uploaded images (this resulted in a large repository).

I rectified this by removing the folder and adding it my .gitignore file and cleared the cached files that where previously being tracked.

However this does not remove the files or folders from Git’s history, meaning my repository size was still very large.

To clear the history I had to complete the following steps:

Continue reading Reducing the size of a Git repository by removing a file or folder from its history

Remove already tracked files with .gitignore

If you have already added a file to be tracked but now want it not to be you will need to add it to a .gitignore file in the root of your git repository then remove them from being tracked:

git rm --cached <file>

If you need to remove a whole folder and don’t want to list all the individual files you can use:

git rm -r --cached .

Then re add all files:

git add .

and commit your changes:

git commit -m "Added /folder to .gitignore"


Rename a local and remote branch in git

If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.

1. Rename your local branch.
If you are on the branch you want to rename:

git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:

git push origin -u new-name