Skip to content

Instantly share code, notes, and snippets.

@Alexisvt
Last active March 9, 2023 16:54
Show Gist options
  • Save Alexisvt/588cc94a7202b8d06be9909c7d561db6 to your computer and use it in GitHub Desktop.
Save Alexisvt/588cc94a7202b8d06be9909c7d561db6 to your computer and use it in GitHub Desktop.
Git commands for branch's managment

Branch Management

How to create a branch

git branch my-branch

How to move to a branch

git checkout my-branch

How to create a new branch and navigate to it at the same time

git checkout -b my-branch

if you want to create a branch and reset it if there is an existing one with the same name use -B option, like this

git checkout -B my-branch

Or if we want to create a new branch and use a different branch as base one write the remote name plus the name of the branch like this

git checkout -b my-branch origin/branch-name

How to create a new branch and navigate to it at the same time

git checkout -b my-branch

if you want to create a branch and reset it if there is an existing one with the same name use -B option, like this

git checkout -b my-branch origin/other-branch

How list existing branches

git branch

or

git branch --list

Note: the current branch will be highlighted in green and marked with an asterisk.

How to push the new branch

git push -u origin my-branch

How to list remote branches

git branch -r

or if you want to show both local and remote branches, use instead:

git branch -a

How to list all the branches that have been merged into the current branch (locally and remotely)

git branch --merged
git branch -a --merged

How to merge a branch

We need to follow the next steps:

git checkout master
git pull origin master
git branch --merged
git merge my-branch
git push origin master

How to delete a branch (local and remote)

The first command will delete my-branch branch locally and the next one remotely and prune it from the list

git branch -d my-branch
git branch -d -r origin/my-branch

How to prune branches from remote list

git remote prune origin

If we want to see which branches will be prune without actually prune them run this instead

git remote prune origin --dry-run

How to list the remote URLs of the current branch

git remote -v

If we want to remove one of them

git remote rm origin

Above we are deleting the origin URL from the remote list

@Alexisvt
Copy link
Author

Alexisvt commented Mar 9, 2023

This is another list of helpful git commands:

Retrieve/Clone a repo = git clone (URL)
List remotes = git remote (-v for detail)
Commit graph = git log --all --decorate --oneline --graph
Retrieve/download from a remote = git fetch (remote name)
merge branch or tracking-branch = git merge (branch or tracking branch name)
Show status = git status
Upload to a remote = git push (remote name) (branch name)
stage an edit = git add (filename)
make a commit = git commit -m "description"
stage and commit = git commit -a -m "description"
List local branches = git branch
List remote branches = git branch -r
List both local and remote branches = git branch -a

@Alexisvt
Copy link
Author

Alexisvt commented Mar 9, 2023

This adds another remote to an existing repo

git remote add upstream (URL) 

@Alexisvt
Copy link
Author

Alexisvt commented Mar 9, 2023

Sync upstream

git remote add upstream (URL) this adds another remote to an existing repo
git fetch upstream
git merge upstream/master
git push origin master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment