How to merge from upstream branch? - windows

Here is my git setup (we use Git + Atlassian Stash in our corporate network):
upstream:
master
origin (my fork of 'upstream'):
master
branch1 (branch of master, with a few commits on top of it)
clone (local; clone of 'origin'):
master
branch1 (ahead of 'origin:branch1' by 1 commit)
What I want to do:
I want to merge upstream:master -> clone:branch1. I know there will be conflicts with this merge (since I changed files in my branch1 that others have changed in upstream). Once this is done I hope to push my changes back to origin:branch1, which will include my 1 commit + latest base from upstream (I want to keep up to date with the master branch, since that's the one I branched from). Along with this I want it to be a rebase, so that the commit history is clean and doesn't spider-web all over the place.
Another note is that I do not use git command line directly. On Windows, I'm using SmartGit, so if anyone knows instructions for that tool that would be most ideal.
How can I properly merge like I have described above?

If no one else has cloned or is working with branch1, you can rebase it on top of master, once you have updated master to upstream/master.
First, fetch upstream (SmartGit: Remote/Pull, select "Fetch Only")
Then reset master to upstream/master (SmartGit: Local/Reset)
Now rebase branch1 on top of master (SmartGit: In the Branches view, you can right-click on a branch like master and select Rebase HEAD to rebase your current HEAD onto the selected branch master)
Resolve merge conflicts if necessary.
Finally push (force the push) of branch1 to origin (SmartGit: In the context menu of the Branches view, you can invoke Push and Push To on local branches).

Related

GIT : How to get latest changes from master branch to the custom branch using git commands?

Branch "abcd/child" is created from "abcd/master" . Changes are made to "abcd/child" and meanwhile "abcd/master" also have added changes. Now how to make sure the latest changes are pulled from "abcd/master" is available in "abcd/child" using git commands in git bash?
Assuming abcd is the name of your remote, here's how I'd do it:
git checkout child
git pull
git merge abcd/master
git push
When you checkout child, it'll probably say "set up to track remote abcd" or similar.
The git pull command does two things: It fetches all updates from the server (on all branches) to your local git repo, and it updates your branch to match what's on the remote.
The git merge abcd\master means to specifically bring in all of the commits that are on the remote's copy of the master branch. That's important because you may not have updated your local master to have all those commits.
Note also that you might get conflicts in the git merge if both abcd and master have edited the same sections of the same file. There's lots of help in resolving git merge conflicts.
Also: You want to make sure everything works after the merge. It's possible that the changes on master broke an API that you were using, so you may need to make edits to deal with that.
Update: My assumption about abcd being the name of the remote is wrong.
First, get the name of your remote with the git remote command. Mine goes like this.
git remote
origin
So I only have one remote, and I call it origin. That's pretty common. If you've got more than one remote, it'll be trickier.
So, with "origin" as the remote it goes like this:
git checkout abcd/child
git pull
git merge origin/abcd/master
git push
Obviously substitute the name of your remote if it's not origin.
Same caveats apply about conflicts and making sure it works.

The local repository is out of date when push the code even after pull is completed

1.Pull the latest code[
2.Commit the new code
3. When tried to push the code to bitbucket repository getting message "Local repository out of date[![enter image description here]
**
tried bothway Xcode push and cmd based push but no luck still getting this weird message
Note:
Xcode 12.4,
bit bucket
I don't understand, the image shows after the "pull"? If not, please edit your question and place the image in the correct place.
That message can be because someone else have pushed to your repo after the last time you pulled from it. You will need to revert all the X commits that you have done after your last succed push (save the work in another branch or whatever):
git reset --hard HEAD~X
Or reset your local branch to directly match your remote branch:
git reset --hard origin/remote-branch-name
Pull from the repo, make your commits and then you will be able to push again.
You have another option: PULL FORCE!!!
If you alredy known the "push --force" command, then pull force will sound you familiar, but in reality doesn't exist something like "pull --force", but exist a way to replicate that functionality in pull requests:
git fetch --all
git reset --hard origin/master
git pull origin master
(Replace "master" by your branch name)
With that command you overwrite your git history to match exactly the history of your remote repository branch. That will overwrite local changes (the ones that you have not pushed yet) and then you can make your commits and push them.
You can avoid this ackward situation by always working in a new branch only for your work, and when you need to integrate your changes you can make rebase or merge.
There are changes in your local repository that are not on the remote and vice versa, causing a conflict in your history.
You could try the following in your local repository (assuming your remote is called origin):
Make sure your local changes are committed (git commit -am "Some commit message")
Create a new branch (git branch new-branch-name)
Fetch the remote branch (git fetch origin)
Reset the target branch to the version on the remote (git reset --hard origin/target-branch-name)
Merge your newly created branch (git merge new-branch-name)
Push your changes to the remote (git push origin target-branch-name)
Finally resolved it by push the commits to the new PR and then merged the code with old PR

git branch commit does not merge into master

From git window, I executed these commands:
git branch
master
*wcopy
git commit -am "modified provider features"
On branch wcopy
Your branch is up to date with 'origin/wcopy'.
nothing to commit, working tree clean
git merge wcopy
Already upto date
git push origin master
Everything up-to-date
I go to git hub repository
Branch master says:
latest commit 9 days ago
Branch wcopy says:
This branch is 2 commits ahead, 5 commits behind master.
Latest commit db8bdca 18 minutes ago
I checked brnach wcopy the code is upto date
But master is behind.
I merged into master via git command, but it still shows two separate branches with master behind wcopy
(1) How can I bring master upto date?
(2) Also I renamed a file DB/Model.xcdatamodeld to Model.xcdatamodeld on my source.
But the wcopy branch shows the path as
DB/Model.xcdatamodeld/GridModel.xcdatamodel
says this path skips through empty directories.
If I look at the actual code on the source, it shows the path correctly as DB/Model.xcdatamodeld
I don't know why the difference?
Instead of doing this:
git merge wcopy
By doing that you merged wcopy into itself (which is not what you require). You should have switched to the master branch, then did the merged wcopy branch into master by doing this:
git pull origin wcopy
# to make sure you are up to date on wcopy branch
git checkout master
# to have your master branch up to date with the remote.
git pull origin master
git merge wcopy
# resolve possible merge conflicts if any.
git push origin master
This should resolve all your problems and merge branch wcopy with master.
I merged into master via git command, but it still shows two separate branches with master behind wcopy
When you did this:
git merge wcopy
You merged wcopy into itself, the wcopy branch, which of course didn't do anything. You should have switched first to the master branch, then did the merge:
git checkout master
git merge wcopy
# resolve possible merge conflicts...
git push origin master

GitHub - reset local branch and pull new changes

I cloned a repository and checked out specific branch and start editing one file.
My friend also cloned and checked out the same branch and start editing the same file.
We then troubleshoot together on his computer and confirm that everything is working.
He then pushed all changes to the same branch.
How do I properly reset/cancel all my changes (of the same branch) and just pull new ones?
(the ones that he pushed)
You can first discard all your changes
git reset --hard
Then you can pull his version of the branch that he pushed
git pull
How do I properly reset/cancel all my changes and just pull new ones?
(the ones that he pushed)
There are few options:
Delete the local branch and check it out again:
# delete your local changes
git branch -D <branch name>
# fetch the changes or checkout the branch
git fetch --all --prune
# now checkout out the desired branch again
git checkout <branch>
Reset to any given point
# "remove" all your changes from the tip of the branch
# see the next section on how to get the required SHA-1
git reset <SHA-1> --hard
# grab the changes from the remote
git pull origin <branch>
How to find the last common commit?
# Get latest changes from the remote
git fetch --all --prune
# find the last "shared" commit of your 2 branches
git merge-base <branch> origin/<branch>
Check if your branches are up to date
# show remotes and local branches and they status against the remote
git remote show origin

Git-hub issue: Shows 87 files changed of my laravel project

I am new to git-hub and when I create a pull request it shows a large amount of file changed, I am working on Laravel5 and being a trainee in a company I have to send the PR to my team head. So, How to save a fresh Laravel file in master branch and then after doing 1 or 2 changes I commit and generate PR?
Showing 87 changed files with 834 additions and 2,357 deletions
First, if you generate a PR, do so from a dedicated branch, not from master.
Second, whenever you are pushing to a fork (to make a PR), rebase your local branch first on top of the original repo branch.
cd /path/to/local/repo
git remote add upstream /url/original/repo
git fetch upstream
git checkout mybranch # in your case, for now master
git rebase upstream/master # rebase on top of the destination branch
# which will receive and integrate your PR
git push --force origin mybranch
Then make your PR: the diff will be only your work, as opposed to a delta representing evolution on upstream done while you were preparing your fix.

Resources