DEV Community

Cover image for 𝐆𝐢𝐭 𝐁𝐫𝐚𝐧𝐜𝐡 𝐂𝐨𝐦𝐦𝐚𝐧𝐝
Megha Sharma
Megha Sharma

Posted on

𝐆𝐢𝐭 𝐁𝐫𝐚𝐧𝐜𝐡 𝐂𝐨𝐦𝐦𝐚𝐧𝐝

The 𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 command in Git is used for managing branches within a repository. It provides various functionalities to create, list, delete, and manipulate branches. Below are some common use cases for the 𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 command:

👉 𝐂𝐫𝐞𝐚𝐭𝐞 𝐚 𝐍𝐞𝐰 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Creates a new branch with the specified name.
𝐠𝐢𝐭 𝐜𝐡𝐞𝐜𝐤𝐨𝐮𝐭 -𝐛 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Creates and switches to a new branch in one command.

👉 𝐒𝐰𝐢𝐭𝐜𝐡 𝐁𝐫𝐚𝐧𝐜𝐡𝐞𝐬
𝐠𝐢𝐭 𝐜𝐡𝐞𝐜𝐤𝐨𝐮𝐭 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Switches to the specified branch.
𝐠𝐢𝐭 𝐬𝐰𝐢𝐭𝐜𝐡 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Switches to the specified branch (Git version 2.23 and later).

👉 𝐌𝐞𝐫𝐠𝐞 𝐁𝐫𝐚𝐧𝐜𝐡𝐞𝐬
𝐠𝐢𝐭 𝐦𝐞𝐫𝐠𝐞 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Merges changes from the specified branch into the current branch.

👉 𝐋𝐢𝐬𝐭 𝐁𝐫𝐚𝐧𝐜𝐡𝐞𝐬
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 Lists all the branches in the repository. The current branch is highlighted with an asterisk (*).

👉 𝐂𝐫𝐞𝐚𝐭𝐞 𝐚𝐧𝐝 𝐒𝐰𝐢𝐭𝐜𝐡 𝐭𝐨 𝐚 𝐍𝐞𝐰 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐜𝐡𝐞𝐜𝐤𝐨𝐮𝐭 -𝐛 <𝐧𝐞𝐰_𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Creates a new branch with the specified name and switches to it in a single command. (Or, using 𝐠𝐢𝐭 𝐬𝐰𝐢𝐭𝐜𝐡 -𝐜 <𝐧𝐞𝐰_𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> in Git versions 2.23 and later.)

👉 𝐑𝐞𝐧𝐚𝐦𝐞 𝐚 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐦 <𝐧𝐞𝐰_𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Rename the current branch.
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐦 <𝐨𝐥𝐝_𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> <𝐧𝐞𝐰_𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Rename a specific branch.

👉 𝐃𝐞𝐥𝐞𝐭𝐞 𝐚 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐝 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Deletes the specified branch. The -𝐝 option ensures that the branch has been fully merged before deletion. If the branch contains unmerged changes, Git will prevent deletion.

𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐃 <𝐛𝐫𝐚𝐧𝐜𝐡_𝐧𝐚𝐦𝐞> Forces deletion of the specified branch, even if it contains unmerged changes.

👉 𝐕𝐢𝐞𝐰 𝐑𝐞𝐦𝐨𝐭𝐞 𝐁𝐫𝐚𝐧𝐜𝐡𝐞𝐬
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐫 Lists remote branches (branches on the remote repository).

👉 𝐕𝐢𝐞𝐰 𝐀𝐥𝐥 𝐁𝐫𝐚𝐧𝐜𝐡𝐞𝐬 (𝐋𝐨𝐜𝐚𝐥 𝐚𝐧𝐝 𝐑𝐞𝐦𝐨𝐭𝐞)
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐚 Lists both local and remote branches.

👉 𝐒𝐡𝐨𝐰 𝐂𝐮𝐫𝐫𝐞𝐧𝐭 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 — 𝐬𝐡𝐨𝐰-𝐜𝐮𝐫𝐫𝐞𝐧𝐭 Displays the name of the currently checked-out branch.

👉 𝐕𝐢𝐞𝐰 𝐭𝐡𝐞 𝐋𝐚𝐬𝐭 𝐂𝐨𝐦𝐦𝐢𝐭 𝐨𝐧 𝐄𝐚𝐜𝐡 𝐁𝐫𝐚𝐧𝐜𝐡
𝐠𝐢𝐭 𝐛𝐫𝐚𝐧𝐜𝐡 -𝐯 Shows the last commit on each branch along with the commit message.

Top comments (0)