Learn git in less than 5 minutes and use it for 5 years

Sam - Dec 16 '22 - - Dev Community

Git is a version control system that is widely used by developers to track changes to their codebase. It allows developers to collaborate on projects, revert back to previous versions, and maintain a clear history of their work.

Git has a number of commands that are essential for working with it effectively. In this blog post, I'll cover some of the most common git commands that every developer should know and you'll use the thousands of times during the course of your career.

Before we move on, you can see an actual example of a git repository at https://github.com/dotenx/dotenx and the end result at https://dotenx.com. Make sure to check them out as both the repository and the tool can come in handy someday.


Now, let's take a look at the commands.

First, create a new directory for your project and navigate into it:

mkdir myproject
cd myproject
Enter fullscreen mode Exit fullscreen mode

init

Initialize a new Git repository in this directory:

git init
Enter fullscreen mode Exit fullscreen mode

status

Create some files for your project and make some changes to them:

echo "Initial version" > file.txt
echo "This is a new line" >> file.txt
Enter fullscreen mode Exit fullscreen mode

Now, use git status to check the status of your repository:

git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   file.txt
#
# no changes added to commit (use "git add" and/or "git commit -a")
Enter fullscreen mode Exit fullscreen mode

add

Use git add to add new files or changes to files in the staging area:

git add file.txt
Enter fullscreen mode Exit fullscreen mode

This time, if we run git status, this is what we'll see:

git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   file.txt
#
Enter fullscreen mode Exit fullscreen mode

commit

Now it's time to commit the changes to the local repository with a commit message:

git commit -m "Add a new line to file.txt"
Enter fullscreen mode Exit fullscreen mode

log

To view the commit history, use git log:

git log
# commit 2f1e3c5d5e4f3e2d1c0b9a8f7e6d5c4b3a2b1a0b (HEAD -> master)
# Author: Your Name <you@example.com>
# Date:   Fri Dec 17 14:00:00 2022 +0000
#
#     Added a new line to file.txt
#
# commit a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6 (initial commit)
# Author: Your Name <you@example.com>
# Date:   Fri Dec 17 13:00:00 2022 +0000
#
#     Initial commit
#
Enter fullscreen mode Exit fullscreen mode

branch

Create a new branch called "dev" with git branch:

git branch dev
Enter fullscreen mode Exit fullscreen mode

checkout

We can use git checkout to switch between branches:

git checkout dev
Enter fullscreen mode Exit fullscreen mode

remote

You can add the remote repository as the "origin" to your local repository with git remote add:

git remote add origin https://github.com/user/myproject.git
Enter fullscreen mode Exit fullscreen mode

push

Now, if you make some more changes, and commit them you can push them to the remote repository.

echo "This is another new line" >> file.txt
git add file.txt
git commit -m "Add another new line to file.txt"
git push origin dev
Enter fullscreen mode Exit fullscreen mode

clone

Now if your teammate wants to get this repository, all they need to do is:

git clone https://github.com/user/myproject.git
Enter fullscreen mode Exit fullscreen mode

pull

If they make any changes to the repository and you want to get their changes, you can use git pull:

git pull
Enter fullscreen mode Exit fullscreen mode

Git is a very powerful tool that you'll use daily as a software developer and these commands are amongst the most common commands you'll use when working with git. Each of these commands gets multiple options that help you achieve what you need. In another post, I'll focus only on merging branches and how you can do it in multiple ways.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .