Git commands part-2

Default featured post

Long time ago I have written a simple guideline for GitHub and how it works. In that post which is available from here, I have explained some basic Git commands. In this post I will explain more commands and trying to complete it later.

Git clone for cloning a repository

When you push your code to remote repository, if someone else wants to grab a copy of your project or even if you want to have a clone of your repository you should do Git clone. Check the below command,

$ git clone [email protected]:[RepositoryName]

Git branch

If you are working on a project which has different branches you can get list of all available branches with the following command,

$ git branch -a

Now if you want to switch to another branch use this,

$ git checkout [BranchName]

For creating a new branch from the current branch,

$ git checkout -b [NameOfNewBranch]

Now you need to push the new branch to remote repository,

$ git push origin [name_of_your_new_branch]

Git stash and git stash pop

Sometimes it happens when you either want to switch to another branch or do git pull you face with conflict. In this case you better to store your local changes somewhere and then do git pull or switch branch again. Git stash it is used to save aka stash current workplace and then your changes would be disappeared, however, you can return back your changes again with git stash pop.

$ git stash // It saves workplace
$ git stash pop //It returns whatever [latest] stashed

Git reset

If you add one file wrong or your files have conflict and you resolved the conflicts manually but still it shows conflicts and you cannot commit, the best way is to do git reset. Git reset put whatever file which in ready to commit stage or unmerege stage to not ready to commit stage which is quite useful.

$ git reset

Git cherry-pick

If you want to port some commit(s) from one branch to another branch(es), you can do git-cherrypick instead of committing again to different revision.

$ git cherry-pick [CommitNumber]

Git clean

Sometimes it happens that your work directory contains some unnecessary files which you do not need them and also not available in git remote for example, .class or .tmp files. In order to get rid of this kinds of files you can clean your working directly. You should keep in mind git clean removes all files which are not match and available in remote repository, therefore, if you have any files which is not avail in remote repo but you wanna commit it later, you should copy them somewhere else otherwise they would be removed automatically and will not possible to revert back.

$ git clean -xdf

Git tag

Git tag is very useful if you want to tag any bug fix or enhancement to your revision number for example, 1.2.01. Here 01 could be your tag number and for instance may represent number of bug fixes.

In order to know the latest tag just type,

$ git tag

The above command shows list of all tags.

Now for adding a new tag you should type,

$ git tag -a [TagName]

For removing any tag,

$ git tag -d [TagName]

Revert back to master branch/ Discard all changes

If you committed something wrong in your local git and you do not want to push it and revert back to master remote repository and discard all changes use the below command,

$ git reset --hard origin/master