My Git Notes


Typical git commands

git clone git@github.com:<user_name>/the-repo-you-are-cloning.gitgit fetch upstreamgit merge upstream/mastergit add <file>git status and git diffgit status -sgit commit -m 'the message goes here for the commit'git add '*.<file_extension>'git rm --cached <file>git loggit log --summarygit reset <file>git rebase

Work with branches

git branch -rgit branch -m <old-name> <new-name>git checkout -b <new-branch-name>git push <remote-name> <new-branch-name>git remote <name> origingit push origin --delete <branch-name>git push origin :<old-name> <new-name>git push origin :<old-name> <new-name>git checkout <target>

Work with Git Remote URL (URI)

git remote add origin git@github.com:<username>/<reponame>.gitgit remote set-url origin git@github.com:<username>/<reponame>.gitgit remote -v

Force git to overwrite local files on pull

git fetch --all<br>git reset --hard origin/master

1. downloads the latest from remote without trying to merge or rebase anything.

2. resets the master branch to what you just fetched

Tag version

Create tag

Delete tag, also remote


Set commit messages in modal text editor, like Vim


Fork & Merge changes from remote Github repository to your local repository

git clone <Your Public Clone URL>

cd <Local-Repository>
git remote add <remote-name> <Public Clone URL>
git fetch <remote-name>

Fork

git remote add <name> <Public Clone URL>
git pull <name> master
git push

Example

$ git clone git@github.com:bueltge/wp-butler.git
cd wp-butler
git remote add upstream git://github.com/Japh/wp-butler.git
git fetch upstream
git pull upstream master
git push

Rollback a git repository to a specific commit

git reset --hard <old-commit-id>>git push -f <remote-name> <branch-name>

How to undo a local/public commit

git revert HEADgit reset --soft HEAD^

More, check out (StackOverflow 927358)[http://stackoverflow.com/questions/927358/undo-the-last-git-commit]

Clean up a fork and restart it from the upstream

Using 'upstream' as the remote name referencing the original repo forked

More, check out (StackOverflow 9646167)[http://stackoverflow.com/questions/9646167/clean-up-a-fork-and-restart-it-from-the-upstream]

Submodule git commands

git submodule initgit submodule update --recursive

Helpful .gitconfig Configurations

[alias]
​ a = !"git alias"
​ c = commit
​ s = status
​ p = pull
​ w = whatchanged
​ rb = rebase
​ # Rebase interactive
​ rbi = !"git rebase -i"
​ # Rebase interactive on our unpushed commits
​ rbiu = !"git rebase -i @{u}"