简体   繁体   中英

How do I commit my saved files on GitHub to a different branch?

Currently, the branch that I can commit saved files to is master .

However, I want to commit my saved files to a different branch called "First-Level" (I'm designing a game). But I haven't managed to make it work for now.

It will only continue to commit to the master branch and not the First-Level branch. What should I do?

To commit on another branch

Whether or not you already commited on the master branch, just create now a new branch and commit there :

# quite litteraly "checkout a new branch called First-Level"
git checkout -b First-Level

# at previous step, as Arjun rightfully point out, omit -b if the branch exists

# add every change and commit (omit the "a" if you already staged specific changes)
git commit -am "Modified stuff in order to achieve a goal"

(...........if you never commited to your local master , you're good here :-)


To fix unwanted commits on master

After having commited on the new branch like we saw above, check your local history with git log to locate the commits to "erase" from master. No worries though, they'll be safe on your First-Level branch.

(If you know you have only to go back a few commits, no need to let git output a long unneeded history, cut it short with -n , for n commits back)

I'll assume the following output with -3 , but adapt to your case of course :

commit 287a91bd66781331c996067bd09521ea0880be5f (HEAD -> First-Level, master)
Author: serendipity456 <someone@somewh.ere>
Date:   Wed Feb 27 02:07:10 2019 +0100

    Modified stuff in order to achieve a goal

commit 982ee15f5ec26818f9a563c3c7dd52041258669d
Author: serendipity456 <someone@somewh.ere>
Date:   Sat Feb 23 15:17:52 2019 +0100

    Changed a bunch of things in places

commit d9ad330f8acaaf4fdc0785b699efe2aa527507fe  (HEAD -> origin/master)
Author: serendipity456 <someone@somewh.ere>
Date:   Sat Feb 23 15:17:52 2019 +0100

    Solid foundations for the game

If at this point you identify let's say the last commit, d9ad330 (where the remote* still points to) to be the one you want master to stay at, proceed as following :

git checkout master
git reset --hard d9ad330

* (here in my example, but you didn't mention if you used one)

If you already have a new branch created, directly use

git checkout branchname

Else, to move to another branch, use

git checkout -b branchname

To check the branches, use git branch

I always use Git Extension with some awesome Git features we can see in friendly GUI. You can create new branch in your local repository, then you can push your branch. Once a new branch is created, then you can choose the branch and start do anything in that branch (commit, pull, push). Hope it helps!

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