Sunday, April 30, 2017

Overriding a remote branch in git

If you need to override a remote branch with local changes, then you can use:

git push -f


This is very useful whenever you need to discard the changes that you have made and start over.

Thursday, April 27, 2017

Creating a branch



You can create a branch using:

git checkout <branch_name>

This will create a branch from current working branch. You can check your current working branch by running:
git branch

If you have multiple branches your current branch is the one which is marked with * at the beginning.

If you want to create a branch from some other existing branch then you can do:

git checkout -b <branch_name> <from_branch_name>

This will create a new branch from the given branch.

The advantage of creating branch is you can work on your changes without affecting other people who are also making changes.
Once you are done with your work then you can merge it back to the main branch. The main branch in git is referrred to as master.

Initializing git repository and adding files



Initialize an empty git repository in the current folder.
git init .

Add files in the current folder to git so that it can track it.
git add .

Commit all the files that have been modified or added.
git commit -m "My first commit"

If you want to add and commit together, you can use -am option as shown below.
git commit -a -m <message>

But remember that this option only works for files already tracked by git not for newly added files.

Now whenever you modify files and want to add it to the git repository, you have to add the files using git add and commit using git commit.

So far you are able to create repository locally but in most practical circumstances repository resides in a server.
In order to push yur code to the server, you have to first add the server using following git command:

git remote add <name of the remote> <url>

After this you can do:

git push

to publish your changes to the server.

Configuring git



Before getting started with git, set the email address so that your contribution can be identified in git version history.

git config --global --add user.email <your email>

Set the name so that it can be identified in git version history.
git config --global --add user.name  <your user name>

Once you set the above configuration, then your information will appear in git history as shown below:

commit 6e523791f2fde1581d3d42b2fc4f84a0a1d0d9df
Author: Your Name <your_name@gmail.com>
Date:   Wed Apr 26 15:09:02 2017 -0700

    Some comment.


If you want to see all the configurations that are set currently, you can do:
git config --list

There are lot of other configurations that can be set but these two are most important ones to get you going.

Overriding a remote branch in git

If you need to override a remote branch with local changes, then you can use: git push -f This is very useful whenever you need to disc...