TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
CI/CD

Take Your First Steps with Git

Using git might seem like a challenging proposal at first. The good news is that git isn't all that challenging to use, once you know the steps.
Sep 9th, 2024 11:00am by
Featued image for: Take Your First Steps with Git

Developers almost always wind up having to use git to manage their code. Of course, before you actually get into using git, you need to first understand what it is and how it works.

For that, give “Need To Know Git? Start Here” a read. Once you understand what this revision control system is, your next curiosity might be, “How do I use it?”

That’s why you’re here.

Using git might seem like a challenging proposal at first, and there is some accuracy to that proposition because git requires a very specific workflow. If you don’t know or follow the correct steps, you’ll wind up fairly frustrated.

The good news is that git isn’t all that challenging to use once you know the steps.

Let’s dig in and see how git works, so you can take your first steps toward much simpler collaboration with a development team.

First things first.

Installing Git

Not every operating system ships with git. I’ll demonstrate the installation of git on Pop!_OS, which is based on Ubuntu. The installation process is similar on most Linux distributions, the difference being which package manager is used (such as dnf instead of apt for Fedora-based distributions). Once the installation is taken care of, the use of git is the same, regardless of the operating system.

With that in mind, all you’ll need is a running instance of a Linux distribution and a user with sudo privileges.

Let’s install.

First, log in to your Linux operating system and open a terminal window. If you’re using a Ubuntu-based distribution, the installation command is:

sudo apt-get install git -y

If you’re using a Fedora-based distribution, the command is:

sudo dnf install git -y

If you use an Arch-based Linux distribution, the installation command is:

sudo pacman -S git

If openSUSE is your jam, the installation is done with:

sudo zypper in git-core

No matter the distribution, to verify the installation, issue the command:

git --version

You should see something like:

git version 2.34.1

Congrats, Git is installed.

Creating a Local Repository

After the installation is completed, you’ll want to create a repository for your project. Let’s say you want to name your repository “myproject.” Create a directory for this with the command:

mkdir ~/myproject

Change into that new directory with the command:

cd ~/myproject

Next, you must initialize the directory as a repository with the command:

git init

You should see something like the following at the end of the output:

Initialized empty git repository in /home/jack/myproject/.git/

Adding files to the New Repository

You can now add all the files you need for your project (such as all of your code files). The process isn’t just a matter of dumping the files into the new directory. Let’s create a README file. For that, issue the command:

nano README

You can add whatever text you want to the file.

At this point, git will have noticed the file but doesn’t do anything with it. Verify this with:

git status

You should see the README file listed but git will inform you that it’s not being tracked because no commits have been made. Before we issue our commit, let’s add the file so Git tracks it with the command:

git add README

If you’ve added multiple files to the repository, you can add them all at once with:

git add .

Your First Commit

Now it gets exciting. You’ve added a file such that git can track it. Next, you’ll need to submit your first commit. A commit creates a unique ID for all changes, so git can track them.

To make a commit, you need to add text that informs anyone using the repository what you’ve done. For instance, above we added a README file, so our commit might inform other developers that’s what you’ve done. However, before you submit your first commit, git needs to know two bits of information about you, specifically your name and email address. To do this, you’d issue the following two commands:

git config --global user.email "EMAIL"
git config --global user.name "NAME"

Where EMAIL is your email address and NAME is your name.

After taking care of that, you can then add your first commit with a command like this:

git commit -m "Added README file"

The output should read something like this:

[master (root-commit) a083315] Added README file
 1 file changed, 1 insertion(+)
 create mode 100644 README

Creating a Branch

Let’s say you’ve added all your code to the project and everything is going smoothly. You might come to a point when you want to add a new feature to the project but don’t want to chance mucking up the base code that’s already working. For that, you could create a branch, which is a sort of fork of your project that allows you to work on the new version without changing the original code.

Let’s say you want to create a new branch named V2. For that, issue the command:

git checkout -b V2

Git will automatically switch you to the new branch, so any changes you make will only apply to that version. If you issue the command git branch, you should see both V2 and master listed. You can change back to the master with:

git checkout -b master

Remember, the basic workflow for git looks like this:

  • Create and initialize your repository (git init)
  • Add files to the repository.
  • Stage your files (git add)
  • Issue commits (git commit)
  • Create branches (git checkout)
  • Work on files within the new branches.

And that’s the basics of working with git. This only makes use of a local repository, which is accessible to you. Next time around, we’ll talk about creating a repository that other developers can access, so you can see how collaborative git can be.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.