Git Cheat Sheet

Updating all Changes (Add and Remove)

git add -u 

Very handy when you support plugin versions within a repository

Removing all removed Files

git diff --diff-filter=D --name-only -z | xargs -0 git rm

This is a great way to remove all files when you went to manually confirm files to add via git add command

Checking out a project into a folder with existing files

git init
git remote add origin $url_of_clone_source
git fetch origin
git checkout -b master --track origin/master

I tend to use this with WordPress related projects where the wp-content folder is in the repo and I needed to install it over a fresh install of WordPress.

Reset, Delete Commands

If you want to revert changes made to your working copy, do this:

git checkout .

If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:

git reset

If you want to revert a change that you have committed, do this:

git revert ...

If you want to remove untracked files (e.g., new files, generated files):

git clean -f

Or untracked directories:

git clean -d

Renaming Git Branches

1. Rename your local branch.
git branch -m new-name
git branch -m old-name new-name *if on another branch

2. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
git push origin -u new-name

Pulling all Remote Branches and Tracking

for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
git pull --all

Comments & Questions

Add Your Comment