Repositories
Depending on your needs, you may want to start with a brand-new repository; say for a new project you’re working on, or clone an existing repository so you can assist with its development.
Creating a Repository
cd
into the directory which contains your project.- Run
git init
to create a git repository.
To push the new repository into github:
git remote add origin https://github.com/username/repository.git
git add *
git commit -m 'initial commit'
git push -u origin master
Cloning a Repository
Below are a few ways you can clone a repository.
# cloning a repository from your local system
git clone ~/repository ~/clone_of_repository
# closing a repository from a remote server
git clone username@server:/existing_repository ~/clone_of_repository
# cloning a repository from github
git clone [email protected]:username.repository.git ~/clone_of_repository
Updating the Origin
If the origin repository changes its name or location, you can use the following command to reconnect to it.
git remote set-url origin [email protected]:username.repository.git
Adding/Removing files from the Repository
Git will only track the files you specify. Use the following commands to tell git which files you wish to be apart of your repository:
git status
.
# To add a file:
git add [file]
# To add all files under the working directory:
git add .
# To remove a file:
git remove [file]
Committing
Committing is like taking a snapshot of your project. It allows you to roll-back to that state in time if required.
To commit, run: git commit -m [message]
. The message should be a short sentence that outlines the changes you’ve made. It needed incase you wish to roll-back, you’ll know when based off of your messages.
Pushing
Push
takes your changes which have been committed and uploads them to yout remote repository.
git push origin [master/branch]
Fetch & Pull
Fetch
pulls down all of the changes performed to the remote repository but does not attempt to merge those changes with your local repository.
Pull
on the other hands will pull all changes from the remote repository and merge those changes with your local repository.
# Pull down all changes to the remote repository but do not merge them with the local repository.
git fetch origin [master/branch]
# Pull and merge all changes with the remote repository into your local repository.
git pull origin [master/branch]