User:Cyrusty/Git

From Organic Design wiki
< User:Cyrusty
Revision as of 07:36, 22 March 2019 by Cyrusty (talk | contribs) (Lesson #2: Managing files with GitHub)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Lesson #1: Setting up a GitHub account

I have now created an account on GitHub which a web application allowing people and teams to work on their code and record all the changes made to it. My GitHub profile is at github.com/Cyrusty. I then created a repository for the code that will make up my site, my new repository is at github.com/Cyrusty/cy.rusty.space.

I also added in my public RSA key by going into the shell on my local computer and printing the contents out with the following command:

cat .ssh/id_rsa.pub

This printed out my public key which is shown below. It's no problem for me to show it here because it's public, people who have my public key can use it to encrypt or lock things so that only I can access them.

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6gxSQCs9A5645IhmIJMnE/WxLEgqDTASj
/YloaV0fI4/b3S3APQ/UqkejZxtH7ctJrRKxD3gQ3HwXZPzmiCLOUw2Pji1BOIP/5gSB1cpD4ewK3
/3+pIrkqo3zL5u8eE6oVjdkk8GZJTF05E4LSy0vfr0D2eCsKdXMmCoz+eA83Cbnv6Pkbn4RW1SLUEhl
N8DQRYMpzJML1P8WfiZabEDxvLRejClFZpN+J74Znkov6lg47cqLZCSYZOmopcq9abIq1EeJnG3Kw0
/JDb8QyL9NFdoEa6MbfPbouTpQ2If3ZIE238SGR2FB4qxBFxKY0Tz4hFlr+3tBqnQPKLWDmWnh cyrusty@cyrusty

I could then select it, copy it and paste it into the text-box in GitHub's page for add a new key.

Lesson #2: Managing files with GitHub

I learned to edit my repository files locally by accessing it through the shell with git.

I had to install Git first using the apt-get command:

sudo apt-get install git

I made a local copy of my repo by going to the repo's page in Github at github.com/Cyrusty/cy.rusty.space and then getting the SSH address of the repo from the big green clone or download button. Then I used the address in a git clone command to make a copy in my /var/www directory (which I had to change the permissions of first).

cd /var/www
chmod 777 /var/www
git clone git@github.com:Cyrusty/cy.rusty.space.git

I then went into the cy.rusty.space directory which is the copy of the repo it made.

cd cy.rusty.space
ls -al

I then used the nano shell editor to edit the README.md file

After making any changes I would commit the changes using the git commit as follows:

git commit -am "Describe whatever changes I made etc"

Note that you generally do a commit for each different task after you've got it working properly or looking right etc.

Committing changes doesn't need the server, it's all on my local copy only, after I've made a bunch of commits, I can then push then to the main repo on the server. I can make changes locally without the need of the internet but to do the git push I need access to the internet.

git push

Aran made some more changes to the main repo which I could pull down to my local copy as follows:

git pull

I need to git pull before making edits because the repo may have changed since it was worked on last. Generally the idea is that when you're about to do some work on a project, you do the following work-flow:

  1. git pull to make sure you're all up to date
  2. Do some changes to the files and git commit them
  3. Do some more changes to the files and git commit them
  4. git push to update the repo

Note that you might want to pull and push more often if you know that other people are also working on the project at the same time. You'll be notified by email when things change in repos you have commit access to.

We then went to the repo at github.com/Cyrusty/cy.rusty.space and saw that the changes we'd made had been pushed properly to GitHub's copy of the repo. By clicking on commits I can see all the history of changes that have been made, and then I can click on one of the ID links to the right of the commits, such as 4616415, to see exactly what was changed in any of the commits.


To reset a git file back to a previous time if your not happy with the changes someone or yourself have made then I can do this by typing in

git reset --hard [ID of file]


An important thing to understand is that git is peer-to-peer, your copy of the repo, and my copy and even the copy at Github are all identical, if GitHub suddenly disappeared you haven't lost anything because all the code and history of changes are in your local copy of the repo. The only reason we use GitHub is because they have a cool site that makes it easier to work with our files, and because it's a massive community that makes it easy for people to find your project.

Lesson #3: Adding, removing and renaming files in a repo

You might think this should just be a matter of create, removing and renaming the files the usual way, but its not quite as simple as that. The reason is that you have to tell Git which files in the directory you want it to manage and which ones to leave alone.

Imagine the repo is an application you're working on. It needs configuration such as passwords and keys etc which are not things you want to have in the public repo. In general all files that are specific to your local environment only are not useful in the repo.


When Copying a file into a repo folder (e.g. /var/www/cy.rusty.space/...) you need to git add that file.

So for this reason, if you create a new file that you would like to be managed in the repo, you need to tell git:

git add [FILENAME]

If you want to rename of delete files that are in the repo, you must do it via git - its the same command you'd use normally from the shell for these actions, but you now precede them with git. For example, here we rename the foo file to bar and then delete it. And don't forget to commit and push your changes after you do any of these things.

git mv foo bar
git rm bar

To remove a whole repo clone just nuke it with the normal rm shell command to delete a directory, e.g.

rm -rf /var/www/cy.rusty.space