Sep.10

Git Useful Commands

There are Git Command line which are use full for developer.

Git Initialization:

Additionally, Git does not require any pre-existing server or admin privileges. All you have to do is cd into your project subdirectory and run git init, and you’ll have a fully functional Git repository.

$ git init

We can set up repository on Remote Git servers like git-hub, git-lab, bit bucket etc.

Define Remote address:

$ git remote add origin https://github.com/PrakashKumarBhandari/projectname.git

Then push your project file into remote.

$ git push -u origin master
$ git config --global user.name "Prakash Bhandari"
$ git config --global user.email"akash2046@gmail.com"
Add Username and Email for sync remote and local data.

Show the remote path or git which is cloned or added origin::

$ git remote show origin

We can use Git clone command if we have already exiting project.

Sometime we get error on pulling local file to origin. error like that

the solution would be ::

The error is resolved by toggling the allow-unrelated-histories switch. After a git pull or git merge command, add the following tag:

git pull origin master --allow-unrelated-histories

$git clone <project_url>

Show all Local or Remote Branch

-a shows all local and remote branches, while -r shows only remote branches.

$ git branch   [ Show all local Branch]
$ git branch -a   [ Remote and Local Branch]
$ git branch -r   [ Show all remote Branch]

How Does Git Checkout Remote Branch Work?

Git checkout remote branch lets us switch to (and work on) a remote branch, just like we’d switch to a local one. There are a couple of ways to do this.

First, fetch the remote branches:

git fetch origin

Next, checkout the branch you want. In this case, the branch we want is called “branchxyz”.

git checkout -b branchxyz origin/branchxyz

Or you can use:

git branch branchxyz origin/branchxyz

With newer versions, you can simply use:

$git fetch

$git checkout branchxyz

Rename a Branch

To rename a branch, run the command:

$git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME

# Alternative
$git branch --move OLD-BRANCH-NAME NEW-BRANCH-NAME

Delete a Branch

Git won’t let you delete a branch that you’re currently on. You first need to checkout a different branch, then run the command:

$git branch -d BRANCH-TO-DELETE

# Alternative:
$git branch --delete BRANCH-TO-DELETE

Compare Branches

You can compare branches with the git diff command:

$git diff FIRST-BRANCH..SECOND-BRANCH
Share this Story:
  • facebook
  • twitter
  • gplus

Leave a comment

Facebook Comment