Archive

Archive for the ‘GIT’ Category

GIT merge or squash 2 or more commits into 1

October 1, 2015 Leave a comment

git lets you squash 2 or more commits into 1 commit before pushing to upstream

Scenario:

you have a git repo
you make some changes and do a commit with message: commit 1
you make some more changes and do another commit with message: commit 2
you can then merge these 2 commits into 1 single commit before pushing your changes upstream
here is the concept on how to do it

My Work Environment

  • Windows 7 – 64 bit
  • git version 1.9.5.msysgit.0
  • git bash installed
  • notepad++ editor installed ( i will be using this as my default editor )

open gitbash

change your default editor to notepad++
this is for Windows 7 64 bit – make sure you have the correct path of the editor for your machine

git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

for testing – create a folder in your file system
open gitk and cd into the folder
initialize the test git repo

git init

create a file 1.txt
do the first commit on master

git add .
git commit -m "commit 1"

create a branch from master and checkout development

git branch development
git checkout development

create a file 2.txt and commit to development

git add .
git commit -m "commit 2"

create a file 3.txt and commit to development

git add .
git commit -m "commit 3"

at this point gitk should show you 3 commits on development branch
we will squash commit 2 and commit 3 into 1 commit with a final commit message

the HEAD~2 is for last 2 commits, for last 3 commits HEAD~3

git rebase -i HEAD~2

this will open up notepad++
you will see 2 commits at the top
pick ebbea73 commit 2
pick fd2055f commit 3

the first word is important: pick
you will keep the first commit the same ( pick ) and change all commits after that to an s ( for squash )
so the file will look like ( the commit hash will be different – this is just an example )

pick ebbea73 commit 2
s fd2055f commit 3

lets say you had 4 commits, then your file would look like ( s = squash at the beginning )

pick ebbea73 commit 2
s fd2055f commit 3
s eccea73 commit 4
s ftt055f commit 5

save and close the file
git should say rebasing and open another window for the commit message
this will be used as the commit message for the squashed commit
you can comment out ( with a hash # ) the first 2 commit messages and add your new commit message at the bottom
the file will look like this ( note that commit 2 and commit 3 messages have been commented out with a # )

# This is a combination of 2 commits.
# The first commit's message is:
#commit 2
# This is the 2nd commit message:
#commit 3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 7500cac
# You are currently editing a commit while rebasing branch 'development' on '7500cac'.
#
# Changes to be committed:
# new file: 2.txt
# new file: 3.txt
#
THIS IS MY FINAL COMMIT MESSAGE

save and close
git should say successfully rebased and updated refs/heads/development
gitk should show the new commit message and only 2 commits

Categories: GIT Tags: , ,

GIT – Ignoring files

November 21, 2012 Leave a comment

there are 2 ways to ignore files in git – these files will not be tracked. If you use an IDE, it will create some settings of its own inside your git directory. If they are not ignored, then they will be pushed to your remote server and will be pulled when someone pulls from your repo.

Ignoring Files withe  a global config file

Just create a global config file and place it anywhere you like ( recommended is to put it in your home directory )

I am going to create it in this dir – /home/Desktop/naveen/GIT

cd /home/Desktop/naveen/GIT
touch .gitignore_global

put all the rules you want in here to ignore files

i typically ignore all hidden files with these 2 lines in the file

.*
!/.gitignore

To see how rules work and samples refer to these links

Next we need to add it to gits global config using this command

please give the full path name to the global ignore file

git config --global core.excludesfile  /home/Desktop/naveen/GIT/.gitignore_global

Ignoring Files from every Repo  

if you want to exclude different files per Repo, then all you need to do is create a file called .gitignore and put it in whatever path you want inside your Repo

 

Categories: GIT

Some daily and useful GIT commands

August 28, 2011 Leave a comment

Initial Steps ( this is for creating a PUBLIC git repository on GitHub : anyone can have access to this )

  • As suggested this post is for setting up a PUBLIC git repository on GitHub ( and local one on Ubuntu )
  • Anyone will have access to this repo
  • Please do not put sensitive data ( like passwords etc ) in this project 
  • If you need a private repo, you can use BitBucket
  • Go to GitHub ( https://github.com/ ) and create a regular account
  • There is a very nice tutorial ( http://help.github.com/win-set-up-git/ ) on how to set up a git repo on your local machine – so i am not repeating it here
  • If you face problems with SSH keys, then you can try using DSA instead of RSA
  • In Linux dont forget to do this in case SSH to GitHub is not working cd ~/.ssh; $ ssh-add id_rsa
  • There is a post on Stack overflow ( http://stackoverflow.com/questions/922210/unable-to-git-push-master-to-github ) for SSH related problems
  • Once you install Git on your local system, you can also install a GUI like Smart Git
  • After copying all the public keys and stuff from your local system to GitHub, you can test to SSH to GitHub from your local system
  • GitHub dose not allow SSH access, but you can see if you are successful or not
  • Use the command ssh -vT git@github.com 
  • The end of the message will say something like “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”
  • If there is a problem, then you can see the verbose log of what SSH was trying to do and refer to Step 9
  • Use the “New Repository” button in you Dashboard to create a new repo ( Lets name it “test” and put the description as “test-repo”)
  • When you finish creating a new repo, GitHub will give you some nice instructions to follow on your local machine, follow them –
    Note – i have a directory called GIT on my desktop and all my projects are located there
Global setup:
 Set up git
  git config --global user.name "Your Name"
  git config --global user.email your-email@gmail.com

Next steps:
  mkdir test
  cd test
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin git@github.com:username/test.git
  git push -u origin master
  • Basically you are creating a README file and pushing it to GitHub ( my folder structure is /home/naveen/Desktop/GIT/test )
  • After these steps you should be able to see README in your GitHub account under repo “test” with the message “First Commit”
  • Now lets add another file to “test”
touch index.php
echo "<?php echo 'Hello World';" > index.php
git add index.php
git push -u origin master
  • You should now be able to see index.php in your GitHub account
  • Lets make some changes to index.php
echo "echo 'Hello World - 1';" >> index.php
git diff
  • git diff will show you the differences
git add index.php
git commit
  • When you use git commit instead of git commit -m ‘your message’ ,  a vim editor opens up by default and you can see what all changes have been done
  • You will have to type a commit message and exit
  • Type the message “added hello world 1” on the first line , then press escape , then press 😡 and hit enter – you will see the commit message
  • This is better way to see all files that have changed rather than just commit with a direct message
  • To change your editor from vim to nano use the command
git config --global core.editor "nano"
git push -u origin master
  • Changes should be  reflected on GitHub
  • To remove  an entire git repo, all you need to do is to delete the .git folder in your repository then you can do an init for a fresh start
  • Next how to clone an existing PUBLIC repository from Github
  • First we will delete our existing “test ” repo on our local machine, then we will clone the “test” repo on GitHub that we just created ( take care where you execute rm -rf and always keep a backup of your old git repos )
rm -rf .git
rm index.php
rm README
cd ..
rmdir test
git clone git://github.com/username/test.git
  • You will now get back the test repo on GitHub – you can make modifications and repeat the cycle if necessary
  • To add some color to your command line interface use the below commands
git config --global color.status auto
git config --global color.branch auto
  • Change remote-origin with this command ( github does not allow git protocol – you might get an error while pushing )
git remote set-url origin git@github.com:username/project_name.git
  • To show all files in a branch
git ls-tree -r branch
  • To compare the same file from 2 branches
git diff branch_1..branch_2 file.txt
  • To find if you need to pull updates from remote branch
git remote update
git status

 

  • You can create .gitignore file in your project or globally to ignore some files
  • For global gitignore, create a file somewhere. Eg /home/naveen/.gitignore
  • Now set the global settings with the below command –
git config --global core.excludesfile /path-to-gitignore-file

 

Good tutorials and references at http://gitref.org

I will update the post as an when i learn more 🙂

Categories: GIT Tags: