Git: Difference between revisions

From Redbrick Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Git is a version control system, like Mercurial with slightly more confusing commands, or like Subversion on steroids. It's a distributed VCS (DVCS), which you can google to find out more about, but if you've only ever used subversion and CVS, you'll find it a little different (hopefully more pleasant) to work with then you're used to.
Git is a distributed version control system, like Mercurial.


At some stage, someone might put up a full tutorial showing how to use git, but for now, here's how to get and use a repository on redbrick's git hosting area.
If you are looking for a guide on how to host your git repo on Redbrick, click [[Hosting_a_Git_Repo_on_Redbrick|here]]!


== Getting a git repository on Redbrick ==
For this guide we will be using the Git CLI.
Before you can use redbrick's git repositories from one of your computers (or from your redbrick shell), you need to create a public/private SSH keypair there if you haven't already. You'll need to do this for every device/place that you want to access git from. This might sound annoying, but it also lets you use git without having to type a password every time you need to clone/push/pull.


To do this on a Unixey (Linux/Mac OS X/on redbrick) system, run:
== What is Git? ==


  ssh-keygen -t rsa
Git is a version control system, used by almost every developer. It allows you to see changes you've made to your code and roll back the project or specific files to previous versions. It is extremely useful for collaborating with several developers via a remote repository and for finding out what happened when a change breaks your project.


== How does it work? ==


<pre>
Git takes "snapshots" of your project whenever a change was made. To save on space, if a file was not changed it will link to the original file instead of making a copy. Think of it as copy and pasting a directory and making a change on the new one, but all of the unchanged files in the new copy are just shortcuts to the files in the old one.
[you@your-computer ~]$ ssh-keygen -t rsa
 
Generating public/private rsa key pair.
There are three main states in a git repository:
Enter file in which to save the key (/Users/you/.ssh/id_rsa):
* The Working Directory. This the code you are working on, and you can make changes as you like!
Enter passphrase (empty for no passphrase):  
* The Staging Area. These are the files that you plan for git to save a snapshot of.
Enter same passphrase again:
* The Git Repository (repo). This is the ".git" directory made when you initialize a new repo. It tracks and saves the snapshots of your files.
Your identification has been saved in /Users/you/.ssh/id_rsa.
 
Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
In practice, it looks like this:
The key fingerprint is:
* You work on your code and make changes
58:06:86:0c:83:5e:4e:ab:75:93:96:37:76:42:73:07 you@your-computer.local
* You "stage" the files you've changed
The key's randomart image is:
* You then "commit" these staged files, adding them to the repository.
+--[ RSA 2048]----+
 
| .oo .o  E.     |
== Creating Your First Repository ==
|.  +o. + . .    |
 
|. + . + = .      |
Say you have a project that you want to backup with git in ~/my/project. First we need to navigate to the project with the cd command:
| . + * O .       |
    cd ~/my/project
|  o o = S        |
 
| .              |
Then we create the repository:
|                |
    git init
|                |
 
|                |
We can then stage the files. The * here means all of the files in the directory:
+-----------------+
    git add *
[you@your-computer ~]$
 
</pre>
Finally, we can commit the changes:
    git commit -m "initial commit"
 
If you have sensitive files you don't want to be added to the repository, such as .env files, you can use a '''.gitignore''' file at the project root.
    cd ~/my/project
    touch .gitignore
 
Any files or directories specified in the gitignore will not be added to your repository. For example:
     *.env # ignores any files with the extension .env
    hello.* # ignores any files starting with hello.
    /foo # ignores the entire directory foo
 
You can also specifically allow files or directories inside of ignored directories using an !:
    !/foo/bar # specifically allows the file bar in the foo directory, even though foo is in the gitignore file

Latest revision as of 21:42, 11 April 2026

Git is a distributed version control system, like Mercurial.

If you are looking for a guide on how to host your git repo on Redbrick, click here!

For this guide we will be using the Git CLI.

What is Git?

Git is a version control system, used by almost every developer. It allows you to see changes you've made to your code and roll back the project or specific files to previous versions. It is extremely useful for collaborating with several developers via a remote repository and for finding out what happened when a change breaks your project.

How does it work?

Git takes "snapshots" of your project whenever a change was made. To save on space, if a file was not changed it will link to the original file instead of making a copy. Think of it as copy and pasting a directory and making a change on the new one, but all of the unchanged files in the new copy are just shortcuts to the files in the old one.

There are three main states in a git repository:

  • The Working Directory. This the code you are working on, and you can make changes as you like!
  • The Staging Area. These are the files that you plan for git to save a snapshot of.
  • The Git Repository (repo). This is the ".git" directory made when you initialize a new repo. It tracks and saves the snapshots of your files.

In practice, it looks like this:

  • You work on your code and make changes
  • You "stage" the files you've changed
  • You then "commit" these staged files, adding them to the repository.

Creating Your First Repository

Say you have a project that you want to backup with git in ~/my/project. First we need to navigate to the project with the cd command:

   cd ~/my/project

Then we create the repository:

   git init

We can then stage the files. The * here means all of the files in the directory:

   git add *

Finally, we can commit the changes:

   git commit -m "initial commit"

If you have sensitive files you don't want to be added to the repository, such as .env files, you can use a .gitignore file at the project root.

   cd ~/my/project
   touch .gitignore

Any files or directories specified in the gitignore will not be added to your repository. For example:

   *.env # ignores any files with the extension .env
   hello.* # ignores any files starting with hello.
   /foo # ignores the entire directory foo

You can also specifically allow files or directories inside of ignored directories using an !:

   !/foo/bar # specifically allows the file bar in the foo directory, even though foo is in the gitignore file