DEV Community

Cover image for Git: 20 basic commands to know as a developer
Akande Joshua
Akande Joshua

Posted on

Git: 20 basic commands to know as a developer

What is Git?

Git is a free and open-source distributed version control system designed to help developers manage changes to their code over time. It was created by Linus Torvalds, the creator of the Linux operating system, and first released in 2005.

With Git, developers can track changes to their codebase, collaborate with others, and revert to previous versions of their code if necessary. Git stores all code changes in a repository, which can be hosted locally on a developer's machine or remotely on a server.

One of the key benefits of Git is that it allows developers to work independently on their own copies of the code, making it easier to manage complex projects with multiple contributors. Git also makes it easy to merge changes from different contributors into a single, unified codebase.

Git is widely used by developers across many industries and is supported by a large and active community of contributors. Many popular code hosting platforms, such as GitHub and Bitbucket, use Git as their underlying version control system.

Components of Git

*Working directory: *
This is the directory on your local machine where you store your project files. You can modify files in this directory using your favorite text editor or IDE.

Staging area:
Also known as the "index", this is an intermediate step between your working directory and your Git repository. When you make changes to your files in the working directory, you can use the git add command to stage those changes. Once changes are staged, they are ready to be committed to the repository.

Repository:
This is where Git stores all of the versions of your project files. The repository is typically hosted on a server, but can also be stored locally on your machine. When you use the git commit command, Git takes a snapshot of the files in the staging area and stores them in the repository. Git also records metadata about the commit, such as the author, date, and commit message.

These three components work together to allow you to track changes to your project files over time, collaborate with others, and revert to previous versions of your code if necessary.

Below are the 20 basic commands all developers should familiarize themselves with before starting to work with Git.

1. git init: Initializes a new Git repository in your current directory.

Example:

git init
Enter fullscreen mode Exit fullscreen mode

Initialized empty Git repository in /Users/username/project/.git/

2. git add: Adds changes made to files in the current directory to the staging area, preparing them to be committed.

Example:

git add myfile.txt
Enter fullscreen mode Exit fullscreen mode

3. git commit: Commits changes in the staging area to the local repository, along with a message describing the changes made.

Example:

git commit -m "Added a new feature"
Enter fullscreen mode Exit fullscreen mode

4. git status: Displays the current state of the repository, including which files have been modified, which files are in the staging area, and which files are not being tracked.

Example:

git status
Enter fullscreen mode Exit fullscreen mode

Response will be like

On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: myfile.txt

5. git log: Displays a log of all previous commits made to the repository, including the commit message, author, and timestamp.

Example:

git log
Enter fullscreen mode Exit fullscreen mode

Response

commit 1234abcd5678efgh
Author: John Smith john@example.com
Date: Mon Jan 1 12:00:00 2023 -0500
Added a new feature
commit 5678efgh1234abcd
Author: Jane Doe jane@example.com
Date: Sun Dec 31 23:59:59 2022 -0500
Initial commit
6. git pull: Fetches and merges changes from a remote repository into the local repository.

Example:

git pull origin master
Enter fullscreen mode Exit fullscreen mode

7. git push: Pushes local changes to a remote repository, making them available for others to access.

Example:

git push origin master
Enter fullscreen mode Exit fullscreen mode

8. git branch: Lists all branches in the repository, or creates a new branch.

Example:

git branch
Enter fullscreen mode Exit fullscreen mode
  • master

feature-branch

9. git checkout: Switches to a different branch, or checks out a specific commit.

Example:

git checkout feature-branch
Enter fullscreen mode Exit fullscreen mode

10. git merge: Merges changes from one branch into another.

Example:

git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

11. git clone: Creates a local copy of a remote repository.

Example:

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

12. git diff: Shows the differences between the working directory, staging area, and repository.

Example:

git diff myfile.txt
Enter fullscreen mode Exit fullscreen mode

13. git reset: Unstages changes from the staging area.

Example:

git reset myfile.txt
Enter fullscreen mode Exit fullscreen mode

14. git revert: Reverts a previous commit, creating a new commit that undoes the changes made.

Example:

git revert 1234abcd5678efgh
Enter fullscreen mode Exit fullscreen mode

15. git rm: Removes a file from the repository.

Example:

git rm myfile.txt
Enter fullscreen mode Exit fullscreen mode

16. git remote: Shows a list of remote repositories, or adds a new remote repository.

Example:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Response:

origin https://github.com/username/repository.git (fetch)
origin https://github.com/username/repository.git (push)

17. git fetch: Fetches changes from a remote repository, but does not merge them into the local repository.

Example:

git fetch origin
Enter fullscreen mode Exit fullscreen mode

18. git tag: Creates a new tag for a specific commit, such as to mark a release.

Example:

git tag v1.0.0 1234abcd5678efgh
Enter fullscreen mode Exit fullscreen mode

19. git stash: Temporarily saves changes made to the working directory, allowing you to switch branches or pull changes from a remote repository without committing the changes.

Example:

git stash
Enter fullscreen mode Exit fullscreen mode

20. git branch -d: Deletes a branch in the local repository.

Example:

git branch -d feature-branch
Enter fullscreen mode Exit fullscreen mode

This command deletes the feature-branch branch from the local repository. If the branch has not been merged into the current branch, Git will prevent you from deleting it. In that case, you can use the -D option instead of -d to force the branch to be deleted.

Keep in mind that when you delete a branch, any commits that were made only to that branch will also be deleted from the repository. So be sure to double-check which branch you're deleting before you run this command.

These are just a few basic Git commands with examples to get you started. As you become more familiar with Git, you'll likely use many more commands and options to manage your codebase.

Thank you.

Top comments (0)