Christopher SuAbout Blog Projects Contact More

Change file name capitalization for Git on Mac

Sometimes, depending on the file system, Git may not pick up case changes in filenames. To fix this, do the following:

git mv OldFiLename.thing temp.file
git mv temp.file oldfilename.thing

Cherry picking a commit

git cherry-pick <commit id>

Count the number of lines changed

To count the number of lines added/edited between two commits or branches:

git diff --stat <commit or branch> <commit or branch>

Delete git branch (local and remote)

To delete the branch locally:

git branch -d branch_name

To delete the branch on the remote:

git push origin --delete <branch_name>
or git push origin :<branch_name> for older git

Diff two branches

git diff branch_1..branch_2

Git web interface

Git has a built-in web interface that can be started with the following:

git instaweb --httpd=webrick

Move a commit to another branch

To move the last commit(s) to another branch, first make a new branch (this will include the commits from the current branch):

git branch newbranch

Remove the unwanted commit(s) from the current branch (the command below just removes the latest commit):

git reset --hard HEAD^

To remove N commits, you can use:

git reset --hard HEAD~N

Now newbranch has the new commit(s) while the current branch no longer does.

Rebase on pull

git pull --rebase <remote> <branch>

Remove a staged file

git reset <filename>

Remove a submodule

To fully remove a submodule from a git repository:

# Remove from .gitmodules
git submodule deinit <submodule_name>

# Remove from repository
rm -rf .git/modules/<submodule_name>

For older versions of Git, you may need to do the following as well:

# Remove from config
git config --remove-section <submodule_name>

# Remove from working tree
git rm --cached <submodule_path>

Revert local branch back to remote branch

git fetch origin
git reset --hard origin/master

Squashing commits

Enter an interactive rebase session:

git rebase -i <commit id to start after>

In interactive mode, replace pick with squash for the commits to squash.

Undo the last commit

To undo the last commit, discarding the changes:

git reset --hard HEAD~1

To undo the last commit, keeping the changes:

git reset HEAD^
git reset --soft HEAD~1