Setup and use a new online repo

Sign in to github or bitbucket; start a new repository; add .gitignore (things to exclude such as your python virtualenv, config files with passwords etc), then follow the instructions to:

git clone # Clone the remote repo to local repo
git add -A # Add all files / subdirectories to staging area
git commit -m 'Commit comment' # Commit the changes to the local repo
git push -u origin master # Push to the remote repo, master branch

Setup new online repo from command line

git init
git add .
git commit -m 'initial commit of full repository'
# bitbucket:
curl --user USERNAME \
--data name=REPONAME --data is_private='true'
git remote add origin https://[email protected]/USERNAME/REPONAME.git
# github:
curl -u 'USER' -d '{"name":"REPONAME "}'
git remote add origin
git push -u origin --all

Find out what is going on

git status # Get current status of git
git log # Get Git commit log showing their SHA
git branch # List local branches and find out current branch
git diff # Shows differences (?)

Using branches

git branch branch_name # Creates a new branch.
git checkout branch_name # Used to switch from one branch to another.
git merge branch_name # Used to join file changes from one branch to another.
git branch -d branch_name # Deletes the branch specified.

To merge back to master

git checkout master # Switch back to master branch.
git merge branch_name # Merge the branch.
git push # push the master branch
git branch -d branch_name # Deletes the local branch specified.
git push origin --delete branch_name # Deletes the remote branch

Correct way to collaborate on remote repos

1. Fetch and merge changes from the remote
2. Create a branch to work on a new project feature
3. Develop the feature on your branch and commit your work
4. Fetch and merge from the remote again (in case new commits were made while you were working)
5. Push your branch up to the remote for review

(Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command.)

Explanation of pull requests: (note pull=fetch+merge)

Undoing / resetting / updating stuff

git checkout HEAD filename # Discards changes in the working directory.
git reset HEAD filename # Unstages file changes in the staging area.
git reset # Can be used to reset to a previous commit.
git fetch # fetches the remote repo and updates local copy
git rm --cached filename # remove file from the repo without deleting local copy of the file
git rm filename.txt # remove file from the repo completely
git commit -m "remove filename.txt"

Remove all commits before commit XXXXX

git checkout --orphan temp XXXXX
git commit -m "Truncated history"
git rebase --onto temp XXXXXmaster
git branch -D temp
git prune --progress
git gc --aggressive
git push -f origin master