HOW TO: use Git to start a project and commit files

In this post, I will show you a few basic git commands that I use every day.

When using Bitbucket to create a new branch, you have two options to get started with the command line:

  1. I have an existing project
  2. I’m starting from scratch
bitbucket commands
bitbucket commands

I usually use the first option, because I already have developed something locally and I’m looking to commit my code into bitbucket. They only show you there two commands to get started with, but I always get an annoying error every time I try to follow their commands.

error: src refspec master does not match any.
error: failed to push some refs to '...'
Git error
Git error

Remember to read the Git documentation to better understand what commands you should use and why: https://git-scm.com/doc

How to install PHP, Apache and MySQL on Ubuntu

To avoid the following error follow the steps below:

  1. Do a ‘git init’ in your project directory.
    • git init command
      git init command
  2. Do ‘git remote add origin https://…’ (copy command from bitbucket)
    • git remote add
      git remote add
  3. Do ‘git add .’  adds all the content in your directory to a stage where they’ll be ready for the next commit.
    • git add
      git add
  4. Do ‘git commit -m “Add your message here.” ‘ this will commit the files you just added.
    • git commitgit commit
  5. Do ‘git push –set-upstream origin master’ to push your commit into the master branch in bitbucket.
    • git push
      git push

Your files are now committed to the master branch of your project, in bitbucket. This solved the error above. Optionally, follow the next steps to make sure your files have been committed and you check out your development branch.

Top 3 free coding editors

As a developer, when working for a production system or any project in general, you should follow the Git Work Flow. Please read more details about it here.

The general rules are:

  • master branch is for the production system (only work that has been tested and is ready to be deployed should be committed)
  • all the work should go into another branch, usually called ‘develop’ (develop should include tested new features, that are not ready to be deployed yet)
  • developers should work using feature branches; a new feature branch should be created for every new change/feature developed. When created, this feature branch should check out a copy of develop, not master branch. The feature branch should be committed back to the develop branch once is done. This offers more traceability within the project
  • every code change of the main branches (develop, master) should not be merged until a pull request has been approved by the team members
  • only when the develop branch is building correctly and it has been tested it can be merged into the master branch.

Commands:

  1. Do ‘git status’  to check the status of your files.
    • git status
      git status
  2. Do ‘git branch’ to check how many branches are available.
    • git branch
      git branch
  3. Go to bitbucket and create a new branch.
    • bitbucket new repo
      YourProject/Branches -> click the button “Create branch”
  4. Do ‘git fetch && git checkout develop’ . This will do a ‘refresh’, see what’s new and will then check out the develop branch.
    • git fetch and check out
      git fetch and check out

You should now be able to see that develop is the branch you are working into, instead of master:

git develop branch
git develop branch

In conclusion, using Git is really easy, you just need to read the documentation and understand what commands you need to use. The commands above are the ones most developers use on a daily basis. Remember to commit as much as possible or at least once a day, when you finished developing stuff.

How to solve – No ‘Access-Control-Allow-Origin’ header is present on the requested resource

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.