Git get latest changes before pushing a branch
Asked Answered
C

2

13

I have a local branch features that I based off of my local master to add some stuff in my code. I am now ready to merge those back into master , however, the state of the remote master branch has changed since then.

How can I get the latest version of master and then add my changes on top of that, so that I can then make a pull request to my remote master branch.

I read a couple of similar articles here, but all of them gave different answers and I got confused.

Should I first "switch" to my master branch, do a git pull and then somehow merge my features branch into it (resolving any conflicts), or is there another way.

Some articles pointed out the usage of git checkout branch features, but I am not sure what is the point in that. As far as I understand it git checkout just switches to a certain branch.

Can anyone point me in a correct direction as to how I might approach this. Again, I just need to get the latest changes of my remote master branch so that when I push my features branch I dont get a ton of conflicts.

Chatelain answered 31/10, 2017 at 7:11 Comment(0)
S
23
  1. Go to master branch with: git checkout master
  2. Update your local master with: git pull origin master
  3. Resolve conflicts (if applicable)
  4. Go back to features with: git checkout features
  5. Merge master branch over features with: git merge master
  6. Push changes with: git push origin features

Now, you have to create a new pull request from features to master. Done!

Spiritualty answered 31/10, 2017 at 7:21 Comment(4)
Thank you for the great explanation. One more question though - You say that I merge features with master. Would it be wrong if i do this the other way around - merge master into features. I am thinking of doing this, since when I create the pull request I want it to show that its from my features branch, not my master branch. This is not really an issue, I am just curious. Again, thanks for the wonderful answerChatelain
Yes, you are right. In fact, that's how I do it! I have edited the answer. Glad to help you.Infrangible
@Spiritualty Thanks for such a nice and clean explanation. And thank you Kobek for asking, I have had same problems as you understanding this.Hyperplasia
@Spiritualty I have a question though. I created features branch, added some changes, stage, committed and then did git push -u origin feature. Then I wanted to make sure my master is updated with remote master, so I switched to my master and did what you explained above. git pull origin master reported "All up to date". I checked out feature and did git status without merge and now I see all files (that I added/modified previously and pushed using -u to remote) as either "to be committed", or as "staged", or as "untracked". Why is this? Was something wrong in what I did?Hyperplasia
T
0

I took Hector's answer and added in some "automation" for Windows users;

For me I have ALL my individuals repos within a directory called "Workspace" So my filetree looks like this; c:\Users\Gavin\Workspace\App1 c:\Users\Gavin\Workspace\App2\repo1 c:\Users\Gavin\Workspace\App2\repo2

So the last directory of the full path is the REPO name, too.

rem Start in the app-root
cd "C:\<Full\Path\to\mySourceCodeDirectory>"

rem Echo to the screen where we are
echo Current Path: %cd%

rem Get and store the last portion of the FULL PATH - (The repo name)
for %%f in ("%cd%") do set "current_directory=%%~nxf"
echo Git repository: %current_directory%

rem Get and save the current branch    
for /F "delims=" %%n in ('git branch --show-current') do set "current_branch=%%n"    
echo Current branch: %current_branch%

echo ===================================================

rem Turn echo back on so we can see what commands match any returned messages
@echo on
git stash push
git checkout master
git pull origin master
git checkout %current_branch%
git merge master
git push
git stash pop
Trichomonad answered 2/4 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.