zok
zok

Reputation: 7882

Understanding git branching

I have a newbie question about Git.

The scenario:

1) I create an "experiment" branch in a project

2) Then I decide to do a change (ex: add files to .gitignore) that must affect the "experiment" branch as well as the master branch or any future branch I would branch from the master branch.

What is recommended on that situation?

Checkout the latest commit on the master branch before branching and commit with --amend?

Create a branch to do just that and then merge with master and the experiment branch? When I tried to do this, I applied all that was on my "experiment" branch to the master branch and lost a whole lot of files. I'm very confused now... any help will be greatly appreciated

update

What happened (and I could not explain in my last paragraph above):

I actually had merged the 'hotfix' branch (which had the .gitignore commited) into the 'experiment' branch and then merged it from master (on the 'experiment' branch I had deleted a bunch of files).

So I used:

git checkout master
git reset —hard [hash for the previously last commit on master]
git branch -d hotfix
git checkout -b hotfix (now from master)
git add .gitignore
git commit -m “add .gitignore”
git checkout master
git merge hotfix
git checkout experiment
git merge hotfix
git branch -d hotfix

Then I kind of 'undid' that and did it again using the rebase method, from one of the answers below. It appealed more to me because of the 'history rewriting' thing, which makes more sense to me by looking to the branch graph. And i'm working alone (so far) on this project.

Upvotes: 1

Views: 95

Answers (2)

gabra
gabra

Reputation: 10544

You can use rebase after commiting the new .gitignore to the master branch.

git checkout master git add .gitignore git commit -m 'Updates ignore list' git checkout experiment git rebase master

This will update the experiment branch.

But, please, only do this if you are the only person working on this branch. If someone else is working as well, it will give conflict issues. In that case, I would merge the master branch in the experimentone.

Upvotes: 1

Guardian667
Guardian667

Reputation: 452

Don't know if I understand the content of your last paragraph right. Have you surley merged in the right directions? Following procedure should work

git branch MaintainGitIgnore
git checkout MaintainGitIgnore

//manipulate .gitignore file
git commit -a .gitignore -m "Ignoring new files now"

git checkout master
git merge MaitainGitIgnore

git checkout experiment
git merge MaintainGitIgnore

You can now keep the branch MaintainGitIgnore, and maybe use a script to automate updating you ignore definition.

Upvotes: 1

Related Questions