Skip to content

Instantly share code, notes, and snippets.

@pyliaorachel
Last active August 2, 2018 03:39
Show Gist options
  • Save pyliaorachel/10dbe592daa3dd40b12ab009a069bc0a to your computer and use it in GitHub Desktop.
Save pyliaorachel/10dbe592daa3dd40b12ab009a069bc0a to your computer and use it in GitHub Desktop.
Git Basics

Basic Flow

If you're not using any branching (i.e. you only work on master branch), you can follow the below simple and basic Git flow.

Step 0: install git & configure

Install:

  • MacOS: open your terminal, $ brew install git
  • Windows: install GitBASH in Git for Windows, then right-click anywhere on desktop and choose Git Bash

Configure:

$ git config --global user.name <your-name> 
$ git config --global user.email <your-email>
Step 1: clone a project from GitHub

Go to GitHub, create an account, and create a new repository. You can also "fork", i.e. copy, from other's repository to your own account by clicking the "Fork" button on other's repository.

Then click "Clone or download" and copy the URL in the form https://github.com/<your-username>/<repo>.git. In the terminal, clone the repo from that URL:

$ git clone <URL>

Step 2: make some changes

Get into the repo just cloned:

$ cd <repo-name>

Then make some changes within the repo...

Step 3: saving changes
# 1. Check status
$ git status                                # show which files are modified  

# 2. Add files that were updated to staging area, in which they are ready to be saved to history
# EITHER
$ git add <file/dir>                        # tells Git you wanna include updates to a particular file in the next commit
                                            # (i.e. put them in staging area)
# OR
$ git add -A                                # you wanna include ALL updates in the next commit

# 3. Save to history
$ git commit -m <message>                   # actually commits what you added to the staging area
                                            # please include a concise commit message for future reference
Step 4a: get updates from remote

The changes made in step 2 & 3 were all on your own local machine. You need to sync with your GitHub repo as well. Before you push your local changes to GitHub, pull down the latest version from GitHub first, because there might be changes made by others.

$ git pull origin master      # Git will try to merge them, but you need to resolve any conflicts introduced manually
Step 4b: get updates from other repo (i.e. repos other than your own forked one) (only when repo forked)

Used when you forked the repo from others, and you want to sync with their changes.

$ git remote -v                             # show what remote names you have, usually you'll have an 'origin' remote repo  
                                            # that's where you cloned your repo from in Step 1  

$ git remote add <remote-name> <repo-url>   # add a remote repo, usually from the repo you forked from
                                            # usually name it 'upstream'  

$ git pull <remote-name> master
Step 4c: resolve conflicts (if any)

Git tells you which files are having conflicts like this:

...
Auto-merging <file>
CONFLICT (content): Merge conflict in <file>
Automatic merge failed; fix conflicts and then commit the result.
...

Open that file, you'll see:

<<<<<<< HEAD
<your version of code>
=======
<remote version code>
>>>>>>> branch-a

Decide what to keep and remove all other stuffs. Then commit again.

$ git add -A
$ git commit -m <message>
Step 5: push commits to remote
$ git push origin master                     # push to the master branch of your own repo
Step 6: make a pull request (only when repo forked)

If your remote repo is a forked one, and you would like to request the original repo to merge your changes, i.e. update the original repo so that your changes are reflected.

Go to your forked GitHub repo and press the button New Pull Request. This will send the pull request to the original repo, and the authorized users can decide whether to merge your changes, or abort.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment