Git Mastery: Git branch and undoing changes

Photo by Yancy Min on Unsplash

Git Mastery: Git branch and undoing changes

Table of contents

No heading

No headings in the article.

Git is a popular version control system that helps developers keep track of their code changes, collaborate with others, and manage different versions of their projects. In this article, we will go over some of the most commonly used Git commands and provide examples to help you get started.

  • Git Branch

The git branch command is used to list all the branches in your repository. By default, the current branch you are on is indicated by an asterisk (*) next to its name. To create a new branch, use the following command:

rachana-uniyal@rachanauniyal:~$ git branch <branch-name>

For example, to create a new branch called feature1, you would run:

rachana-uniyal@rachanauniyal:~$ git branch feature1
  • Git Checkout -b <branch>

The git checkout command allows you to switch between different branches in your repository. To create a new branch and switch to it in one command, you can use the -b option:

rachana-uniyal@rachanauniyal:~$ git checkout -b <branch-name>

For example, to create a new branch called feature2 and switch to it, you would run:

rachana-uniyal@rachanauniyal:~$ git checkout -b feature2
  • Git Merge Branch

The git merge command is used to combine two branches into one. For example, if you have created a new branch called feature2 and want to merge it into your master branch, you would run the following command while in the master branch:

rachana-uniyal@rachanauniyal:~$ git merge feature2

This command will take the changes from the feature2 branch and apply them to the master branch.

  • Git Revert <commit>

The git revert command allows you to undo a specific commit. To revert a commit, you need to know its commit ID, which is a unique identifier for each commit in your repository. You can use the following command to revert a commit:

rachana-uniyal@rachanauniyal:~$ git revert <commit-ID>

For example, if you want to revert a commit with the ID abc123, you would run:

rachana-uniyal@rachanauniyal:~$ git revert abc123
  • Git Reset File

The git reset command is used to reset changes in your repository. If you want to reset changes to a specific file, you can use the following command:

rachana-uniyal@rachanauniyal:~$ git reset file

For example, to reset changes to a file called index.html, you would run:

rachana-uniyal@rachanauniyal:~$ git reset index.html
  • Git Clean -n

The git clean command is used to remove untracked files from your repository. The -n option is used to dry-run the command, which means it will show you what files will be deleted without actually deleting them. To run a dry-run of the git clean command, you would run:

rachana-uniyal@rachanauniyal:~$ git clean -n

This command will show you a list of untracked files that would be deleted if you were to run the git clean command without the -n option.

In conclusion, these are the Git commands related to branch and undoing changes.