git
This page serves as a reminder for performing common, simple actions with git.
How do I create a new local branch?
This will create a new branch locally.
git checkout -b <new-branch>
How do I push a newly created branch to my remote repo?
This will push a newly created branch to the remote repo. It should be executed from the newly created branch.
git push -u origin <new-branch>
How do I delete a branch both locally and remotely?
This will delete the branch locally and in the remote. It assumes the remote name is “origin” as this is normally the case.
git push -d origin <branch_name>
git branch -d <branch_name>
When deleting the branch locally, if the branch is not merged with upstream, it may not let you delete the branch using ‘-d’. In this case you can force it using this command.
git branch -D <branch_name>
If you have other machines using the repo, you will want to use this command so they stop tracking obsolete branches.
git fetch --all --prune
How do I merge multiple commits from working branch into mainline as single commit?
git checkout mainline
git merge <branch-with-changes> --squash
How do I make changes to a commit without creating a new commit?
Sometimes you need to make changes to a commit after you’ve called git commit -m "message"
. This could because you forgot a file or just part of your code
review process where you want to make changes based of comments in your second
revision. While keeping the entire CR in one commit for easy tracking.
git add <changed-files>
git commit --amend
You can also use the --amend
flag to change the commit message.
How do I undo a ‘git add’ before a commit?
If you mistakenly added a file and want to remove it before committing, you can use this command.
git reset <file>
If you want to unstage all files you can use this command:
git reset
How do I undo my most recent local commit?
Sometimes you commit before you mean to, e.g. added a file you didn’t want. This will undo the commit to allow you to make changes or remove files while maintaining the same commit. This is ONLY for local commits and does not work if the commit was already pushed to your remote repo.
git reset HEAD~
How do I revert a Git repository to a previous commit?
This can be done relatively safely while maintaining the repo’s history using these commands which commits the change back to a previous commit.
git revert --no-commit <target-commit>..HEAD
git commit
How do I remove a git submodule?
There are 3 steps to de-initialize and remove a submodule from your repo.
git submodule deinit -f path/to/submodule
rm -rf .git/modules/path/to/submodule
git rm -f path/to/submodule
How to connect to remote github repo?
This assumes the repo repository exists and has nothing in it. This could be
the case if you just called git init
on a local project and then created a
new repo on github.
git remote add origin git@github.com:<username>/<repo-name>.git
git push -u origin main
How to ignore files in a git repo?
You can ignore files and directories by adding a gitignore file at the root of
your repo. If you already committed the file you want to ignore, you first need
to stop tracking. The following will stop tracking the public
directory and
make git ignore it in the future.
git rm -r --cached public
touch .gitignore
echo "public" > .gitignore
How do I rename a git branch?
You can rename a git branch locally (and it will create a new mirror branch when pushing without deleting your old branch) by using this command.
git branch -m <oldname> <newname>
If you want to rename the current branch you can also use this command:
git branch -m <newname>
If you are using a case sensitive file system, you have to use this command or you will encounter a “branch already exists” error.
git branch -M <newname>
How to remove untrack files from a git workspace?
This is what git clean
does. There are a few ways to call it. To remove
everything files and directories you can run:
git clean -f -d
To remove just files use this command:
git clean -f
To remove ignored files as well, use this command
git clean -f -x
To remove only ignored files, use this command:
git clean -f -X
To do this remove interactively, use this command:
git clean -i
How do I checkout a remote git branch?
You can checkout a remote branch by using this command.
git checkout -b <branch-name> origin/<branch-name>