RECENT NEWS
📢 𝟑𝟎% Discount for all ads only this month ❄️

Git: What it is, how to use it, and why it will improve your server

Crow
power_settings_new
Seen 4 months ago
Rune Warrior (34/40)
Rune Warrior
0
0
0
34 Posts
Posts
0
Warning level
0
Likes
0
Dislikes
Joined: 2022-04-24

This means:

  • It's fast (duh).
  • It's free.
  • It tracks your code changes.
  • It's easy to rollback if you find a bug.
  • No accidentally overwriting work your friend did.

Related - The Benefits of Open Source:

  • Other people can help and chip in. Free code (and help) for nothing!
  • Bug reporting and fixing becomes a lot easier.
  • Improve your community - people team up to help, work together, and fix problems.
  • Lets people see you are updating and working on it all the time.

There is no good reason not to use a proper version control system for your code. Just because it has a long name doesn't make you incapable of using it. It is incredibly simple if you spend literally 5-10 minutes to understand the basics.

PSA: Git is not GitHub

GitHub is a hosting service for git repositories (code projects). You can use GitHub with git. They are not the same.


This is Octocat, the lovable GitHub mascot.


This is git, with a less impressive logo.

Installing Git:

Go to the git downloads page here. Download the release appropriate for your operating system. Install it.

Creating a GitHub (or other) account:
GitHub offers free project hosting for open source projects and I highly recommend it. BitBucket offers free project hosting for both open and closed source projects, and is another good one. This guide will use GitHub because I want you to open source your code. If you'd rather use BitBucket, the gist is the same as this.

1. Go to GitHub. Pick a username and password, and supply a working email. Tuh-dah.

Setting your username and email:

You set this so git knows who made the commit.

  1. Make a folder to keep your project code in (or use an existing folder).
  2. Open Git Bash in this directory (can right click in explorer.exe and select Git Bash if you're on windows).
  3. Type the following:

 

Code:
git config --global user.name "Your Name Here"
Code:
git config --global user.email "your_email@example.com"

If you want to use a different username or email for each project, drop the "--global".

Letting git use your local folder (the code on your computer):

  1. Go to the folder.
  2. Open Git Bash.
  3. Type "git init".

Explanation of terminology:

  • Upstream Repository: The version of your project that's on the internet (i.e. on GitHub, BitBucket, etc)
  • Local Repository: The version of your project that's on your computer.
  • Commit: Telling your local repository that you've updated a file (or files), and to use the updated one instead.
  • Index: Where git stores local commits that haven't been sent to the upstream repository.
  • Push: Updating the code in the upstream repository with (updated) code from your local repository.

Ignoring unnecessary files:

There are some files that you don't want to push to git. This can because they contain secure information (like your SQL database user/password), or because they're unnecessary for other people (like your compiled files - .class etc). Luckily, git makes it easy to auto-ignore these - using a file called .gitignore. You add every path (i.e. folder), file, or file type you don't want git to upload, and it'll ignore those by default (you can override this when committing if you wish).

Here's one I prepared earlier:

Code:
!.gitignore
.*
*~
/target
*.iml
/lib
/bin

Generally it's bad to upload any settings files (like .project that eclipse generates), any libraries (such as netty), or any compiled files (.class etc). Also note that if you have a class that contains SQL user/database information (you should read it from a file at startup instead) you can't just leave that class out - you'll have to do what i suggest (hint) or edit the file every time you commit it (which is going to get annoying very quickly).

Linking your local repository with an upstream repository:
(Remember, this means linking the code on your computer to the code on GitHub)

  1. Go to the new repository page on GitHub (here).
  2. Name and describe it
  3. Hit 'Create Repository' (you should've already run "git init", so don't check that box).
  4. Copy the HTTP URL from the next page (i.e. from this box).
  5. Now open Git Bash in your local repository (the folder you used before) and type:

 

Code:
git remote add origin PASTE_YOUR_URL_HERE

And then:

Code:
touch README
git add README
git commit -m "First commit"
git push -u origin master

Your code is now on GitHub. Nice one!

Committing updated files:
So you've written new code. Now what? Luckily, this can be dealt with in about 5 seconds.

  • Open Git Bash in your local repository.
  • Type the following (you can use *.java to add every java file - it'll only include the ones that you've changed).

 

Code:
git add FILE_NAME_HERE
  • Type the following - the commit message requires " " and you should be speaking to git like you're ordering it around (e.g. "Add file x", "Fix bug something", etc).

 

Code:
git commit -m "COMMIT MESSAGE HERE"

Updating upstream repositories:
Made a commit? Great, now you can update the upstream repository (e.g. your GitHub repository) with one simple command:

Code:
git push

Your repository host (e.g. GitHub, BitBucket) will ask for your username and password so that random people can't update your code, and you're done!

Useful guides and links:
NDP Software Git Cheatsheet.
Salesforce Cheatsheet.
GitHub help.

00
  • Like
Reactions: