# stages the desired file
git add new-file
# changes the last commit's message and includes staged files in that commit
git commit --amend
git remote prune origin
# view existing remote URL
git remote -v
# change existing remote URL
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
Reference: Github
git update-index --skip-worktree <file>
(This only works if you don’t pull again from master)
Reference: StackOverflow
# undo last commit and leave changes unstaged
git reset HEAD~
# undo last commit and leave changes staged
git reset --soft HEAD~
# undo last commit and undo changes (WARNING: This is permanent)
git reset --hard HEAD~
Reference: How to undo the last commits in Git?
git diff -p \
| grep -E '^(diff|old mode|new mode)' \
| sed -e 's/^old/NEW/;s/^new/old/;s/^NEW/new/' \
| git apply
Reference: Gist
git log -p DIR
git diff master..yourbranch path/to/folder
- Go to the commit immediately before the commit you want to edit:
git rebase -i <earliercommit>
- In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify.
- Then do one of the following:
git commit --amend --author="Author Name <email@address.com>"
git commit --amend --reset-author --no-edit
- Finally end the rebase:
git rebase --continue
Reference: Gist
1. Install ondir
(on a Mac: brew install ondir
, other OSs need to install it from the linked page).
2. Put these functions into your shell profile (e.g. .profile, .zshrc):
# ondir configuration
cd() {
builtin cd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
pushd() {
builtin pushd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
popd() {
builtin popd "$@" && eval "`ondir "$OLDPWD" "$PWD"`"
}
eval "`ondir /`"
3. Configure ondir by putting these lines into ~/.ondirrc
(adapt it to whatever you need):
enter ~/dev/work
export GIT_AUTHOR_NAME='Name at work'
export GIT_AUTHOR_EMAIL='email@work.com'
export GIT_COMMITTER_NAME='Name at work'
export GIT_COMMITTER_EMAIL='email@work.com'
echo 'Switched to git user/email settings for "work".'
leave ~/dev/work
unset GIT_AUTHOR_NAME
unset GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME
unset GIT_COMMITTER_EMAIL
echo 'Switched back to global git user/email settings.'
4. Now when you cd to ~/dev/work, git config user.name will still show the global git user name, but for commits, the environment variable will be used (check with echo $GIT_AUTHOR_NAME).
Don’t forget to source the changed shell profile to your current shell session.
Reference: How to set git user and email per directory