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 branchesgit fetch origin # add `-p` to prune remote branchesMerge 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 maingit checkout feature-branch
git merge mainMerge 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 -vgit remote -vDelete 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 branchgit push -d origin <branch_name> # delete remote branch
git branch -d <branch_name> # delete local branchRemove 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 maincd 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 mainMake 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_HEADgit reset --merge ORIG_HEADTo 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>..HEADgit revert --no-commit <commit-sha>..HEADTo 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 --amendgit commit --amend