Merge (squash) multiple git commits into one

Git-Logo

How to combine multiple git commits as one commit?

 

Step 1:

 

git rebase -i HEAD~n # n is the number of commits you want to merge.

-i means interactive.

Step 2: You will see interactive git screen:

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
# Rebase 49687a0..d426a8a onto 49687a0

# # Commands:
# p, pick = use commit 
# r, reword = use commit, but edit the commit message 
# e, edit = use commit, but stop for amending 
# s, squash = use commit, but meld into previous commit 
# f, fixup = like "squash", but discard this commit's log message 
# x, exec = run command (the rest of the line) using shell 

# # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. #

 

Step 3: 

To merge all commit to one we can edit above content as  following, we can also reorder commits by moving them around:

squash c01a668 commit 3
pick fda59df commit 1
squash x536897 commit 2

Don’t need to work on each commit as they will be ignored. After saving the squash settings, your editor will open once more to ask for a commit message for the squashed commit.

 

You can cancel rebase any time by:

git rebase --abort

 

Advertisements

Git : Deploy local site to remote production live server by git push

This tutorial will help you to create git repo in remote to deploy your local work master branch  to live site by git push.  Lets us assume some scenario to make this tutorial easier.
Scenario:

Your website root remote  path:  /home/username/

Git repo path to be created in remote: /home/git/

 

Create ssh keys to access your server, as from git we only do push by ssh. To do this you need to generate private and public key. There are many ways you can create private and public keys. Please read this article to create one.

So lets assume you have create private key and public key as:

 

git_rsa
git_rsa.pub

 

Procedure Remote:

Create Remote git :

Setp 1: Setup git in your remote server.

$ Yum install git

Step 2: Setup bare git repo.

lets create a user called “git”

 $ useradd -m git

by default, every server has default location for user which is /home/<username>. So go to git folder.

$ cd /home/git

Step 3: Add ssh details.

$ mkdir .ssh
vi authorized_keys

Copy paste your public key git_rsa.pub here then save exit. by command wq.

 

Step 4: Create git bare repo for example mysite.git inside your username “git” folder.

$ mkdir mysite.git
$ cd mysite.git
$ git init --bare

Above command will create a new folder called mysite.git and initiate bare repo inside it, so full path will be /home/git/mysite.git
you can create multiple git repo inside git folder which you can access anytime.

 

Step 5: Create hook to update main site: go to hook folder of your repo. ( /home/git/mysite.git/hooks)

$ cd hooks

create post-receive hook

$ vi post-receive

and copy and paste

#!/bin/sh
GIT_WORK_TREE=/home/username git checkout -f

save and exit by wq in vim.


Step 6: Setup correct permission.

Now you git repo is stepup but need to give correct permission to your git repo and also need to give access to your website to be access by git .

a) Go to your git folder.

cd /home/git
chown -R git:git mysite.git
chmod -R ug+x mysite.git

This will change the ownership of your git repo to the git user and git group.

b) Give permission to your website root folder to be access by git user. So add git as the user in your website user group. So mostly <username> of your website is also the group so lets add git user to that group.

usermod -a -G username git

Procedure Local:

Step 1: Setup local working directory with git.

Go to your local working folder, if you dont have any repo you can create by opening git shell in your working directory and typing

 

git init.

Step 2:  Setup ssh for the repo.

We need to add our private key to git ssh otherwise push to the remote server will not work. so in your git shell do the following:

ssh-add /your/path/git_rsa

Check whether you have correclty added your key or not.

ssh-add -L

This will show you your key. NOTE: form window machine, some time ssh-add will not work. You might get error like “Could not open a connection to your authentication agent.” so to fix this problem go to your repo in your local window machine. and then do the following

$ eval `ssh-agent -s`  

OR

$ eval `ssh-agent -c`

Then you can do ssh-add.

 

Step 3: Create remote for your repo by:

$ git remote add live ssh://myserver.com/home/git/mysite.git

Initiate your first push.

$ git push live +master:refs/heads/master


Above will push your first master to your live server. for further updates you can simply do:

$ git push live

And you are done. Enjoy:

opt/git