Little Git problems

Git-Logo-2Color

We meet various little problems in everyday life. Most of them we don’t meet frequently to remember how to solve them. Therefore in this post I want to wrote solutions for some of them.

Problem 1. Setting SSH key

In git we can use HTTPS or SSH. One of the benefits of using SSH is that you don’t need to type credentials everytime. At HTTPS we also can save credentials, but we need to download additional software for that.

Step 1. Generate SSH key (MacOS or Linux).

Type in terminal:

ssh-keygen

It will ask the file where you want to save the key. To use standard path (/Users/username/.ssh/id_rsa) just press enter.

If it says that this file already exists, then you already have private key. Public key default path is /Users/username/.ssh/id_rsa.pub. You can use keys that you already have OR overwrite them.

Then it will ask to enter passphrase for key that you need to enter every time you use private key. You can keep it empty and you will not be asked for passphrase.

At the end you should have private key (id_rsa) and public key (id_rsa.pub). Private key should be saved only in your computer, keep this key in secret. Public key will be sent to Git server.

Screen Shot 2017-02-08 at 18.58.47

Step 2. Save public key in Git server.

Lets consider two of the most popular git online services: Bitbucket and GitHub.

Open public key in text editor. I prefer vim:

vim /Users/username/.ssh/id_rsa.pub

Just select all text and copy it.

Add copied key contents into big text form at following path:

Bitbucket: authorize in site -> menu on the top right corner -> Bitbucket settings -> SSH keys -> Add key.

GitHub: authorize in site -> menu on the top right corner -> SSH and GPG keys -> New SSH key.

Set some title for key and save it.

Step 3. Use SSH not HTTPS link.

Everywhere use SSH link, so your private key will be automatically used. If you have repositories in local machine that previously worked by HTTPS, you need to change their remote from HTTPS to SSH.

Go to git repository, see current remote address:

git remote -v

Change remote to SSH by command:

git remote set-url origin git@[bitbucket.org|github.com]:[username]/[repository name].git

When you will use SSH key first time in Git server, you will be asked to save Git server address in known_hosts.

Problem 2. Start working with existing repository

Get SSH link of repository. For example: git@github.com:Doszhan/AlamofireExample.git

Just copy it to local computer by command and start working with this git repository:

git clone git@github.com:Doszhan/AlamofireExample.git

Screen Shot 2017-02-09 at 13.51.56

Problem 3. Upload to Git server existing project

You can write initial code of project and make it Git repository by these commands:

git init
git add .
git commit -m 'initial'
git remote add origin git@github.com:Doszhan/AlamofireExample.git
git push origin master

Problem 4. Removing last commit

If you want to remove last commit, it is better to revert changes manually and make one more commit. This is safe workflow. But if you are risky developer =) :

git reset --hard HEAD~1

This command will remove last commit and revert it’s changes locally. If you uploaded deleted commit to git server and want to delete last commit from server:

git push origin +HEAD

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Profile photo of Doszhan Kalibek

Doszhan Kalibek