简体   繁体   中英

How to push code to repository from another branch

I have two branches on my repository: branch1 and branch2 (master is no needed here).

On branch1 I have my code, but there is whole project with not needed files, so I decided to create new branch, add .gitignore and include there directories and files that I don't need, then push it into branch2 to contains only src directory, gradle.build and .gitignore.

I use GitBash and do in order:

git checkout -b branch2 //to create branch
git add --all //to add all files (.gitignore should exclude no needed files)
git commit -m "message"
git push -u origin branch2

Then I enter my repository on bitbucket and there is a branch2 but with files which I don't want to have there. What am I doing wrong?

Thanks in advance.

Adding and pushing the .gitignore does not mean that the files to be ingored are going to be deleted automatically. They were committed before the branch2 was cut (on a branch1 commit), thus when you cut branch2 they were included.

You need to git-rm these files once, push to the remote repo, and then hopefully the will be git-ignored.

If files are already versioned adding a .gitignore won't remove them. You have to remove them from git

git rm --cached file //with file what you want

The gitignore will now prevent them to be add.

So do these step by step like these:

  1. Create a new branch2 with the same command: git checkout -b branch2

  2. Then remove all the files, each by running git rm using command : git rm . This way you'll be making it eligible to be deleted from branch2. Remember it's not affecting your branch1.

  3. Then commit and push your changes to branch2.

  4. Now just copy all those unwanted files from branch1 and add them to branch2.

  5. Now your gitignore will come into picture and will neither ask you to commit these files, nor will ask them to push these into remote repository. Any changes made to these files henceforth would be ignored.

files and paths listed in .gitignore means they will not be tracked even if they exist (or not), even if you create new branch, git won't care since they are ignored

.gitignore is a configuration file shared among branches

I think what you want is to delete ignored files and directories (listed in your .gitignore)

on branch1 or branch2: git clean -xfd 
on branch1 or branch2: git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM