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.
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>
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>
Get into the repo just cloned:
$ cd <repo-name>
Then make some changes within the repo...
# 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
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
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>
$ git push origin master # push to the master branch of your own repo
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.