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
Initialize a new Git repository in this directory:
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
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")
git add to add new files or changes to files in the staging area:
git add file.txt
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 #
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"
To view the commit history, use
git log # commit 2f1e3c5d5e4f3e2d1c0b9a8f7e6d5c4b3a2b1a0b (HEAD -> master) # Author: Your Name <firstname.lastname@example.org> # Date: Fri Dec 17 14:00:00 2022 +0000 # # Added a new line to file.txt # # commit a1b2c3d4e5f6a7b8c9d0a1b2c3d4e5f6 (initial commit) # Author: Your Name <email@example.com> # Date: Fri Dec 17 13:00:00 2022 +0000 # # Initial commit #
Create a new branch called "dev" with
git branch dev
We can use
git checkout to switch between branches:
git checkout dev
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
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
Now if your teammate wants to get this repository, all they need to do is:
git clone https://github.com/user/myproject.git
If they make any changes to the repository and you want to get their changes, you can use
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.
Top comments (0)