Using the basic GIT command line on Bitbucket.

Some people don’t know how to start using Git. Now I write down some steps to show you how to use Git on your project with the basic understanding.

Step 1: Clone source from the repository.

I know two ways to clone a source code from git:

1.HTTPS:

git clone https://<username>@bitbucket.org/<username>/<repository>.git

If you clone the source code by https method. Every time, you push or pull code you have to provide username and password.

2.SSH

git clone [email protected]<username>/<repository>.git

To follow this way, you have to generate two ssh keys: a private key in your local PC and a public key added to your repository.

If you have just copped these command above and run them. Git will create a folder as a repository name in the current folder.

Example, Git will create a folder has a name like “nguyenhainam” in the current folder “/var/www/html” if we run:

git clone [email protected]<username>/nguyenhainam.git

If you don’t want Git to create the nguyenhainam folder in “/var/www/html “, you just added “.” after command line like this:

git clone [email protected]<username>/nguyenhainam.git .

After the clone, you need update all the remote-tracking branches, use

git fetch

And in addition merge the remote master branch into the current master branch, use

git pull

Step 2: Working with branches

List both remote-tracking branches and local branches.

git branch -a 

To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

git checkout <branch>

If you don’t want to working on default branch as master branch. You can create your own branch. e.g: develop

git checkout -b <new_branch>

After created <new_branch>, this branch has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin <new_branch>

In some cases, you don’t want to either use this branch any more or this branch can’t fix because It was conflicted, you need to delete <branch>, use

git branch -d <branch>

But remember that that command above is only deleted local branch. If you want to delete remote branch, use

git push origin :<branch>

Step 3: Working with changes

You added the new files or modified some files. Now you want to push them to a branch. Before you want to do some changes, make sure that you are checkout correctly branch which you want to work in.

The command displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git (and are not ignored by gitignore).

git status

The command shows changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk.

git diff

First, Add file contents to the index

git add <file 1> <file 2>

The command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. It typically adds the current content of existing paths as a whole, but with some options, it can also be used to add content with only part of the changes made to the working tree files applied or remove paths that do not exist in the working tree anymore.

Second, Record changes to the repository. The command stores the current contents of the index in a new commit along with a log messages from the user describing the changes

git commit -m <msg>

Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

Finally, Update remote refs along with associated objects

git push

Updates remote refs using local refs, while sending objects necessary to complete the given refs.