-
Notifications
You must be signed in to change notification settings - Fork 1
Introduction to Github
-
Download it from here. You can use either the GUI or the command line for Github if you install this version.
-
Sign up for an account with your
northwestern.edu
email account at Github. Once you have done that, go to Github Education and hit the "Request a discount" button to get your 5 free private repositories for the next 2 years. -
I regret to announce that if you are a collaborator of a private repository, it counts towards your "5 free private" totals.
-
cd
in the directory where you want the repository to appear -
In the command line, type
git clone https://github.com/lkloh/Cascades-2014.git
-
By default, you should be on the branch master.
-
We assume you are always in the repository
aimbat
(or whatever other parent repository) from now on, on the command line.
-
Supposed I have made changes to a file, and added a function to, for instance, draw a funny face. I now want git to save my changes.
-
Type in the command line,
git add .
to stage the commits, and then
git commit -m 'added function to draw a funny face'
- The syntax of the commit command is kind of like
git commit -m '[YOUR COMMIT MESSAGE HERE]'
-
To check if my commit was saved by Github, type
git log
. If it was saved, you should see the latest commit with your message there, and all other commits you made previously, in chronological order. -
Doing
git status
is also similar and shows you what changes you made. To exit thegit log
, just typeq
for quit. -
You should commit each time you make a significant change, and just before you push your changes to Github, or switch branches. Github will issue a warning if you don't, and disallow such commands if it notices you have un-committed changes.
-
Now supposed I was wrong. I wanted to draw an angry face, not a funny face. I wish to get rid of the funny face function I just committed. What do I do?
-
Do
git log
and check for the last commit when I drew the funny face. There's a commit hash there, that looks like gibberish. The message looks something like this:commit 3d600e56f185042ce14414803182cb9ea3b0b25b
Author: lkloh lkloh2410@gmail.com
Date: Tue Feb 25 22:19:56 2014 -0800
added function to draw a funny face
-
To get rid of that change, do
git reset --hard 3d600e56f185042ce14414803182cb9ea3b0b25b
-
Warning: This PERMANENTLY gets rid of the change you just made; its impossible to get it back. But I've found it the best and easiest way to get rid of changes I no longer want. There are other, less permanent ways to do something similar which are more complicated. Look at
git cherrypick
for examples.
-
git pull origin master
orgit pull origin [branch-name]
where branch-name is the name of the branch you want to get the latest changes from. -
You should do
git pull origin master
regularly to make sure you get the latest updated changes that all collaborators agree on. This will be faster than cloning the repository again as you only need to download the latest changes, not everything on Github.
-
I have checked out a branch branch-x, and made some commits, then pushed it to master and merged the branch. Now I realize I don't want any of the commits on the branch.
git checkout branch-x git fetch origin git checkout master git reset --hard origin/master
-
That gets rid of all commits made on that branch.
-
git blame --show-number [path-to-file]
-
This allows you to see who was the last person to touch that piece of code, so you can blame them if they messed up your stuff.
-
q
for quit to exit. -
Example: Supposed someone messed up the code in the file
aimbat/src/pysmo/aimbat/qualctrl.py
. I want to see who did it to blame them. I dogit blame --show-number /src/pysmo/aimbat/qualctrl.py
to see who did it.
-
You should have the final version you want to keep on branch master. To see which branch you are working on,
cd
into the project aimbat, wherever you stored it, and inside that folder, typegit branch
. You should get something like:- branch-1 branch-2 master
-
If there is a
*
in front of the branch, that is the branch you are on.
-
Make sure you are on the master branch. Do
git pull origin master
to get any latest updates from other collaborators (git will just notify you on the command line that there are no changes if no one has done anything to master yet). -
Get to the master branch. Suppose I wish to call my new branch branch-2.
-
In the command line, type
git checkout -b branch-2
. -
Type
git branch
and you should now be able to verify you are on a new branch, branch-2
-
If I am on branch-1, and now I want to switch to branch master, type
git checkout master
. -
Similarly, if I am on branch
master
and want to switch to branch branch-1, typegit checkout branch-1
. -
To change from branch-1 to branch-2, type
git checkout branch-2
.
-
Assume I have done some work on branch-1 that I am happy with and want to push to it onto Github to share with other people.
-
Type
git branch
to make sure I am on branch-1, and not accidentally on branch-2. -
Now I have verified I am on branch-1, type
git push origin branch-1
to push my work to Github. -
Now, sign in to Github. You should see the branch there. Open the pull request and you can merge it into the master branch there, if there are no merge conflicts.
-
I have now pushed the work on branch-1 onto master. The problem is, the stuff I did is not on the master branch on my personal computer. To get the latest changes, switch branches to master, and type
git pull origin master
to get the latest version onto my master branch.
-
Suppose I am happy with what I did on branch-2. I have done
git pull origin master
to add the latest changes to branch-2, then didgit push origin branch-2
to send my changes to Github, so my collaborators can get my changes. I logged into Github online to merge my changes. -
Now, I no longer need branch-2 sitting on my command line. To get rid of it, I switch branches to master, and type
git branch -D branch-2
. -
Type
git branch
. You should no longer see branch-2 there anymore.
-
In your account page, go to the main page for the repository you are working on.
-
Click on
Settings
, thenCollaborators
, and type the Github username of someone you want to add as a collaborator.
Explained here.
-
Happens if Alice and Bob are working on
file.py
at the same time. Suppose Alice finished first, and pushes her work to master. Now, Bob is done, and he doesgit pull origin master
before trying to push his branch to merge with master. -
A merge conflict may happen, if Git does not know whether to keep Alice's work or Bob's work. In this case, the compiler will complain about a "merge conflict" to Bob. Bob could resolve it by keeping the work he feels should be kept manually before pushing to master, as described here. Sorry its hard to describe without a live example.
-
Suppose Alice checked out branch
alice-rocks
. She did some work and pushed it toalice-rocks
. -
Alice suddenly remembered her advisor said not to do the work she just did since they did not need it anymore.
-
Alice deletes branch
alice-rocks
on her computer, and does not mergealice-rocks
to master. -
Next week, Alice does some work again, on a branch called
alice-rocks
, since she names branches that describe her well.
Note: it is good style to name a branch something more descriptive, such as finding-low-freq-events
if that is what you are doing.
-
She tries to push stuff to branch
alice-rocks
, but gets a error message! [rejected] alice-rocks -> alice-rocks (non-fast-forward) error: failed to push some refs to 'https://github.com/lkloh/PysmoTests.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
-
To fix this, on branch
alice-rocks
, dogit checkout -b not-alice-rocks
, and push branchnot-alice-rocks
to Github instead. Then go to Github and delete the existingalice-rocks
branch WITHOUT merging with master.
If anything seems weird with Github somehow, it would be best to delete the repository on your computer, then clone it again from Github. That usually fixes most strange issues.