Working with git

The Basics

Download a repository from github

git clone <url>

Push changes back to github

git commit -a -m “Commit Message”
git push

This will only publish modified files. If you’ve added any new files you have to manually stage them with

git add <filename>

Or if you want to stage all new, modified & deleted files, use

git add -A

To see what files have been staged before doing a commit, use

git status

Saving Credentials

You will be asked for your github username and password every time you push. To prevent this you can use the credential helper. Enable the credential helper with

git config credential.helper store

The next time you do a push, your credentials will be stored for future use. Your credentials will be stored in this file


Working with Branches

To see a list of all branches in the current repository type

git branch

To create a new local branch

git branch <branch-name>

If you want to switch to another branch you can use

git checkout <branch-name>

To create a new local branch and switch to it in one step

git checkout -b <branch-name>

To delete a branch, use

git branch -d <branch-name>

Push the new branch to the remote server

git push origin <branch-name>

Syncing a forked repository

So you’ve forked a repository and made some changes to it but the original repository has also been updated. You’d like to sync those changes from the original repo into your fork without losing the changes you’ve made.

Start by adding a remote called ‘upstream’ that points to the original repo

git add remote upstreamĀ

Then fetch all the branches from the upstream remote

get fetch upstream

First make sure you are in the master branch, then merge the upstream master into our own master

git checkout master
get merge upstream/master

Then your local master should updated with the changes from the original repo. The final step is to push the merged copy back to the server by simply doing

get push


If you have any branches, you need to rebase them to your master like this

git checkout <branch name>
git rebase master

Ignoring files

If your project depends on other projects and you’re using composer to autoload them, you might want to use a .gitignore file so your dependency files are not included in your project. Just create a .gitignore file in the root of your project and add these 2 lines




Add a Comment

Your email address will not be published. Required fields are marked *