- Get a Github account
- Download latest version of Git
- Do the setup steps described here
- Set up credentials, to avoid annoyance of entering password each time you connect to Github
- Not sure how to do this step-by-step on Windows and Mac.
- Pro Git ebook
- Git tutorial - Try Git
- Github for beginners
- An intro to Git and Github for beginners
- Git - the simple guide
- Software carpentry
- Atlassian git tutorials
- Git concepts simplified
- Github visual guide
- Gitk tutorial
- Aha moments when learning git
- A successful git branching model
- Git best practices
- PDF from rogerdudler.github.io/git-guide
- PDF from education.github.com
- PDF Markdown cheatsheet from Github Guides
- Basic Git commands
git status
git add -A - track files in staging area for commits
git diff - see changes
git commit -am "message" - commit tracked files to local computer.
git commit -a -m "Main commit message" -m "Second paragraph with more details" - commit multiple paragraphs.
git push origin master - sync local master branch to online repo's master branch Explanation
git checkout -b mybranch - switch to branch (-b is to create & checkout in 1 step)
git branch - list branches in repo and current branch (-a option will also show remote-tracking branches)
git add -A - if files were staged in master they are also staged in branch but good to do in case new files are made
git commit as usual within the branch
git push origin mybranch - sync local branch mybranch to online repo's branch mybranch
git merge mybranch --no-ff --m "merging mybranch into current branch" - mybranch = name of branch you want to merge with current branch (such as master).
Make sure you're in the branch you want to merge to! --no-ff - no fast-forward (can add or reduce confusion on case-by-case basis)
- Solve merge conflict
- Open file with conflict
- Delete conflict marker lines containing
<<<<<<<,=======,>>>>>>> - Make the changes you want to see in the final merge
- Save file
git add .git commit -m "resolved merge conflict"
- Solve merge conflict during pull requests
git branch -d mybranch - delete local branch
git branch -rd upstream/master - delete remote-tracking branch locally. Doesn't delete the remote branch on Github.
git push origin --delete mybranch - delete remote branch
git push origin master - don't forget to push "the branch that you merged into" to the remote too!
gitk --all - visualize commit tree
- Restoring single file locally
HEAD- most recent commit,HEAD~1= one commit beforeHEAD(read the docs on how HEAD pointers work)git log --oneline -5- see unique identifiers for last 5 commits
git log --patch src/helloworld.py- narrow down which commit you want to restore (gives list of SHAs - Secure Hash Algorithm values uniquely identifying the commit)
git checkout c6fab02 src/helloworld.py- checkout a file from the commit whose SHA started withc6fab02
git commit –a –m “restore helloworld.py from commit c6fab02”- commit the file you checked out, which was from an earlier commit - Restore everything locally to where it was after a prior commit (use with caution)
git reset --hard c6fab02 - Make a new branch starting at a previous commit
git checkout c6fab02; git checkout –b mybranch - Undoing a merge
- Restore after pushing to remote repo
git revert- but safest way is to just fix the bad code locally and push a new commit
git remote -v - remote URLs (such as origin)
git ls-remote - references in remote repo
git ls-tree -r master - list all tracked files in branch named "master"
git tag, git push origin --tags, etc. - tag stable versions of your code and push them online so people can download "Release v1.5" for example
- Pro Git Ebook Section 6.2: Github - Contributing to a Project
- scikit-learn-sprint-instructions (PDF)
- Contributing - scikit-learn
- Contributing to Numpy
- Git fetch and merge
- Difference between git pull and git fetch
General fork-merge workflow (Source)
- Fork the project.
- Create a topic branch from master.
- Make some commits to improve the project.
- Push this branch to your GitHub project.
- Open a Pull Request on GitHub.
- Discuss, and optionally continue committing.
- The project owner merges or closes the Pull Request.
- Fork the group project repo (in browser)
cdto a folder in command linegit clone "https://github.com/my-username/my-reponame.git"- copy forked repo to your computercd my-reponame- do all subsequent git commands from the repo's local foldergit remote add upstream https://github.com/group-username/group-reponame.git- track a remote repo calledupstream. This should be the group project repository that you just forked.git remote -v- showsoriginis the repo that you forked, the URL should have your username. Everything you push tooriginwill go to a repo under your username. There should also be a remote calledupstream.git checkout -b mybranch- make a new branch- Write code, make some commits to
mybranch. - Make sure your local
masterandmybranchare always up-to-date with theupstream/masterbranch!- Get changes from
upstream/masterbranch into localmasterbranchgit checkout master- subsequent bullets assume you're inmasterbranch.- Method 1:
git pull -r upstream master- Pull upstreammasterbranch into currently checked out local branch (should bemasterbranch).-roption is to rebase (otherwise it would do a merge). - Method 2: safer than Method 1 because you can examine changes before merging
git fetch upstream master- fetchmasterbranch ofupstreamrepo but don't merge or rebase yet, in other words "update my local copy ofupstream/masterbranch"gitk --all,git diff master..upstream/master,git log --oneline master..upstream/master- See differences between localmasterand fetchedupstream/mastergit merge upstream/master- merge fetchedupstream/masterbranch into currently checked out local branch (should bemasterbranch).
- Now, local
masterbranch is up-to-date with upstream repo'smasterbranch.
- Merge those changes to
mybranch, so it stays up-to-date withupstream/masterbranch.git checkout mybranch- subsequent bullets assume you're inmybranchgit diff master..mybranch- difference frommastertomybranch. For example, if a commit on mybranch added "123", it shows up as +123, meaning "123 added, relative to master".git merge master- mergemasterintomybranch.- Resolve any merge conflicts. Try to avoid edits conflicting with new features in
upstream/masterso your feature can be pulled seamlessly into main codebase later.
- Now, your local
masterand the group project repo'smaster(upstream/master) are identical, andmybranchhas successfully incorporated the latest state of the codebase, plus your new feature.
- Get changes from
git push origin master; git push origin mybranch- pushmasterandmybranchto your own repo.- In browser, initiate pull request from
mybranchwhen it's ready to be merged into the group project's master branch (upstream/master). - Monitor comments in pull request, make more commits/comments as needed until they merge and close pull request.
- If someone submits a pull request to your repo, checkout the pull request and test locally before merging it on Github.
- Pull the updated
upstream/masterback into your localmasterbranch and deletemybranch, since your work is now merged to the main codebase.