Git cheatsheet
Exclude a directory
Create a .gitignore
file in root, and add the directory name in it
dir_name/
Remove directory from git and local
git rm -r dir_name
git commit -m "remove directory"
git push origin [you-git-branch]
Remove directory from git but NOT local
git rm -r --cached dir_name
Remove .DS_Store
# Remove existing files from the repository
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
# Add the line to `.gitignore`
.DS_Store
# git commands
git add .gitignore
git commit -m ".DS_Store removed"
Delete master branch
First delete master
in the local repo. To do this we first make a new branch call placeholder
, and delete master
from there:
git branch placeholder
git checkout placeholder
git branch -D master
We next want to delete the branch on github. However, if we do this the naive way:
git push origin :master
we will get error like this:
remote: error: refusing to delete the current branch: refs/heads/master
To git@github.com:matthew-brett/datarray.git
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'git@github.com:matthew-brett/datarray.git'
This is because github is looking at the master
branch to provide the web content when you browse the remote repo. So we first have to make github look at our placeholder
branch instead, then delete master
.
First push up the placeholder
branch
git checkout placeholder
git push origin placeholder
Then set placeholder
to be the github default branch. Go to the page of remote repo, then select Settings
>Branches
. Under Default branch
, switch the default branch from master
to placeholder
. Now you can delete the master
branch
git push origin :master
Create project page
Project page are kept in the same repository as their project. The URL for the project page is https:://<username>.github.io/<projectname>
. The steps for creating a project page are as follows
- You can build and publish Project Pages sites from the
master
andgh-pages
branch. You can also publish your site from a/docs
folder on yourmaster
branch. - To publish your site’s source files from a
/docs
folder on yourmaster
branch, you must have amaster
branch and your repository must:- have a
/docs
folder in the root of the repository - not follow the repository naming scheme
<username>.github.io
or `.github.io
- have a
- Under your repository name, click
Setting
>Options
>Github Pages
>Source
. Select master branch /docs folder from the drop-down menu as your Github Pages publishing source. (This option will not appear if the/docs
folder doesn’t exist on themaster
branch.)
Remove sensitive/large data
This is for the case where you accidently committed sensitive or large files, see the original post. There are more steps than what is listed here for multi-branch repos, see the original doc for more information.
- Navigate into the repository’s working directory.
$ cd YOUT-REPOSITORY
- Run the following command, replacing
PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA
with the path to the file you want to remove, not just its filename. These arguments will:- Force Git to process, but not check out, the entire history of every branch and tag;
- Remove the specified file, as well as any empty commits generated as a result;
- Overwrite your existing tags.
$ git filter-branch --force --index-filter \ 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \ --prune-empty --tag-name-filter cat -- --all
- Add your file with sensitive data to .gitignore to ensure that you don’t accidentally commit it again.
$ echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore $ git add .gitignore $ git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
Folk your own repo
It’s not allowed to folk your own repo in Github, so here is a work-around:
-
Create a new repo for your folk.
- clone your fork to local
git clone git@github.com:<username>/<forked-repo>.git
- Add your original repo as an upstream remote
cd <forked-repo> git remote add upstream git@github.com:<username>/<original-repo>.git
- Check if the new
upstream
has been addedgit remote -v
We should get:
origin <fork-repo> (fetch)
origin <fork-repo> (push)
upstream <original-repo> (fetch)
upstream <original-repo> (push)
- Update your fork
git pull upstream <branch>