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.
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.
# 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
.
git checkout feature-branch
git merge main
git checkout feature-branch
git merge main
Merge with squash
git merge --squash <branch-name>
git merge --squash <branch-name>
Create a local branch from an existing remote branch
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.
git branch <new-branch> <existing-branch>
git branch <new-branch> <existing-branch>
Add a remote repository
git remote add <remote-name> <repo-url>
git remote add <remote-name> <repo-url>
View all remote endpoints
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.
# 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
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
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
# 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.
git reset --merge ORIG_HEAD
git reset --merge ORIG_HEAD
To reverse to a specific commit
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.
git revert --no-commit <commit-sha>..HEAD
git revert --no-commit <commit-sha>..HEAD
To reverse a commit before its been pushed
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
git commit --amend
git commit --amend