Add a remote
git remote add origin https://github.com/user/repo.git
See Adding a remote – GitHub Help
Create and checkout a new branch
git checkout -b [branchname]
Example:
git checkout -b master
Checkout a remote branch
git checkout -b [localbranchname] origin/[remotebranchname]
Example:
git checkout -b master origin/master
Delete a local branch
git branch -D [branchname]
Example:
git branch -D BranchName
Delete a remote branch
git push origin --delete [branchname]
Example:
git push origin --delete BranchName
Automatically stage tracked files
git add -u
Automatically stage tracked files in a folder
git add -u folder_name
Example:
git add -u myfoldername
Commit
git commit -m "{commit message}"
Example:
git commit -m "Update WordPress to version 4.3"
Get a list of all changed files after the most recent pull
git diff --name-status ORIG_HEAD..
Show the current state of the working directory and the staging area
git status
Stash Changes
If you wish to switch branches, but don’t want to commit what you’ve been working on yet, you’ll need to stash your changes.
git stash
To see which stashes you’ve stored:
git stash list
To keep your local modifications, stash them before pulling, then apply them afterward:
git stash git pull git stash pop
Discard all local changes
If you’ve changed files but wish to revert to the last committed version:
git checkout -- .
Remove untracked files
git clean -f
Dry Run:
git clean -f -n
Remove ignored files:
git clean -f -X
Remove directories and files:
git clean -f -d
Update Git to use a new .gitignore file
Commit all outstanding changes before running the following commands:
Remove everything from the index:
git rm -r --cached .
Then run:
git add .
Commit it:
git commit -m ".gitignore is now working"
Solution courtesy of StackOverflow
Edit/Amend last commit message
git commit --amend -m "New commit message"
Ignore permissions on working directory
git config core.filemode false
Ignore symlinks
Add all symbolic links to your .gitignore
:
find . -type l >> .gitignore
Remove all symbolic links from the repository:
find . -type l -exec git rm --cached {} \;
Ignore File Permission Changes
Make Git ignore local file permission changes:
git config core.fileMode false
Fix Git Index Locked By Another Git Process
If Git returns the following error:
Another git process seems to be running in this repository, e.g. an editor opened by ‘git commit’. Please make sure all processes are terminated then try again. If it still fails, a git process may have crashed in this repository earlier: remove the file manually to continue.
Run:
rm -f .git/index.lock