Master Git Branch Management: Essential Commands and Best Practices
This guide provides a comprehensive overview of essential Git commands for pulling, committing, branching, merging, tagging, stashing, and managing files, along with practical tips and conventions to streamline collaborative development workflows.
Branch Management Diagram
Basic Operations
git pull # pull default master branch
git pull origin master # pull specified branch
git add . # add all files in current directory
git add * # add all files in repository
git add dirname test.php hello.txt # add specific files or directories
git commit -m 'comment' # commit with message
git push # push to remote repository
git checkout . # discard all local changes
git checkout test.php # discard changes to test.php
git clean -fd # remove untracked files and directories
git reset HEAD test.php # unstage a file
git checkout test.php # discard changes after unstaging
git reset --hard HEAD^ # revert to previous commit
git log # view commit history
# Example commit line:
# commit 4aa614980a3db998f3f6299f7c22e82f4e248e27
# Author: Gong Fuxiang <[email protected]>
# Date: Thu Aug 27 21:47:26 2015 +0800
git reset --hard d496317fc # revert to specific commit (short hash ok)
git push origin master # push reverted state to remoteFile/Folder Management
git rm test.php # delete a file
git rm -r dirname # delete a directory and its contents
git mv test.php new_file_name.php # rename a file
git commit -m 'file rename' # commit rename
git push # push to remoteBranch Management
git branch develop # create a branch
git checkout develop # switch to branch (shows * develop)
git checkout -b develop # create and switch to new branch
git fetch origin test # fetch remote branch without merging
git checkout test # switch to fetched branch
git branch # list local branches
git branch -a # list all branches (local + remote)
git branch -m develop new_name # rename local branch
git push origin develop # push local branch to remote
git branch -d develop # delete local branch
git push origin --delete develop # delete remote branch (Git < 1.7.0 use git push origin :develop)
git merge develop # merge specified branch into currentTag Management
git tag v_0.0.1 # create a tag
git log --pretty=oneline --abbrev-commit # view short commit IDs
# tag a specific commit:
git tag v_0.0.1 2663f5a
git tag # list all tags
git push origin v_0.0.1 # push a single tag
git push origin --tags # push all tags
git tag -d v_0.0.1 # delete local tag
git push origin --delete tag v_0.0.1 # delete remote tag (Git < 1.7.0 use git push origin :refs/tags/v_0.0.1)
git fetch origin tag v_0.0.1 # fetch a specific remote tagStashing Changes
git stash # stash current changes
git stash list # list stash entries
git stash pop # apply latest stash and drop it
git stash apply # apply latest stash without dropping
git stash apply stash@{0} # apply specific stash entry
git stash drop # delete the latest stash entryGit Usage Guidelines
1. Create separate branches for different features; for multi‑developer work, push to the remote repository and develop collaboratively on the new branch.
2. Tag the code before merging branches to clearly mark version numbers.
3. Write detailed commit messages.
Source: 21CTO compiled from the web
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
