Tuesday, March 18, 2014

Commit new changes to last commit

If you ever find that you accidentally left something out of your last commit, be it a file or an extra change to a file that you just committed, don't worry. It can easily be fixed All you have to do is stage the extra changes like you would for a normal commit:

git add .

And then just commit with the --amend argument.

git commit --amend

If you don't specify a commit message with -m you will be prompted with the previous commit message as a default. When you're done you can check git log --stat to see your amended commit with the extra changes.

Tuesday, March 11, 2014

Pull from and Push to the current branch

Following command will setup your git to pull the current branch on remote

git config --global pull.default current

Following command will setup your git to push into the current branch on remote

git config --global push.default current

Monday, March 10, 2014

Check what you are about to push with git?

For a list of files to be pushed, run:

$ git diff --stat [remote/branch]

example:

$ git diff --stat origin/master

For the code diff of the files to be pushed, run:

$ git diff [remote repo/branch]

To see full file paths of the files that will change, run:

$ git diff --numstat [remote repo/branch]

Share your repository with others

On your machine

Install SSH: Make sure that openshh server is installed, otherwise other developer will not be able to pull code from your machine.

sudo apt-get install openssh-server openssh-client

Create a new user: This is necessary if you don't want to share your password with others.

sudo useradd -m USER_NAME

Example:

sudo useradd -m guru

Setting the password for newly created user: Other developer will use this password to get your code.

sudo passwd USER_NAME

Example:

sudo passwd guru

Create a group: If you have created a new user, then this is necessary to create a group.

sudo groupadd GROUP_NAME

Example:

sudo groupadd developers

Add new user to this group: Add your own user and above created user to this new group. Why? wait.

sudo usermod -G GROUP_NAME USER_NAME

Repeat above command to add your own user_name to this group. Example:

sudo usermod -G developers guru

Change the ownership of repository directory: Now the new user and your own user will have rights to read the content of the repository, that's why we create a group.

sudo chgrp GROUP_NAME REPO_DIR

Example:

sudo chgrp developers ~/MyProject

On another machine

Add remote: Add a new remote location to your existing repository which was cloned from a common repository online e.g. github

git remote add REMOTE_NAME ssh://@/path/to/repo_dir

Example:

git remote add buddy ssh://guru@192.168.1.33/home/sumit/MyProject

To see the list of remotes: Verify new remote is successfully added.

git remote -v

To see the list of branches on remote

git ls-remote --heads REMOTE_NAME

Example:

git ls-remote --heads buddy

Pull remote branch

git pull REMOTE_NAME BRANCH_NAME

Example:

git pull buddy new_feature