Hi. I am back with a new blog post on some Useful Git commands. Recently I pulled the latest code of a branch and due to some bad commit I was not able to use certain feature. So, I needed a way to rollback to some previous version(commit) i.e. the point when the code was working fine. So, I googled and found that there is a command which allows us to rollback to a specific commit using this command:-
git checkout commithash
We can also create a new branch from this checkout by using -b with the checkout command. The command below creates a branch while checking out if there’s no existing branch with the name :-
git checkout -b <new_branch_name>
We can use git log command to find the list of previous commits in a remote repository like this:-
git log origin/branchname
To filter the commits by a certain author we can use:-
git log --author=[authorname]
We can also reset our repository to a specific commit like this:-
git reset --hard [COMMIT HASH]
I would also like to share with you a concept of Git known as stashing.In our project we quickly switch to different branch and do some work, and we have some uncommited changes in the current branch.So we can either create a patch and apply it later or we can stash all changes from current branch and then apply it later.
We can save uncommited changes to stash using this command:-
git stash save "some message"
We can apply the latest stashed item like this:-
git stash apply
Apply and drop the latest stashed item:-
git stash pop
That was all about git commands!