DEV Community

Cover image for Git for Beginners
Random Nerd
Random Nerd

Posted on

Git for Beginners

What is Git?

Git is a distributed version control system, which allows for efficient collaboration and merge conflict resolution. Pipelines can be created to streamline the development and production process and supports non-linear workflows through branches and forks.

Having a basic understanding of Git is a must for all developers, whether you are developing as a hobby, or you're sitting in an office reading this article.

Why did you write this?

When I started as a dev, I struggled understanding those over-complicated "beginners" tutorials. This article is written in plain-english.

What we'll be learning

In this article, we'll be covering the basics of git CLI/BASH:

  • git init
  • git remote add
  • git add
  • git branch
  • git commit
  • git push

And two hands-on practical activities to strengthen your understanding of git.

Step 1: git init

I would recommend creating an empty directory to play around with in this tutorial, but if you want, you can use an existing directory.

In this section, we'll be initializing a git repository locally in your selected directory. Open command prompt and cd into your chosen directory, now run the command git init.

That was easy, we now have a local git repository!

Step 2: Adding a Remote Repository

If you wish to work locally and never push to a devops/git provider like github, you can skip this step.

To add a remote repository, we can run git remote add <<name>> <<url>>, where name is what you want to nickname your connection to the remote repository (generally we just call it origin) and url is the url of your remote repository.

Step 3: Adding files

The next thing you need to learn is how to add files to git.

git add . This will change new files, deleted files and modified files. Previously in git 1.0, there used to be a difference between git add . and git add -A but since 2.0 they are the same.

git add -u This will only change modified or deleted files. It will not add new files.

Generally, when you commit, you do not need to run git add, however, it can be useful if you need separate commit messages for separate files.

Back to the tutorial, lets add a file called index.html and put inside something very minimal for now:

Save the file and now run git add .. You should see it adding your index.html file.

Step 3.5: Branching

You can switch between git branches using git checkout <<branch name>>, create branches using git branch <<branch name>>, and view all branches using git branch.

For now, we don't need to do this.

Step 4: Committing

Every time you finish a feature, it is good practice to commit. When you commit to a repository, it still stays locally and hasn't been pushed to the remote repository.

We've added our file, now lets make our initial commit using git commit -m "<<message>>", where the message is your commit message.

Step 5: Pushing

We've finally come to the step where we push our changes to the remote repository.

Run git push <<the remote name from step 2>> to push your commits to the remote repository.

git push accepts two optional parameters - remote name and branch name, for example, git push origin main.

Go sip on a well-earned cup of coffee, you've completed the first half.

Practical 1

Oh No! We forgot the comma in hello world. Go ahead and change the file.

Now we can compare the changes using git diff.

Great, we're all clear to commit our changes and push to the remote repository!

Practical 2

Hmm, let's add a Javascript timestamp clock to our project.

Add a file called index.js and copy the code:

Since this is a new feature, let's create another branch. Run git branch timestamp-clock, then git checkout timestamp-clock to go to the branch.

Now write your commit and push to the timestamp-clock branch. In a real-world environment, we would normally continue developing the feature in the timestamp-clock branch, but for simplicity's sake, let's imagine we've spent hours of work and tens of commits on this branch.

Let's merge it back to the main branch:

  1. First, we must checkout to the target branch using git checkout main.
  2. Next, we need to run git merge timestamp-clock to merge it.

Wrapping Up

Congratulations, you now know the basics of git. Go brag about it to your friends!

Please do drop a comment and let me know how you felt about this article!

Very funny git meme

Top comments (0)