Most of the time, when you start working with Git, you find it confusing, and the first question you ask is “How do I push commits using the command line?”
Leaving the motivational lines and philosophy aside, let’s start working on it and finish things in five minutes.
But, first of all, this post is for Linux users, as it’s mentioned already. Second, I hope you’ve Git installed on your machine and if it’s not, then just open your terminal and do a quick –
$ sudo apt-get install git
Now, since you’ve Git installed. So, it’s time to finish the job.
STEP 1: Setting up Git
Open your terminal ( I know it’s already opened ) and type –
$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR EMAIL ADDRESS"
STEP 2: Authenticating with GitHub from Git
Let’s be little fast and set things up using SSH instead of using HTTPS stuff, okay?
Follow (copy paste) the below steps:
$ ssh-keygen -t rsa -b 4096 -C "id@example.com"
After hitting enter, you’ll get the following lines:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Do nothing. Just hit enter to proceed to the next step where you’ll be asked to enter a passphrase, twice.
Type your passphrase, for example: “gitubuntu1337” or whatever you want and hit enter. Type the same passphrase again and hit enter to proceed to the next step.
STEP 3: Adding your SSH key to the ssh-agent
- Start the ssh-agent in the background.
$ eval "$(ssh-agent -s)" Agent pid 29645
- Add your SSH key to the ssh-agent.
$ ssh-add ~/.ssh/id_rsa
STEP 4: Adding a new SSH key to your GitHub account.
- Copy the SSH key to your clipboard
Use xclip to copy your ssh key. But before that, you need to install it because I think Linux doesn’t come with xclip preinstalled. So. just type:
$ sudo apt-get install xclip
And when the downloads are finished, use the following command to copy your ssh key to the clipboard:
$ xclip -sel clip < ~/.ssh/id_rsa.pub
- Go to the Settings
- Go to SSH and PGP Keys and click on New SSH key
- Enter a suitable title in Title’s section and use CTRL + V to paste the ssh key you copied using xclip command before a few minutes ago in Key’s section and click on “Add SSH Key” to add the ssh key in your GitHub account.
Now, you’re ready to go.
To test whether you are being able to push commits using Git and you’re connected via SSH or not, follow the given steps:
- Create a repository and clone it via “Use SSH” link.
For example: $ git clone git@github.com:username/repository.git
- Now, cd into your project folder and add a file or make changes to the existing file.
So, it’s time to push the commits and for that, you’ve to do:
$ git add filename
$ git commit -m " your commit message"
$ git push -u origin master
Done!
So, finally, you know how to set up Git and work with it. This is a simple example that teaches you the basics so that you can work on the small repositories. But Git is really huge and so is its usage, so just keep exploring and try learning different commands.
Hope it helps!