Handling Git Branch Commands in Ubuntu Terminal

| | 1 min read

While working on a project using git repository, if you want to create and switch to a new branch, then use: git checkout -b [my-branch-name]

To simply checkout a branch, use: git checkout [my-branch-name]

If you want to rename a branch when pointed to any other branch, you can type the following command: git branch -m [old-branch-name] [new-branch-name]

To rename the current branch, use: git branch -m [new-branch-name]

Also to push a branch after adding files and a commit, then type git push origin [branch-name] in your command line.

In your current branch, if you want to know who updated the file last, then you can use: git blame [filepath].

To know who updated the line of a file last, use: git blame -L[LINE-NUMBER],+1 [filepath], for example, git blame -L21,+1 myfile.php.

For deleting your current branch, first checkout to another branch and type the command, git branch -d [branch].

The above command deletes the branch locally, so for deleting the branch remotely you have to use, git push origin --delete [branch-name].

For deleting a file from current branch, you have to use: git rm [filepath].

For listing all branches that are local and remotely connected, use the command, git branch -al.

Hope this helps.