Handy git commands

Clearcase was my first version control software. I used it for 2 years and eventually I liked it since most of the commands are really simple to understand. In these two years I used to debug the code on debugging branch. Once I was done with the fix, I renamed the branch to follow the standard specified by my company. The command below was really handy to rename the branch.

 [cmd-context] $rename replica:old_branch_name new_brach_name 

But when I migrated to Git version control, I couldn’t get head around git. It took a while to discover that even git has command to rename the branch which is as following.

$ git branch -m old-branch-name new-branch-name 

This kind of practice leaves a lot of branches in your local and remote repo and sometimes you wonder to clean this up.

$git push origin :debug-branch 

The above command will delete debug-branch on the origin remote and below is the command used to delete the branch present in local repo.

$ git branch -D debug-branch 

This command can also be used with -d option. The only difference between these two commands is that -D forces the delete process but -d doesn’t.

If you have made some changes in a file and now you don’t want those changes. you can run the following command to do that.

$ git checkout <file-name>  

There are instances when you staged the file but now you want to unstage the file. For this use the below command.

$ git reset HEAD <file-name> ...  

If you have create and new branch and want to push to the remote repo (origin) then use the following command

$git push origin your-branch-here 

and to verify that your branch has been push to origin or not run the following command.

$git branch -r 

This command basically lists the remote branches.
If you like to know about the branch which has the specific commit hash code or tag, then use this command.

git branch --contains <tagname or commit has code> 

You may also encounter scenario where you added the file in the git index and realized that we don’t want to commit this file. If we don’t the the exact command you may guess this command.

git rm <file_name> 

Well if you execute this command you will get this error.

error: 'file_name' has changes staged in the index
(use --cached to keep the file, or -f to force removal)

Well, Git itself has provided the hint for the correct command. If you want to keep the file but don’t want to commit the changes then use the –cached option.

git rm --cached <file_name> 

Your valuable comments are always welcomed. It will help to improve my post and understanding.

  “By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest.” 
By : Confucius

,
Trackback

no comment untill now

Add your comment now