This article is in continuation to my existing article Getting started with GIT on Linux. If you are new to Git, I would recommend you to first go through my previous article and then continue with this.
In this article, we will cover creating a branch, tag, renaming the branch and revert the commits on Git.
- Branch: Branching helps to create a new line of development to avoid any mess on the main branch.
- Tag: A tag in Git is a reference to Git history.
- Rename a Branch: Renaming a branch means changing the name of the existing branch and having our existing code on a branch with a new name.
- Revert a commit: Reverting helps to undo changes in the local and remote repository.
Pre-requisites
- Basic understanding of Git (Click here to learn the basics of Git.)
What will we do?
- Create a branch.
- Merge a branch to the main branch.
- Create a tag.
- Rename a branch.
- Revert a commit.
Create a branch
Go to your local repository and just check your branch and the status of it using the following commands before creating a new branch. Here, test-repo is my repository name.
pwd
git status
git log
git branch
Create a new branch in your existing repository and check-out to it.
git branch
git branch my-feature-branch
git branch
git checkout my-feature-branch
git branch
Let's create a new file and push it to the branch we created.
ll
touch new-file-in-my-feature-branch
git status
git add new-file-in-my-feature-branch
git commit -m "created a new file in my-feature-branch"
git push
git push --set-upstream origin my-feature-branch
Merge a branch to the main branch
If we want our changes in the new branch to be merged in the main branch we can use the following commands to merge those changes in the branch we want. First, we need to checkout to the main branch and then merge the branch we created.
ll
git branch
git checkout main
ll
git merge my-feature-branch
ll
Create a tag
Creating a tag is as simple as creating a branch. First, let's make some changes in the repo, and then create a tag. We still need to commit out changes before we push them to the remote repo.
git tag
ll
touch new-file-for-tag
git status
git add new-file-for-tag
git tag -a mytag.v1 -m
git tag -a mytag.v1 -m "create a tag"
git tag
git log
git status
git commit -m "create a tag mytag.v1"
git push
We can check what all tags we have and commits to those tags. The way we push our branch we can push tags too.
git tag
git show mytag.v1
git push origin mytag.v1
The way we checkout to a branch, we can checkout to a particular tag too.
git branch
git tag
git checkout mytag.v1
git branch
Let's check out to the main branch before we proceed further.
git branch
git checkout main
git branch
Rename a branch
Sometimes you may feel that you need to rename your branch. You can do it very easily using the following commands.
git branch
git branch wrong-brach
git checkout wrong-brach
touch file-in-wrong-branch
git add file-in-wrong-branch
git commit -m "Created a branch wrong-brach with a new file"
git push
git push --set-upstream origin wrong-brach
git branch
git branch --move wrong-brach correct-branch
git branch
push --set-upstream origin correct-branch
You can even delete the branch from the remote repo.
git branch
git branch -a
git push origin --delete wrong-brach
git branch -a
Revert a commit
In case you want to revert your commit and still preserve your changes locally, you can use the following commands to do a soft reset.
git branch
git checkout main
git log
git reset --soft HEAD~1
git log
You can see that after reverting the commit, your local changes are still available.
git status
git pull
git log
git status
If you want to revert your commit and do not want to preserve your changes locally you can use the following commands and do a hard reset
git log
git reset --hard HEAD~1
git status
git log
In this case, you can see that after reverting your commit you do not have your changes locally
git status
git pull
git log
In the above two scenarios, we saw reverting the local commits. Sometimes you may need to revert your commits from the remote repo. To do so you need to push your changes to the branch with "+" as a suffix to the branch name after you revert your changes locally.
git branch
git log
git reset --hard HEAD~1
git push +main
git push origin +main
You can see that the remote repository does not contain the commit which has been reverted.
Conclusion
In this article, we covered scenarios like creating a branch and tag. We saw how a branch can be renamed if we realize we need to change the name of the existing branch. We also saw commits in the local repository, as well as the remote repository, can be reverted. You can now use commands from this article and try them out on your own.