Add sub-project as remote
git remote add -f RemoteName RemoteUrl
git subtree command
Pull subtree as needed
git subtree add --prefix Path/To/Put/Code NameOfRemote master --squash
Create a branch without a parent
git fetch NameOfRemote master
git subtree pull --prefix Path/To/Put/Code NameOfRemote master --squash
Very useful when you are updating a project that you are rewriting. For example,
say you are using semantic versioning and are wanting to start a new major
Delete All Branches that have been merged
git checkout --orphan BRANCH
Great for cleaning up local branches that aren't being used any more.
Ignore changes to a file that is being tracked
git checkout master
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
git update-index --assume-unchanged [directory|file]
How to ignore dirty submodules
git update-index --no-assume-unchanged [directory|file]
.git/config and add
ignore = dirty.
Clone a repo and give name other than origin
path = path/to/submodule
url = git://github.com/username/repo.git
ignore = dirty
git clone -o upstream https://repo.git
Ignore Files for Repository without using
Add the file
.git/info/exclude and fill it with the contents you want to ignore. This will ONLY apply to the
repository and will not be tracked by git.
Count the number of commits that you have made, let's say the previous 5 are your commits.
The first commit leave as
Search for a specific line of code/file in the history
pick the rest will need to be changed to
squash. After that you will be able to
leave a new commit message or just leave as is to keep the commit messages from all previous commits.
Copy file from one branch to current branch
git log -SThatOneFile.php
Copy a file from
branch and put into staging.
Git Grepping for fun and profit!
git checkout BRANCH path/to/file.ext
# Real Life Examples
git checkout origin/featureBranch web/js/random.js
# Pulls into your current branch web/js/random.js from
# Basic grep (case sensitive)
$ git grep 'search term'
# Case Insensitive search
$ git grep -i 'search term'
# Search within a directory
$ git grep 'search term' src/
# Search only files with `php` extension
$ git grep 'search term' -- '*.php'
# Grep in the 'src/` directory, only yml files
$ git grep 'search term' -- 'src/**.yml'