Nihilarian
Nihilarian

Reputation: 1198

how to discard a branch and bring the changes from master

Our current workflow is such that every Monday morning, we want to discard all the commits merged to temp branch and mirror it with the latest version of master branch.

However, over the entire week, there would be commits made by different developers to the temp branch which would be pushed to Gitlab. We would like to reset the temp branch completely and bring it back to way master branch.

Upvotes: 0

Views: 168

Answers (1)

TTT
TTT

Reputation: 28964

There are multiple ways to accomplish this. Here are two fairly straight forward methods: (note these assume your remote name is "origin"):

git fetch                           # make sure your remote copy of master is updated
git checkout temp                   # checkout the temp branch if not already
git reset --hard origin/master      # reset temp to remote master
git push --force                    # push out the new temp branch to origin/temp

You can collapse the middle two commands into one if you prefer:

git fetch                           # make sure your remote copy of master is updated
git checkout -B temp origin/master  # checkout/create temp and reset to origin/master
git push --force                    # push out the new temp branch to origin/temp

Side note: When force pushing, I almost always recommend using git push --force-with-lease so you don't accidentally blow away commits on the remote that you don't know about. However, I think this is one of the rare cases where you actually do want to use git push --force since it sounds like you want to reset the temp branch no matter what; even if someone pushed a new commit to that branch a few minutes ago.

Another Side Note: The Git repo itself uses a branching strategy called Gitworkflows, which, like your strategy, has a throw-away branch that is used for integration testing, and is periodically reset. The Git maintainers call that branch next, which represents candidate features for the "next" release. (There's even a second level throw-away branch called pu for "proposed updates".) Just thought you'd be interested in knowing that you are using a(n arguably) tried and true strategy, at least regarding your temp branch.

Upvotes: 1

Related Questions