DEV Community

Cover image for Git basics
Mohammad Ayaan Siddiqui
Mohammad Ayaan Siddiqui

Posted on

Git basics

Git is a widely-used version control system that allows developers to track changes to their codebase over time. It is an essential tool for collaborative software development and has become a fundamental component of modern software engineering.

In this guide, we will provide a comprehensive overview of Git, including its key concepts, basic commands, and best practices.

Key concepts of Git

Before diving into the Git commands, it is important to understand some key concepts of Git. These include:

  • Repository: A repository is a collection of files and directories that Git tracks.

  • Commit: A commit is a snapshot of changes made to the repository at a particular point in time.

  • Branch: A branch is a separate line of development that diverges from the main line (or "master" branch) of the repository.

  • Merge: Merging is the process of combining changes from one branch into another.

  • Remote: A remote is a copy of the repository that is hosted on a different server

Basic Commands of Git

Image description

Image by Doabledanny

git init: This command initializes GIT to your folder.

git init
Enter fullscreen mode Exit fullscreen mode

git add: This command adds changes made to a file to the staging area, which prepares the changes to be committed to the repository. You can add specific files or directories by specifying their names after the add command. For example:

git add file.txt        # Add changes made to file.txt to the staging area
git add directory/     # Add changes made to all files in the directory to the staging area
Enter fullscreen mode Exit fullscreen mode

git commit: This command saves changes made to the repository with a commit message that describes what changes were made. The -m option allows you to add a message to the commit, which is useful for keeping track of changes and collaborating with other developers. For example:

git commit -m "Added new feature"    # Commit changes with a descriptive commit message
Enter fullscreen mode Exit fullscreen mode

git push: This command uploads changes from the local repository to a remote repository, such as a repository hosted on GitHub. The origin keyword refers to the remote repository you are pushing to, and main refers to the branch you are pushing changes from. For example:

git push origin main     # Push changes from the local main branch to the remote repository named origin
Enter fullscreen mode Exit fullscreen mode

git pull: This command retrieves changes from a remote repository and merges them into the local repository. This is useful for keeping the local repository up-to-date with the latest changes made by other developers. For example:

git pull origin main     # Pull changes from the remote main branch into the local repository
Enter fullscreen mode Exit fullscreen mode

git branch: This command lists all branches in the repository. The current branch is highlighted with an asterisk. For example:

git branch               # List all branches in the repository and show which branch is currently checked out
Enter fullscreen mode Exit fullscreen mode

git checkout: This command switches to a different branch. You can specify the branch you want to switch to by providing its name after the checkout command. For example:

git checkout new-feature     # Switch to the new-feature branch
Enter fullscreen mode Exit fullscreen mode

git merge: This command combines changes made in one branch with another branch. For example, if you want to merge changes made in the new-feature branch with the main branch, you can do the following:

git checkout main       # Switch to the main branch
git merge new-feature   # Merge changes from the new-feature branch into the main branch
Enter fullscreen mode Exit fullscreen mode

git clone: This command creates a copy of a remote repository on your local machine. You can specify the URL of the repository you want to clone after the clone command. For example:

git clone https://github.com/user/repo.git    # Clone the repository located at https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode
  1. git status: This command displays the current state of the repository, including which files have been modified, which files are staged, and which branch the repository is currently on. For example:
git status    # Show the current status of the repository
Enter fullscreen mode Exit fullscreen mode
  1. git log: This command displays a list of commits made to the repository, including the commit message, author, and date. You can use options to filter the list, such as displaying only the last 5 commits or only commits made by a specific author. For example:
git log         # Display a list of all commits made to the repository
git log -5      # Display the last 5 commits made to the repository
git log --author="John"   # Display only commits made by the author "John"
Enter fullscreen mode Exit fullscreen mode

git fetch: This command retrieves changes from a remote repository without merging them into the local repository. This is useful for keeping the local repository up-to-date with changes made by other developers without modifying the local repository. For example:

git fetch origin    # Fetch changes from the remote repository named origin
Enter fullscreen mode Exit fullscreen mode
  1. git branch -d <branch>: This command deletes a branch from the repository. You can only delete branches that have already been merged into another branch. For example:
git branch -d new-feature    # Delete the new-feature branch
Enter fullscreen mode Exit fullscreen mode

git clone -b <branch> <remote>: This command clones a specific branch from a remote repository to your local machine. For example, to clone the development branch from the remote repository named origin, you can do the following:

git clone -b development https://github.com/user/repo.git    # Clone the development branch from the remote repository named origin
Enter fullscreen mode Exit fullscreen mode

git reset: This command resets the repository to a specific commit, discarding any changes made after that commit. You can use it to undo changes or remove uncommitted changes from the repository. For example:

perlCopy codegit reset HEAD~1    # Reset the repository to the previous commit, discarding any changes made after that commit
Enter fullscreen mode Exit fullscreen mode

git push <remote> <branch>: This command pushes changes from the local repository to a specific branch in a remote repository. For example, to push changes from the local main branch to the development branch in the remote repository named origin, you can do the following:

git push origin main:development    # Push changes from the local main branch to the development branch in the remote repository named origin
Enter fullscreen mode Exit fullscreen mode

Top comments (0)