Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: about to fork my own project

Tags:

git

branch

I am a very basic git user; I basically only use the commands git add -a (to add files after modifying them) and then git commit -m "what I did" and then git push (to push it to my server).

Now I want to do something very drastic to my codebase. Something that might possibly not work -- and I will have to revert back to where I was.

How do I do that? Do I create a "branch"? Or a tag? Or something else?

I am after a set of simple commands with simple explanations. also to then return to the original HEAD, or to then (maybe) merge the changes into HEAD (which won't have changed)

Merc.

like image 706
Merc Avatar asked Jan 14 '23 18:01

Merc


1 Answers

You need a branch.

git checkout -b nameofyournewbranch

This checks out your current branch (default name is 'master') as a new branch with the name you specify. Any commits you do now will be on the new branch. If you want to leave it and go back to where you were:

git checkout master

to go back to your new branch:

git checkout nameofyournewbranch

if you want to merge your branch into your main codebase:

git checkout master
git merge --no-ff nameofyourbranch 

--no-ff means you will see it branch out of master and then branch back in, allowing you to keep track of separate features.

To see what you're doing, get a git source tree viewer of some sort e.g. source tree, gitk etc. Much easier to understand what the branches are doing if you can see them visually.

Update: To check what would happen before merging, make a temporary branch in the same place as master and merge to there:

git checkout -b tempbranchname master
git merge --no-ff nameofyourbranch

If you like it, merge it to master and it'll be as if you did it on master in the first place:

git checkout master
git merge tempbranchname

If you don't, just delete it:

git checkout master
git branch -D tempbranchname 

-D means delete even if not merged to master. Use -d normally as you'll get a warning if you try to delete unmerged stuff that you would lose.

like image 156
Matt Gibson Avatar answered Jan 25 '23 09:01

Matt Gibson