ong-z
8/12/2016 - 8:34 AM

Git Branch

Git Branch

# Git branches 
# List local git branches
git branch

# List local git branches with last commits
git branch -v

# List referenced remote git branches
git branch -r

# List both local and remote git branches
git branch -a

# List branches merged or not merged into current branch
git branch --merged
git branch --no-merged

# Create git branch
git branch {branch_name}

# Switch branch 
git checkout {branch_name}

# Create + switch git branch in 1 step
# existing, uncommited work will also not be lost
git checkout -b {branch_name}

# **** Reset <branchname> to <startpoint> if <branchname> exists already.
git branch -f {branch_name} {start_point}
# if want to reset current branch
git reset --hard {branch/commit_to_reset_to}

# create a local branch from a remote branch
git checkout -b {branch_name} origin/{branch_name}

# Merge changes from branch to master (on master)
git merge {branch_name}
git merge {commit-id} (merge commits into current branch)

# delete branch
git branch -d {branch_name}
git branch -D {branch_name} # Force delete branches that have not been merged

# create a new remote branch from current local branch
git push origin {branch_name}

# Push local dashboard branch to remote spring branch
git push origin dashboard:spring

# Force push commit to remote master branch 
git push -f origin 52e36b294e:master

# remove remote branch (dangerous?)
git push origin :{remote_branch_name} # Git v1.5.0
git push origin --delete {remote_branch_name} # Git v1.7.0

# Only for fast forward merges
# Merge local branch foo into local branch master, without having to checkout master first.
# Here `.` means to use the local repository as the "remote":
git fetch . foo:master
# Merge remote branch origin/foo into local branch foo, without having to checkout foo first:
git fetch origin foo:foo

# show file from another branch
# where branch can be any ref (branch, tag, HEAD, ...) 
# and file is the full or relative path of the file
git show branch:file

# list the difference between the tips of 2 branches
git diff branch_1..branch_2 
git diff branch_1...branch_2 (diff from their common ancestor to test)
git diff --name-status master..branchName (shows files that have changed)