Git Commands Developers Should Know

You will find loads of tools out there to help with workflow within a team, but one that has been widely adopted in the development industry is Git.

In this tutorial, we are going to see some Git commands that every developer should know in order to be able to contribute to a project on Github or Bitbucket. We will be using Git Bash.

Adding Existing Project To New Repository

You may need to add an existing project, you’ve been working on your local machine, to a new repository. That’s how to do it:

1. First, in your Ternimal, go to the root directory of your project (example on Windows: cd d:xampp/htdocs/myproject). Then type the following:

git init

This will initialise the directory as a local repository by creating a hidden folder .git.

2. Now we want to get all the files ready to commit.

git add *

3. We can now make our first commit.

git commit –m "First commit"

4. At this point, you need to login to your GitHub or Bitbucket account and create a new repository. This will generate a URL for the remote repository which you will need for the next step.

5. Here we want to link the local repository to the new remote one.

git remote add origin https://github.com/repository-url.git

Make sure to replace https://github.com/repository-url.git with your remote repository url. Then verify the url with:

git remote –v

6. We can now push the project to the remote repository.

git push origin master

Updates were rejected

After going through the steps, you may get the following error message Updates were rejected because the remote contains work that you do not have locally. that’s because you need to first pull the files by running the Git command:

git pull -–allow-unrelated-histories origin master

This will create a merge and ask for a commit message. Type you message and press Ctrl + x and Ctrl + c (on Windows) to save and get out of the editor. Then type :wq and press Enter to go back to the prompt. Now you just need to use the git push command from step 6.

Not possible to fast-forward

You may also have the error Fatal: Not possible to fast-forward, aborting. To fix this you need to bring changes from the remote branch into yours with the following command:

git pull origin master --rebase

Remove File/Directory From Remote Repository Without Deleting On Local

The following Git commands are very useful if you want some files or folders to be removed from the remote repository but not on the local.

To remove a file run the command:

git rm --cached myfilename.txt

For a directory with all the files in it:

git rm --cached –r directoryname

After that, add all files and directory in the .gitignore file save and then run a commit and push.

git commit -m "Ignore files/folders"
git push origin master

Commit Changes To Repository

Of course, these are the Git commands you will be using the most as that’s what you will need whenever you make any changes to your project, and you want them to be pushed to the remote repository.

git add -A
git commit –m "Commit message"
git push origin master

Create And Merge Branches

Working on a different branch can be useful when you don’t want your changes to affect the master branch. For example, if you are working on a new feature, you want to create a new branch so your changes are isolated from the main one, and you have fully tested your code then you merge them together.

Create a new branch

First, we create a new branch. Here we call it new-branch but we can change that as long as you use only numbers, letters, hyphens and underscores without spaces.

git branch new-branch

After that, we want to to switch to the new branch using:

git checkout new-branch

And push the changes:

git push origin new-branch

Merge branches

When you are done with your new feature and are ready to merge then switch back to the master branch:

git checkout master

Then merge the branches and push the changes:

git merge new-branch
git push origin master

Fetch new branch

If you need to work on a branch that someone else has created, then you need to fetch it to your local repository first:

git fetch && checkout new-branch

This will fetch the new branch and switch you to it at the same time.