Skip to content

Essential Git Operations

In all below examples, origin is used as the name of the remote repository. While this is convention, keep in mind that any identifier can be used (see git remote --help).

Getting info on remote changes

Get updates to all remote branches, but don't integrate them into local branches.

sh
git fetch origin # add `-p` to prune remote branches
git fetch origin # add `-p` to prune remote branches

Merge remote changes into a local branch

Assuming you are on <branch-name>, there are two main options.

sh
# Option 1: pull
git pull   # integrates remote changes for you current branch

# Option 2: fetch and merge
git fetch origin
git merge origin/<branch-name>
# Option 1: pull
git pull   # integrates remote changes for you current branch

# Option 2: fetch and merge
git fetch origin
git merge origin/<branch-name>

Note that git merge can also be used to merge local branches together. In all cases the branch that is specified is merged into the branch that is currently checked out. For example, the below operation will merge main into feature-branch.

sh
git checkout feature-branch
git merge main
git checkout feature-branch
git merge main

Merge with squash

sh
git merge --squash <branch-name>
git merge --squash <branch-name>

Create a local branch from an existing remote branch

sh
git checkout --track origin/<branch-name>
git checkout --track origin/<branch-name>

Create a branch from another local branch

Sometimes it is useful to branch off another branch that isn't currently checked out.

sh
git branch <new-branch> <existing-branch>
git branch <new-branch> <existing-branch>

Add a remote repository

sh
git remote add <remote-name> <repo-url>
git remote add <remote-name> <repo-url>

View all remote endpoints

sh
git remote -v
git remote -v

Delete a local branch once remote branch is merged and deleted

When working with a source control UI like GitHub or GitLab, often the UI will allow you to merge remote feature branches with main and then automatically delete them. In this case you may still have lingering local branches that would be best to clean up.

sh
# First prune any remote changes
git fetch origin -p

# Next, make sure you're ahead of the branch you merged
git checkout main
git merge origin/main
git branch -d <branch>
# First prune any remote changes
git fetch origin -p

# Next, make sure you're ahead of the branch you merged
git checkout main
git merge origin/main
git branch -d <branch>

Delete a local and remote branch

sh
git push -d origin <branch_name>     # delete remote branch
git branch -d <branch_name>                 # delete local branch
git push -d origin <branch_name>     # delete remote branch
git branch -d <branch_name>                 # delete local branch

Remove file from index but leave on filesystem

Push an existing folder to a new Git repository

sh
cd existing_folder
git init --initial-branch=main
git remote add origin git@github:custom_repository_ssh_uri
git add -A
git commit -m "Initial commit"
git push -u origin main
cd existing_folder
git init --initial-branch=main
git remote add origin git@github:custom_repository_ssh_uri
git add -A
git commit -m "Initial commit"
git push -u origin main

Make a tag

sh
# Basic
git tag <version>
# Advanced
git tag -a <version> -m "Message with tag"
# Basic
git tag <version>
# Advanced
git tag -a <version> -m "Message with tag"

Reverse a merge

Use the following command to reverse a merge to the commit directly before the merge.

sh
git reset --merge ORIG_HEAD
git reset --merge ORIG_HEAD

To reverse to a specific commit

sh
git reset --merge <commit-sha>
git reset --merge <commit-sha>

Reverse a commit

The following command will revert the current branch from HEAD to the commit referenced by the commit-sha.

sh
git revert --no-commit <commit-sha>..HEAD
git revert --no-commit <commit-sha>..HEAD

To reverse a commit before its been pushed

sh
git reset --soft HEAD~<N>
git reset --soft HEAD~<N>

Where <N> is the number of commits behind the current HEAD.

Adjust a commit message

sh
git commit --amend
git commit --amend