Banner: ZumGuy Publications and Network

ZumGuy Publications and Network

Setting up Git for the first time

Posted by Sean on Friday, 22nd September 2017 15:54
Git is a very useful version management tool. It allows you to work collaboratively with any number of developers, as well as keeping backups of every version of your project.

Installation and creation of a project


Intallation on Ubuntu is very simple, just pressCtrl+Alt+Tto open a terminal window and runsudo apt install git.

Next, create a folder for your new project by runningmkdir MyProject, and enter it withcd MyProject. Now you must initialize this folder as a git repository, by tunninggit init. This will create a hidden folder called.gitinside your project folder, which you can see withls -a(the-aparameter will show hidden folders).

Next, we want to add a file to our project. Let's add a README file.touch README.mdwill create an empty file calledREADME.md, andnano README.mdwill open that file with the nano command-line editor (you could use any text editor, for instance vi or gedit). Once you have added some text to your README file, pressCtrl+Xfollowed byyandEnterto save the changes and return to the command line (in nano).

Now that we have a new file, rungit status:
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.md

nothing added to commit but untracked files present (use "git add" to track)
Our fileREADME.mdis currently "untracked". This means there have been changes to this file since the last "commit" in our repository. A "commit" is essentially a snapshot of the project that git has stored in its repository. To add files to the next commit, we will use the commandgit add. This will add the files to the so-called "staging area". Runninggit add README.mdwill add only that file, runninggit add .will add all untracked files. Runninggit statusagain will show us the changes we made:
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   README.md
Runninggit commitwill now create a new commit with all the staged files added to the repository. If you rungit logyou will see the commits in your repository.


Accessing a remote repository via SSH


If you have created a repository on a remote hosting service (for instance GitHub, BitBucket or ETH GitLab), you will want to be able to work on it on your computer. Git allows you to download (or "clone") the repository ot your computer, make new commits and push and pull them from the server.

First off, we want to generate an SSH public/private key pair (unless you already have one). In order to do this, run the commandssh-keygen -t rsa -C "your.email@example.com" -b 4096. Then you will be prompted to specify a path to save the keys to. Simply hittingEnterhere will save them in the default path and overwrite any existing keys. Next, you will be asked to enter a password/passphrase to secure your keys. This is considered good practice, but not required and can be skipped by pressingEnter. Now that we have the keys, we want to copy the public key to our clipboard, so you can paste it into your website of choice (GitHub, ETH GitLab etc.). We can do this with the programxclip(which you may need to install first withsudo apt install xclip):xclip -sel clip < ~/.ssh/id_rsa.pub(~/.ssh/id_rsa.pubis the default path for Ubuntu, you may need to change it).

Important: your private key is stored at~/.ssh/id_rsa. NEVER copy this anywhere or share it with anyone! If anyone should get their hands on your private key, they would be able to assume your identity. Only the public key should be copied, shared, sent over the internet etc.

Once you have your SSH public key copied, you can paste it into the appropriate filed on GitHub (there is usually an "SSH keys" sections in the settings).

Finally we want to clone the repository from the remote source to your computer. You will need to get the SSH address from your online repository. This looks something likegit@gitlab.ethz.ch:user/my-repo.git. Then you can clone the repository withgit clone git@gitlab.ethz.ch:user/my-repo.git.

Now we can work on our project and commit as usual. Every now and then you will want to sync your repository with the commandsgit push(upload your changes to remote) andgit pull(download changes from remote).

You must be logged in to post messages.

Quote of the day...


ZumGuy Internet Promotions