Using git - tutorial

NOTICE: june (webcatz.neocities.org) has created a guide about this topic, which in my opinion is more readable and informative as this one, so I recommend you to give it a try instead:
https://webcatz.neocities.org/how-to/deploy-your-neocities-with-github

disclaimer: this is my first ever tutorial for anything, expect errors and overcomplication, I would love to hear feedback about it though!

What is git?

Designed mainly for code, git is a program that allows you to keep track of changes in your project. The main idea is that every time you're done making changes to your code, you commit them, which adds your changes to the archive, and then you can (optionally) push it online to one of many git hosting websites (like Gitlab or Github).

Why use git?

There's quite a few advantages of using git:

Installing git

To install git, head over to https://git-scm.com/downloads and find the appropriate download for your OS.

When installing on Windows though, theres a couple of options you might want to modify to your liking:

Default editor used by Git

The default option for that is Vim, which is fairly complicated if you haven't used it before, so I'd advise to change it.

It's very likely that the editor you're using is available as an option, so just use that. If not, i recommend Nano (which comes with git) or Notepad, as they're the smallest and simplest ones to use

Adjusting the name of the initial branch in new repositories

The default branch name for git is master, which is, let's say... kinda weird. That's why it's subject to change in the future. Most of the git hosting websites already use main as the branch name, so I recommend selecting Override... and setting main as the default branch name.

For the rest of the options, you can just leave them at their defaults

The user interface

Git by default only comes with Git Bash as it's default user interface, which means that you need to type in commands in its commmand prompt to use it.

If you aren't used to terminal commands, I recommend installing a graphical user interface for Git. I won't be covering them here, because this tutorial would be twice as long, but you are free to explore on your own.

Some good gui apps for git that I know of:

Using Git Bash

After installing git, go to the folder you keep your website at.

Right clicking on the empty space, you should see an option Open Git Bash here or something similar. Bash is a command line for Linux, but you basically need like two things to use it

Working with Git

Creating a repository

Creating a repository is simple, just go to the folder with your website, open it with git bash, and then run:

git init

And thats it! Your git repository has been created, and theres now a hidden .git folder containing info about it

Cloning a repository

In case you already created your repository, let's say on a git hosting service such as GitHub, you can download it easily with:

git clone <url-to-your-repository>

Example:

git clone https://www.gitlab.com/scarecat/website

That should download the code for my website, and if it doesn't then i probably forgot to make it public :p

Staging files

From this point, any change to the files you make in git, will be noticed by git. You can check what files are changed with:

git status

If you make any changes, it will display Unstaged or Untracked files.

All changes you make have to be packed into one "commit", which contains your changes, along with the date, the author and a message about the changes.

To do that, first you have to mark the files as staged, which basically means 'ready for a commit'. To do that, simply type:

git add <path-to-file-or-folder>

For example: git add guides to stage the 'guides' folder, or git add guides/using-git.html to stage the file 'using-git.html'

Cool tip: you can use git add . to add your whole directory (which essentially adds every changed file)

Another cool tip: you can use git add -p to have git ask you about every change

Now we can move on to-

Committing

After staging your files, all that's left to do is to write:

git commit

To create the commit. But hold on a minute, because the first time you do it, you will probably be met with a message from git asking you who you are. That's because, as I mentioned before, along with your changes you have to add both the author and a message.

To add your name to the commits, you have to do:

git config --global user.email "youremail@youremail.com"

git config --global user.name "your name"

If you don't put the --global the name will apply only for the repository you're in.

Keep in mind the email and user name don't have to be real, it's just to denote who you are

With that done, you should be able to run git commit again.

A window containing your changes will open in the editor you selected during the installation. All that's left to do now is write a short message about your changes, save and quit the editor.

You just made your first commit, and that pretty much covers the basics of adding your changes to git. You can view a list of your changes with git log.

Creating a gitignore file

If you don't want files to be included in your commits, you can create a file named .gitignore containing names of files you dont want to keep track of.

Uploading / Downloading changes

To do that, you need an online repository. You can make an account on Github (most popular), Gitlab, BitBucket or others and create a repository there. Then it's just a matter of cloning it using git clone, as mentioned above. It will create new folder with your repository (depending where you opened git bash).

After you're done adding a commit to it (or multiple commits), it's time to "push" them online.

To avoid conflicts, its always good to update your repository before sending in new things, and for that all you need to do is git pull

It will download all commits that were online but not on your computer.

After you're sure your repository is up-to-date, you can use

git push <url> <branch-name>

With the url being the repository link, and the branch name is the branch you want to update

We can simplify it though:

So with your simplifications, we are left with

git push -u origin main the first time we do it

and then just git push

And with that, the changes are sent online to your account.

Conclusion

There are the very basics of using git to manage your code. As you get used to it, you can see how tremendously useful it is for keeping changes, collaboration, and making sure nothing is ever lost

If you need more information, you can consult the Git manual.

That's it for this guide, it's taking wayyy too long. See ya!