Basic Git commands

Git is the version management system by excellence currently. You can find complete information on the official website: http://git-scm.com.

The aim of this article is to provide a basic Git commands reference, in the end, are what you will use the 95% of times. Here’s the repertoire:

Clone repository:

git clone repository

You will use this command if you want to copy an existing online repository.

Start project:

git init

If what you want is to start a new project, is as simple as.

Add files:

git add .

This is the first step when you want to save changes to your project.

Add files and remove files deleted:

git add -A

This command is the same as the previous, but it also eliminates files that you have deleted.

Add specific files:

git add file1 file2

If what you want is to save one or more specific files, use this command.

Do commit:

git commit -m "message"

This is the next step. It stores in the repository the files added by the command add.

Create remote repository:

git remote add origin https://xxx@bitbucket.org/xxx/repositorio.git
git push -u origin --all

Finally, you will probably want to keep your repository on the network. Github for BitBucket are the most commonly used websites. If you want private free repositories will have to go with Bitbucket.

First you have to enter the website and create a repository. Then, copy the link to the repository and copy it in the previous line by replacing https://xxx@bitbucket.org/xxx/repositorio.git. Now the local repository (on your machine) and the remote repository (on the web) are linked.

Whenever you want to save the changes made by commit use the command of the second line (push).

Create new branch and position in it:

git checkout -b new_branch

Create a new branch can be useful to modify a stable software that we want to preserve, or to work several people on the same repository (each person creates a branch where to work). You will find more information on the official website.

Merge branch with main branch (master):

git checkout master
git merge rama_a_fusionar

Once you are finished on that branch, the logical thing is to merge it with the main branch (is not always the case). This is done by positioning in the master branch and merging it with the branch to merge.

See all the commits:

git log

At any time you can see all the made commits.

Differences between previous commit and now:

git diff

Differences between any commits:

git diff hash_commit1 hash_commit2

When you do git log, you can see in each commit an associated number. This is the hash.

Positioning in a commit:

git checkout hash_commit

At any time you can return to a previous commit.

And here the commands that I use. For more information you can see on the official website of Git: http://git-scm.com.

Leave a Reply

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