Friday, April 4, 2014

How to create a new branch for a previous commit in github

First clone the repo:

malintha@malintha-Dell:~/istimetable$ git clone https://github.com/ghmalithi/istimetable
Cloning into 'istimetable'...
remote: Reusing existing pack: 79, done.
remote: Total 79 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (79/79), done.
malintha@malintha-Dell:~/istimetable$


Create a branch called ”malib” in the commit id : 41145eadbfeaf111722ede3c10fe857c284

malintha@malintha-Dell:~/istimetable/istimetable$ git branch malib 41145eadbfeaf111722ede3c10fe857c284e47c2

Switch to the branch:

malintha@malintha-Dell:~/istimetable/istimetable$ git checkout malib
Switched to branch 'malib'
malintha@malintha-Dell:~/istimetable/istimetable$


Commit here the changes:

malintha@malintha-Dell:~/istimetable/istimetable$ git add .
malintha@malintha-Dell:~/istimetable/istimetable$ git status
# On branch malib
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: readme.txt
#
malintha@malintha-Dell:~/istimetable/istimetable$ git commit -m "adding to other branch"
[malib e986491] adding to other branch
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
malintha@malintha-Dell:~/istimetable/istimetable$

Push

Use the branch name as newly created branch.

malintha@malintha-Dell:~/istimetable/istimetable$ git push origin malib
Username for 'https://github.com': ghmalithi
Password for 'https://ghmalithi@github.com':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/ghmalithi/istimetable
 * [new branch]      malib -> malib

Now goto github url. You will see the new branch is added at the commit you previously specified.