Git Guide - mikec964/chelmbigstock GitHub Wiki
I've tried to organize this guide by common tasks, more or less in the order in which you would usually perform them. You'll want to have this concept in mind, though: There will be at least two and probably four copies of a project when you get set up:
- Your working directory where you are editing and building.
- The local git repository with your own commit history.
- The remote repository, probably on Github
- The local cached copy of the remote repository
Contents
You can install Git from Git-SCM.com.
Mac Users, to install Git you may need to:
- First, get the Command Line Tools for XCode from Apple's developer site. I'm not sure, but this might include Git. Try typing Git from the terminal.
- If not, try to install from Git-SCM.com.
- If that fails, try to install using Homebrew with the
brew install git
Set your username and email address:
git config --global user.name "Mike Combs"
git config --global user.email "[email protected]"
You don't have to use SSH to access your projects; you can use HTTPS. But here's the SSH setup. I suggest you add a comment to the key that is your Github username, and use a passphrase that isn't your Github (or other) password.
cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]" # -t is type of key, -C is comment, use the email you use on github
eval `ssh-agent -s` # starts ssh-agent, if not already running
ssh-add id_rsa # prompts for passphrase you chose in the ssh-keygen step. Memorizes passphrase for future.
pbcopy < id_rsa.pub # copies to clipboard
If you don't have pbcopy, you can use a text editor to open the public key and copy the text to your clipboard. Or, just use cat ~/.ssh/id_rsa.pub
and then copy the output to your clipboard. Then go to Github, Account Settings, Keys, and add the key. Test your key with this:
ssh -vT [email protected] # tries to ssh to github
ssh -T [email protected] # tries to ssh to github
For some reason, the ssh-agent steps don't work for me. It doesn't memorize the password and when I try to ssh into github, it prompts me for the passphrase. Simply enter the passphrase into the dialog box and check the "Add to keychain" box. From then on it will not prompt you. Test this by logging out of the terminal window, opening a new one, and trying to log into github again.
http://www.dribin.org/dave/blog/archives/2007/11/28/ssh_agent_leopard/
I don't have the definitive answer yet, but this page seems to get at it: https://superuser.com/questions/88470/how-to-use-mac-os-x-keychain-with-ssh-keys#269570
These instructions apply if you already have a project on disk and you want to start managing it in Git.
cd <projectdir>
git init
Edit .gitignore to set which files to ignore, like *.pyc files.
After you've done some work, or immediately after you've created a new repository, you need to copy your working files into your local repository. This is called commit. Before you commit, you select which files will be part of the commit using the add command.
git status #Shows which branch you're on, which files are staged, which have changed`
git status -s # shortened status`
git add . # adds all files in this folder`
git add <files>`
Change your mind?
git rm --cached <files> # removes files from staging
Commit moves the files from your working directory to your local repository.
git commit -m "descriptive message" # commits staged files
git commit # launches editor to enter message
git commit -a -m "message" # commits all files, like git add .
git log # shows history of commits to repository
git log --oneline
Look at changes
git diff --cached # compares file to staged file
If this is your project, go to Github and create the repository. Get the HTTPS or SSH URL.
If this is someone else's project, get the HTTPS or SSH URL of the project. Then clone the project to your computer:
cd <parent of the project folder the next step will create>
git clone <remoteURL>
Now, configure your local git to link to the project on Github. Name this link "origin".
cd <project>
Use the SSH URL for the project. The SSH password should be stored using ssh-agent or the Mac OS key chain.
git remote add origin [email protected]:mikec964/chelmbigby.git # git remote add <name, usually origin> <remoteURL>
This is the HTTPS URL. If you use this, it will prompt you for your password every time you push your code into the repository. Don't use this. It prompts for your Git password, not your SSH password.
git remote add origin https://[email protected]/mikec964/chelmbigpy.git # same thing, using https with my username
git remote -v # list the remotes, see if you got it right
git remote rm origin # remove it so you can change it, or edit .git/config
To push into someone else's repository, they must first add your Github username to the project's list of collaborators.
Test by pushing your code, even if you haven't made any changes.
git push origin master # git push [name] [branch]
git push
Make your changes and commit them (into local). Before you push those changes back to the remote, fetch+merge (or pull) before you push. This avoids conflicts from being pushed up to the remote.
git checkout master # switch back to the master branch
git fetch # copies remote repository (probably origin) to *remote* repository cache
git merge # merges *remote* into your *working* copy (probably master)
pull origin master # more or less same as fetch then merge
At this point you should test the code and resolve any conflicts. Repeat fetch+merge/pull and testing until there are no conflicts.
git commit -a -m "merge branch X with feature Y into master"
git push
These notes are a mess; I don't know what I was thinking anymore.
You can merge changes to the master into a branch. You can merge changes in the branch into the master.
git checkout --f # revert all files to last committed version
The master branch is the shipping version. You branch to add new features which will be merged back into master.
cd <project>
git branch <branch> # create the branch
git checkout <branch> # now we're working in the new branch
git branch # lists local branches; tells you which branch you're on
git branch -r # lists remote-tracking branches
git checkout master
git merge <branch>
git fetch
git merge
git commit -a -m "new branch update"
git push
git checkout <branch to work in> # branch2
git fetch # copies remote repository to remote repository cache
git merge <source branch> #master
if there's a conflict, make changes to the file with a conflict, then commit. git commit -a -m "new branch update" git push