- userA, userB, and userC all setup their computers with a Github account.
- userA wants to create an original repo called
ProjectName
. - userB wants to fork the
ProjectName
repo from usernameB. - userC wants to fork the
ProjectName
repo from userB.
Go to https://github.com/userA
/ and click the "New" button. Name the new repo project-name
.
-
On your computer, go to terminal and
cd
into the directory you want to host the local repo, then do:$ git clone https://github.com/userA/project-name.git
-
Check that you have remote:
$ git remote origin
-
Open the project in finder and in atom to edit the content of the folder.
$ open . $ atom .
Alternatively...
On your computer, in Finder, make a new folder called project-name
. Copy all the files you want to the ProjectName
folder.
In the Terminal, do the following:
-
Type
cd
into the terminal then drag theproject-name
folder into the terminal. You’ll seedirectoryPath
being auto-completed in Terminal.$ cd directoryPath
-
Initialize the
project-name
folder as a git repo. This essentially makes this folder a "local git repo".$ git init
-
Link your "local git repo" to a "remote git repo" that you own. Call this remote git repo
origin
.$ git remote add origin ssh://[email protected]/userA/project-name.git
-
Make sure
origin
is an available remote and check the path.$ git remote # git remote -v
-
Sync your local repo to your remote repo.
$ git fetch origin $ git add --all $ git commit -m 'Initial Commit!' $ git push origin master
Note, if you added a LICENSE file or a README file from Github, make sure to do a
git pull origin master
before you do agit add --all
. -
Now open a file within
ProjectName
in Atom or another text editor. Make some changes to the code. To sync your local repo with your remote repo, do this:$ git fetch origin $ git add --all $ git commit -m 'I made some changes!' $ git push origin master
-
After you accept a pull request from userB, your remote repo is updated. Now you want to sync your local repo with your remote repo:
$
git pull origin master
Go to https://github.com/userB
/project-name
and click the "Fork" button.
In the Terminal, do the following:
-
Type
cd
into the terminal and drag and drop the folder where you want to create the project folder from Finder into Terminal. You’ll see directoryPath being auto-completed in Terminal.$ cd directoryPath
-
Make a new folder called
project-name
.$ mkdir project-name
-
Go into the
project-name
folder$ cd project-name
-
Initialize the
project-name
folder as a git repo. This essentially makes this folder a "local git repo".$ git init
-
Link your "local git repo" to a "remote git repo" that you own. Call this remote git repo
origin
.$ git remote add origin ssh://[email protected]/userC/project-name.git
-
Populate the
project-name
folder on your computer (i.e., local repo) with the files from the remote repo's master branch.$ git pull origin master
-
List the files in your
project-name
folder to make sure the pull was successful. You should see this folder populated with files downloaded from the remote repo$ ls
-
Link your "local git repo" to another remote git repo that userB owns. Recall you forked the
project-name
repo from userB. The upstream user's (i.e., userB) repo. Call thisupstream
.$ git remote add upstream https://github.com/userB/project-name
-
Make sure
origin
andupstream
are both available remote.$ git remote
-
Now open a file within
project-name
in Atom or another text editor. Make some changes to the code. To sync your local repo with your remote repo, do this:$ git fetch origin $ git add --all $ git commit -m ‘I made some changes!' $ git push origin master
-
To sync your local repo with userB's repo, do the following:
$ git pull upstream master
Some useful things you can do when things go haywire or just to get the most usefulness out of git:
-
Revert your local repo to the previously committed version when everything still worked:
$ git log # git will display the following: # commit: <commitID> # ... # type q to exit the log : # force your local to the commitID $ git reset --hard <commitID> # force your remote to the commitID $ git push --force origin master
-
Switching between branches:
# checkout the alias of the remote e.g., origin $ git remote -v # switch to a new branch called boilerplate $ git checkout -b boilerplate # look at all your branches $ git branch # you should see git display the following branches for origin: # b1 = master and b2 = boilerplate # push local repo to boilerplate branch $ git push origin boilerplate
-
Your local repo should never go out of sync with your remote repo. Git prevents you from pushing changes from your local repo to your remote repo if there are changes in your remote repo that you don't have locally. If you don't want to sync with your local repo with the remote and you just want to overwrite the remote repo with whatever you have locally, this is a useful command:
$ git push origin master --force
-
If you accidentally initiated a directory as a git repo, this undos the
git init
:$ rm -rf .git
-
If you don't want to have to enter your password everytime when you push to a remote, then in the project local repo, do the following:
- Per the official guide from Github, you want to switch your remote URLs from SSH to HTTPS:
$ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
The .gitignore
file should be included in your local repo folder, which tells git to not add files and folders that you don't want to share publically on Github to your remote repo. What kind of files are these?
DS_Store
and other metadata files that macOS create.env
files which includes configuration for your application that may include secret keys that you don't want people to know about.- Transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc. Prime example is the
node_modules
which tend to be large and people downloading your project can get from runningnpm install
if they have the right dependencies listed inpackage.json
.
In general, you should include the following in your .gitignore
#.gitignore
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp
pids
logs
results
tmp
coverage
# API keys
.env
auth.js
# Dependency directory
node_modules
bower_components
build
# Editors
.idea
*.iml
# OS metadata
.DS_Store
Thumbs.db