Skip to content

Instantly share code, notes, and snippets.

@lgommans
Created February 22, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgommans/1a7052b056f0cd4fd5cc9f4938386cf5 to your computer and use it in GitHub Desktop.
Save lgommans/1a7052b056f0cd4fd5cc9f4938386cf5 to your computer and use it in GitHub Desktop.
Git and Github basics
==== Creating repositories ====
Git has repositories. Github is a platform which keeps a copy of the
repository for you, and additionally makes it accessible through a website.
One can create a new repository by using the command "git init". You
then have a local repository, which is located in the .git/ folder, to
which you can do whatever you like. When you're ready to upload your
repository to Github for the first time, you need to:
1. Create the repository on Github (this doesn't actually create the
repository, it just registers the name).
2. Connect the repository using:
git remote add origin https://github.com/<user>/<repo>.git
3. Push the new repository:
git push -u origin master
If you make changes, you only have to use "git push". These three steps
are only for the first time.
==== Making changes ====
How to make changes? Well, it depends on what you want to do:
If you want to move a file, use the "git mv" just like you would the
normal "mv" command. For example, "git mv concept.txt final.txt". This
way, git can keep track of moved files. This allows it to be much more
efficient (namely, just a move operation, rather than creating a new
file in a new location).
If you want to remove a file, you should use the "git rm" command, for
example "git rm test.txt". Note that this only removes it from the
current version: it will remain in history. Search online for "git
remove file from history" if you want to remove it from history.
If you want to change a file: change the file, and add the changes with
"git add", for example "git add evidence.txt". Note that this adds
changes, it does not add a file. So if you do:
echo AAA > test.txt
git add test.txt
echo BBB > test.txt
Then the repository will have the version which contains "AAA". You need
to use "git add test.txt" again, to update the repository to "BBB".
After making your changes, you can check them using "git status" (list
of files and their statuses) and "git diff --cached" (the difference
between the last commit, and the currently added changes). If all is
okay, you can save them to the repository with "git commit". It will ask
you for a commit message, in which you describe the changes.
==== Collaborating ====
When working together with someone, or when changing a file using
Github's website, the Github version of the repository is newer than
your local version. You download the changes using "git pull". It is
best to do a git pull as soon as you know that the Github version has
changed.
A merge commit might be needed. This is done automatically when you use
"git pull", so don't be surprised if an editor pops up with a
pre-written message like "Merge branch something of something". Just
close the editor, the default message is fine.
If you try to push without pulling, Github will reject the push. In that
case, use "git pull" first.
If you work with someone else, and you both changed the same spot in the
same file, you get a merge conflict. The person who pulls in the other's
changes will have to resolve it. In git status, it is noted which files
conflicted. When opening the file, you'll find something like this:
import sys
import os
<<<<<<<
import time
=======
import math
>>>>>>>
This means that you added "import time" and the other person added
"import math". Edit the file (remove the <<<, === and >>>) so that it is
exactly the way you like. In the example, that would be:
import sys
import os
import time
import math
Save the file, use "git add" on the file, and commit it. You've now
resolved the merge conflict, and you can push these changes.
==== Summary ====
To edit your repository:
1. git pull to make sure you have the latest version:
2. Edit files how you like and use "git add <the file(s)>"
3. git commit
4. git push
Step 2 can also include "git mv" and "git rm".
================
Note that this guide did not include anything about branches, the
language style of commit messages, forks, pull requests, etc. This is
just basic usage, but I think it's already a lot of info.
Good luck and feel free to ask questions!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment