git fetch with Xcode - iphone

I've had problems using git pull origin SomeBranch in that when there are conflicts, sometimes I cannot open a file in Xcode to resolve the conflict.
From my reading, although I could be wrong, I thought git fetch grabs the code but does not merge it yet like git pull does. How does this work with Xcode?
For example, on one machine on Branch1, I put in some changes.
Then on machine 2, on Branch2, I want to fetch Branch1 changes.
So I did this
git fetch origin Branch2
My output from the command line was:
*branch Branch2 -> FETCH_HEAD
What does this mean? When I go to the source file of the file that I changed, I do not see any changes made.
I thought what would happen is in Xcode, it would then show the changes of that file in that source file and that file would then be Modified. And only when I commit would it be added to the staging area so I could merge it with everything else. But maybe I am understanding it incorrectly. Thoughts? Thanks.

I thought git fetch grabs the code but does not merge it yet like git pull does.
Yes, that is correct. git merge is what updates your working directory, which is why...
When I go to the source file of the file that I changed, I do not see any changes made.
git fetch retrieves the objects that represent the changes, but does not update your working directory. That's what git merge does. Remember that git pull is basically a git fetch followed by a git merge.

fetch just updates your remote tracking branch.
This allows you to inspect what was pulled down from the server before integrating those changes with what you have locally.
You now either merge or rebase those changes to your local branch (if it exists). If it doesn't, you can simply
git checkout branch2
if it does,
git merge origin/branch2
or
git rebase origin/branch2
to skip this 2 step process, just
git pull origin branch2
which will by default merge. You can override with
git pull --rebase origin branch2
you can change your config so that pull will always rebase instead of merge.

For a Git repository, you need to save changes you’ve made and commit them to your local repository before updating changes from the shared repository.
https://developer.apple.com/library/ios/recipes/xcode_help-source_control_management/UpdatingorPullingChanges/UpdatingorPullingChanges.html

Related

GitHub not Recognizing Differences in File Structure [duplicate]

I have a CMS theme installed on my machine. I'm tracking changes to it via git
and decided to back it up on GitHub so I could share those changes.
The theme as provided is also available on GitHub. On my machine I have added
this as a remote upstream. Now I can easily see the changes between my master
and the remote upstream by using the following command:
git diff --color master upstream/number
If I could add the remote upstream on GitHub I could easily share these changes.
Is it possible to set this relationship on GitHub?
I have tried the following:
git push -u origin upstreambranch
which adds an upstreambranch to the master on GitHub. However trying to
compare both branches doesn't work, the result I get on GitHub is that: "There
isn't anything to compare"
Is there an alternative way to compare these?
If the problem is "main and master are entirely different commit histories.", the following will work
git checkout master
git branch main master -f
git checkout main
git push origin main -f
The Short Answer
It looks like GitHub won't let you compare the branches because they don't
actually share any of the same history at all, even though they may share
much of the same files and code.
Here is a screenshot of the temporary fork I made of your repo, where I tried to
compare master with the upstreambranch, like you described. Notice the error
message:
It says:
There isn't anything to compare.
master and upstreambranch are entirely different commit histories.
The Long Answer
You probably downloaded the original source and added it to a completely new
repo instead of cloning the original repo, right? Doing that will make it so
that the history of your repo will be completely different from the
history of the original repo, since your new repo won't have any of the same
commits with the same sha IDs.
You can see that by doing a reverse log of your master branch and the
upstreambranch:
# Your first commit, see commit sha
git log --reverse master
commit c548d7b1b16b0350d7fbdb3ff1cfedcb38051397 # <== HERE
Author: Padraic Stack <padraic.stack#nuim.ie>
Date: Wed Apr 2 15:11:28 2014 +0100
First commit of everything
# First commit sha of the original repo
git log --reverse upstreambranch
commit 105a12817234033c45b4dc7522ff3103f473a862 # <== THERE
Author: Jeremy Boggs <jeremy#clioweb.org>
Date: Mon Feb 22 16:00:53 2010 +0000
Creates repo directories for the Seasons theme.
Solutions
If you redo your commits on top of the original history, you should then be able
to compare the branches. There are several different ways that you can redo your
commits, including
git rebase --onto
and
git cherry-pick
You also can redo each commit manually, if you have to.
I had a similar situation, where my master branch and the develop branch I was trying to merge had different commit histories. None of the above solutions worked for me. What did the trick was:
Starting from master:
git branch new_branch
git checkout new_branch
git merge develop --allow-unrelated-histories
Now in the new_branch, there are all the things from develop and I can easily merge into master, or create a pull request, as they now share the same commit hisotry.
I solve my issue using these commands
git checkout [BRANCH]
git branch master [BRANCH] -f
git checkout master
git push origin master -f
You can force update your master branch as follows:
git checkout upstreambranch
git branch master upstreambranch -f
git checkout master
git push origin master -f
For the ones who have problem to merge into main branch (Which is the new default one in Github) you can use the following:
git checkout master
git branch main master -f
git checkout main
git push origin main -f
The following command will force both branches to have the same history:
git branch [Branch1] [Branch2] -f
From the experiment branch
git rebase master
git push -f origin <experiment-branch>
This creates a common commit history to be able to compare both branches.
This looks like undesirable behavior on github's part, but it's fairly easy to fix. What you want to do is to rebase your branch on a reasonable (any reasonable) commit in the existing history. What you can do is to fetch the github repo and find which tree in its history is most similar to the one you started with. Start this way:
git remote add github u://r/l
git fetch github
myroot=`git rev-list master --max-parents=0`
root_tree=`git rev-parse $myroot^{tree}`
github_base=`git log --pretty=%H\ %T github/master | sed -n "s/$root_tree//p"`
With any luck, that will find you a commit in the github history that has the exact tree you started with. Assuming it does,
git rebase --onto $github_base $myroot master
and you're done.
If that doesn't find a matching tree, you get to find a nearest approximation. Here's one way to get a rough estimate of the differences:
git log --pretty='echo %H $(git diff-tree -p -b -U0 '$myroot:' %T|wc -l)' github/master \
| sh
which will count the lines in a minimized diff between the tree of each commit in the github/master history and your root tree. It seems reasonable to hope for a nice small difference, you could eyeball the actual diffs on it before calling that the github_base commit and doing the rebase above.
Terminology
First, let's get some terminology out of the way...
upstream <= The remote git repo (likely whose master or release branch is in production)
forked-repo <= The remote [experimental git repo] (https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) also known as "origin".
local repo <= The files and directories that you work with on your local workstaion, which you likely got by running a git clone my-forked-repo.git command
local index <= Also known as your local git "stage", i.e., where you stage your files before pushing them to you remote repo.
Github workflow process
Next, let's talk about the process of getting your changes to the upstream repo:
The process is generally to work on a feature branch and then push said branch, and open a Pull Request, either to your forked-repo's master branch or to the upstream's master branch
Create a feature branch by running git checkout -b FEATURE_BRANCH_NAME
Add/delete/modify files project files.
Add files by running git add .
Commit your files to your index by running git commit -m'My commit message'
Push your staged files by running git push origin FEATURE_BRANCH_NAME
Solution for entirely different commit histories
The master and upstreambranch are entirely different commit histories message can occur when you've forked a git repository and have changed your git history.
For example, if you fork a repo and pull your forked repo to work on it locally...
If then you decide to rewrite the entire application and then decide it's a good idea to deleting all existing files, including the forked-repo's .git directory. You add new files and directories to recreate your app and also recreate your .git directory with git init command.
Now, your application works great with your new files and you want to get it merged into the upstream repo. However, when you push your changes you get that "...entirely different commit histories..." error message.
You'll see that your original git commit will be different in your new local directory and if in your remote fork (as well as your upstream). Check this out by running this command in your current directory: git log --reverse master. Then running the following: pushd $(mktemp -d); git clone https://github.com/my-forking-username/my-forked-repo.git; git log --reverse master; popd
You must fix your local .git repo to match your remote my-forked-repo if you want to push your commits and subsequently perform a pull request (in hopes of merging your new updates to the upstream/master branch).
git clone https://github.com/my-forking-username/my-forked-repo.git
cd my-forked-repo
git checkout -b my-new-files-branch-name
# Delete all files and directories except for the .git directory
git add .
git commit -m'Remove old files'
# Copy your new files to this my-forked-repo directory
git add .
git commit -m'Add new files'
git push origin my-new-files-branch-name
Create a PR on GitHub and request to merge your my-new-files-branch-name branch in your my-forked-repo into master.
Note: The "...entirely different commit histories..." error message can also occur in non-forked repos for the same reasons and can be fixed with the same solution above.
A more simple approach where you can't mingle with the master.
Consider i have master and JIRA-1234 branch and when i am trying to merge JIRA-1234 to master i am getting the above issue so please follow below steps:-
From JIRA-1234 cut a branch JIRA-1234-rebase (Its a temp branch and can have any name. I have taken JIRA-1234-rebase to be meaningful.)
git checkout JIRA-1234
git checkout -b JIRA-1234-rebase
The above command will create a new branch JIRA-1234-rebase and will checkout it.
Now we will rebase our master.
git rebase master (This is executed in the same branch JIRA-1234-rebase)
You will see a window showing the commit history from first commit till the last commit on JIRA-1234-rebase. So if we have 98 commits then it will rebase them 1 by 1 and you will see something like 1/98.
Here we just need to pick the commit we want so if you want this commit then don't do anything and just HIT Esc then :q! and HIT ENTER.
There would be some changes in case of conflict and you need to resolve this conflict and then add the files by
git add <FILE_NAME>.
Now do git rebase continue it will take you to rebase 2/98 and similarly you have to go through all the 98 commits and resolve all of them and remeber we need to add the files in each commit.
Finally you can now push these commits and then raise Pull Request by
git push or git push origin JIRA-1234-rebase
This happened for me because I created a repo from GH, but then I also added a README. In doing so I created a commit on my remote.
Then I went and created a new repo locally, made some changes and committed. Then I pushed it to the 👆repo and tried to make a Pull Request.
But my remote's initial commit was different from my local's commit, hence this error message. GitHub itself even warns you against this:
Create a new repository on GitHub.com. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
GitHub Docs
Similarly if you're creating a new repo, GitHub will quietly suggest that you skip initializing the repo. Rather just define the repo.
tldr the very first commit has to be identical, you can't merge 2 commits that don't have an identical initial commit.
If you know from which commit issue started, you can reset your branch to that commit and then merge them.
this is 100% works in any situation :
1)create new folder in your machine
2)clone the remote repository to the new folder
3)delete all files and folders except for the .git folder
4)add your project files that you are working on to this new folder you created
5)open terminal
6)cd new_folder_path (path to the new folder you created)
warning : don't type > git init
7) > git add .
8) > git commit -m "write anything"
9) > git push URL(url of the remote repository)local_branch_name:remote_branch_name
This happened with me yesterday cause I downloaded the code from original repo and try to pushed it on my forked repo, spend so much time on searching for solving "Unable to push error" and pushed it forcefully.
Solution:
Simply Refork the repo by deleting previous one and clone the repo from forked repo to the new folder.
Replace the file with old one in new folder and push it to repo and do a new pull request.
I solved that problem. In my case when i did “git clone” in one directory of my choice without do “git init” inside of that repository. Then I moved in to the cloned repository, where already have a “.git” (is a git repository i.e. do not need a “git init”) and finally I started do my changes or anything.
It probably doesn’t solve the problem but shows you how to avoid it.
The command git clone should be a “cd” command imbued if no submodule exists.
I got this error when initializing a GitHub repository with a README file, and then trying to push my existing local git repository to it. This resulted in a main branch with the README file, which was the Default branch, and a master branch with my code, and they couldn't be merged.
But since I didn't actually have anything important in my main branch (if you need to keep the data from both your branches, check out PaianuVlad23's Answer instead), I managed to solve the problem by changing the Default branch to the master branch, and then delete the main branch, like this:
When in GitHub, click your user icon in the top right of the window.
Choose "Your repositories", and then click your repository name.
Under the repository name, choose the tab "Settings".
From the pane on the left, choose "Branches".
Under the headline "Default", change the default branch from the one you want to delete (in my case main) to the one you want to keep (in my case master).
Now, click the tab "Code" under the repo name.
Under the tab line containing "Code" etc, you'll see a place where it says "2 branches". Click it.
Find the branch you want to delete, and click the trash bin icon on the right on that line.
Now, your repository has only one branch, which is the one you want to push your local changes to! 🙂 Just as if you hadn't initiated your repository before pushing to it, as #mfaani's answer in this thread suggests you do it.
I found that none of the answers provided actually worked for me; what actually worked for me is to do:
git push --set-upstream origin *BRANCHNAME*
After creating a new branch, then it gets tracked properly. (I have Git 2.7.4)
I don't think we have same case here, but still someone else may find it helpful.
When similar error occurred to me, it was going to be the first merge and first commit. There was nothing in on-line repository.
Therefore, there was no code on git-hub, to compare with.
I simply deleted the empty repository and created new one with same name.
And then there was no error.
I got this error message, because I was migrating an application from SVN to GitHub and it's not enough to invoke a git init in the location of the source code checked out from SVN, but you need to invoke a git svn clone in order to have all the commit history.
This way the two source codes on GitHub will have a mutual history and I was able to open pull requests.
I had an issue where I was pushing to my remote repo from a local repo that didn't match up with history of remote. This is what worked for me.
I cloned my repo locally so I knew I was working with fresh copy of repo:
git clone Your_REPO_URL_HERE.git
Switch to the branch you are trying to get into the remote:
git checkout Your_BRANCH_NAME_HERE
Add the remote of the original:
git remote add upstream Your_REMOTE_REPO_URL_HERE.git
Do a git fetch and git pull:
git fetch --all
git pull upstream Your_BRANCH_NAME_HERE
If you have merge conflicts, resolve them with
git mergetool kdiff3
or other merge tool of your choice.
Once conflicts are resolved and saved. Commit and push changes.
Now go to the gitub.com repo of the original and attempt to create a pull request. You should have option to create pull request and not see the "Nothing to compare, branches are entirely different commit histories"
Note: You may need to choose compare across forks for your pull request.
Top guy is probably right that you downloaded instead of cloning the repo at start.
Here is a easy solution without getting too technical.
In a new editor window, clone your repo in another directory.
Make a new branch.
Then copy from your your edited editor window into your new repo by copy paste.
Make sure that all your edits are copied over by looking at your older github branch.
I had mine solved by overriding the branch:
My case: I wanted to override whatever code is in the develop with version_2.
delete the local copy of conflicting branch:
git checkout version_2
git branch -D develop
checkout a fresh branch from the version_2 and force push to git:
git checkout -b `develop`
git push origin `develop`
I didn't need to rebase. But in my case, I didn't need to take code from my old code.
first: pull from remote repo
merge or rebase
finally: push to remote repo
finish
When you are pull/merging feature to main and are in the main branch in the terminal, I successfully used 'git pull origin feature --allow-unrelated-histories'.
Before using this command, I had the same message about completely different commit histories, and I think it's because I accidentally pushed to main after committing to the feature branch. Then I tried some of the solutions offered here like rebase, which allowed me to merge my code, but I still had the compare and pull notifications through git, and it was a one time fix. By one time fix I mean I still got the different commit history message the next time I tried to merge a feature branch's code to main.
Another source from a google search offered the --allow-unrelated-histories fix, and it permanently works exactly how I wanted it to. The branches were merged and now I can merge without error messages and the compare and pull notifications work through git.
I'm sure there are consequences for people who didn't have the same problem as me, but I didn't lose any code and now my repo is clean. Also, I'm also an amateur coder and the question is older so maybe this command wasn't available when the question was asked or I'm not understanding the issue correctly.
I wanted to copy commit history of "master" branch & overwrite the commit history of "main" branch .
The steps are:-
git checkout master
git branch main master -f
git checkout main
git push
To delete master branch:-
a. Locally:-
git checkout main
git branch -d master
b. Globally:-
git push origin --delete master

I am trying to go back to a specific commit in Github to erase the mistake I made

I have looked at many past posts without a specific response. I used git reset --hard to get me to a point in time when my code was not all jacked up. Yes I should have used a branch but I didn't. Now I have a message of HEAD is now at 0600b73 fixed error. What I would like to know is how to commit those changes, so I can push them back to Git, so they are the current head? I am not even sure if VS Code even recorded the changes. Git can be confusing so any help would be appreciated.
You haven't actually proven you have changes, HEAD is now at 0600b73 fixed error., that does not mean you have changes. That basically means that you moved the HEAD of git to a commit, generally you want it on a branch.
git status will tell you if you have things that need to be committed.
I am not even sure if VS Code even recorded the changes
VScode has some cool features around git but it doesn't actually record changes, unless you are referring to saving files. If you save the file, well it saves.
What you probably want to do is (I am assuming you actually do have changes):
confirm you have changes git status
move to a branch git checkout -b my-fix
add those changes git add . (adds all changes/also make sure you are at the root of the repo) or git add -p will let you look closely at changes and give you an interface to decide if you want to stage the change or not.
commit the changes git commit -m "Your message" or you can do a longer message with git commit -p
At this point you can merge this branch in to master, push the branch. It's up to you.
push your changes, git push origin my-fix, make a pull request and merge to master. You will then want to change to master and pull master
merge git checkout master , git merge my-fix
rebase git checkout master , git rebase my-fix
Here is some git learning material:
https://learngitbranching.js.org/
https://www.codecademy.com/learn/learn-git
https://try.github.io/
https://www.atlassian.com/git

Egit: You are in detached head state. This means that you don't have a local branch checked out

I recently created a project on Eclipse. I set up a git repository on the project. Pushed the code to a new repository on GitLab.
I checked out (Import->Project from Git) the project to another laptop, made updates, committed it as another author and pushed it back to repository (2nd commit /revision). I only removed the target runtimes from the project as the changes.
I went to my 1st laptop and wanted to try update / pull my code. I right-clicked on the repository from the Git Repository view -> Remote -> Fetch. I entered the path to the repository and etc and fetched the repository. But, my project still has the old code having the target runtime. I know I only 'fetched' the repository, not 'pulled' maybe.
So I was confused, the code was not updated. If I am not mistaken, I right-clicked the repository again, clicked on 'Check Out'. This was the result :
After 'Checkin Out', the target runtimes on my project were removed, updated like from the 2nd commit. But there are now 2 branches and 2 refs. I don't what state my local repository is in now. And/but the local master branch was still in 'initial commit', isn't it confusing?.. My code has been updated..
I only read a bit of the git manual online, and I haven't yet be able to wrap my head around concepts like refs.
I could just delete the project and Import->Project from Git, right?
But if I can I would like to know what happened to my project (local repository) and how to fix this?
Thx
I'm not familiar with egit and its menus. But I try to answer your question with git commands which you can run in the console. And I think you can figure out which egit menu items are corresponding to these commands.
You wanted to pull and update the local master in the first laptop repository. You needed git checkout master first, which could be skipped if you were already on master and then git pull origin master. However you did git fetch origin master instead, which just updated origin/master with the new commit. It would have been okay to use git fetch origin master if git merge FETCH_HEAD or git merge origin/master followed imediately, because in many cases pull = fetch + merge, but you didn't.
I guess you then did git checkout origin/master. Checking out origin/master leads to detached HEAD state. To make it easier, you could just consider the detached HEAD as a nameless branch. When you make new commits, this namelss branch will grow. But you can't see these commits on other branches unless you apply these commits to them. Git has some commands to apply commits, including git merge, git rebase, git cherrypick, etc.
To fix the current situation, you can simply run git checkout master;git pull origin master.
So to make my answer more simply. Commit changes on your deateached head and make branch with this commit and then merge it together and push to master.
You can just download again your project if you didn't make changes on detached head that you need.

git fetch not working for fetch new files

I am new to Github and have checked lots of examples for fetching and pulling files from the Git server. However, my fetch command does not work as I expected; my pull command works as I expected and downloads new files onto my system. Is the fetch command not able to download files locally to my branch?
You need to understand the difference between fetching and pulling. When you do a fetch:
git fetch
you update all the local tracking branches in your Git folder. The tracking branches are your local copy of what is actually on the repository, and it is these branches which Git uses for most operations. This means that every tracking branch will now be in sync with the latest changes from GitHub. However, this does not mean that the local branch you have is now up to date. To bring your local branch up to date you have to additionally either merge or rebase that branch on the remote tracking branch.
Assuming you are on the master branch, you can remember what git pull does as follows:
git pull = git fetch + git merge origin/master
In other words, there is merge here to actually get the changes from the remote repository into your local branch. You could also rebase your local branch on the version in the remote via git pull --rebase master.
So the answer to your question is that the fetch command absolutely does bring in changes from the remote to your system, but you have to additionally merge or rebase to update your local working branch.

eclipse git confusions: How to merge & push?

I can never seem to figure out how to use git. I just do random commands and it works eventually. This is what I usually do when I want to push:
Fetch from upstream
Commit
Push to upstream
However, the above steps don't work sometimes. For example, when I do fetch from upstream, my local branch doesn't get updated.
How do I merge the remote branch into my local branch? Is there a difference between pull and fetch?
"git pull" is equivalent to "git fetch" followed by "git merge"
What you want to do is:
git commit -am "your message here"
This commits your current changes locally, and only locally. Now, assuming branch "foo":
git pull origin foo
This does a "git fetch" followed by a "git merge". Everything in this step is done to your local branch, and still has no effect on the remote branch. Depending on how your git is setup, you might need to specify the branch.
If you have a merge conflict, you will have to fix the files listed, then add them back:
git add path/to/resolved.file
When you are done, push everything you have from your local changes, to the remote server:
git push origin foo
If you pull before committing, that might cause your local branch to not be updated.
tl;dr, always commit before pulling!
Well to understand differences betwene git fetch and git pull look here.
In the simplest terms, "git pull" does a "git fetch" followed by a
"git merge".
You can do a "git fetch" at any time to update your local copy of a
remote branch. This operation never changes any of your own branches
and is safe to do without changing your working copy. I have even
heard of people running "git fetch" periodically in a cron job in the
background (although I wouldn't recommend doing this).
Working with git in eclipse might be a little bit tricky, but if you understand git basics, you should cope with it. I recommend you to read one of git tutorials, so you will be able to understand basic git operations.
The first step to learn git: Don't use egit.
Now, you are fetching and pulling from upstream. That makes me think that several people have write-access to the same repository, so we probably don't want to be doing a whole lot of merges to complicate history. It would be best to do this. I'm assuming you have already committed a changes or a set of changes to your local master branch that you want to place on the upstream repository which has been pushed to by someone else while you were making your commits.
First, we fetch the new changes but don't use them yet. This updates upstream/master to the new head of the upstream master branch.
git fetch upstream master
Now, we need to pull in these changes. This command rewrites history, and we are assuming
that you have not published your changes anywhere yet.
git rebase upstream/master master
If you have published your changes, this messier command is the one you should use (do not use both, just use this one!)
git merge upstream/master master
Now, we can push:
git push upstream master
The first two steps can be shorted to git pull --rebase and git pull for the rebase and merge versions respectively.
If you are already on the master branch, most of those second arguments are superfluous but I wrote them in for clarity. Notably, giving a second argument to git-rebase or git-merge will simply check out that branch before doing the operation. Supplying master to fetch and push is only necessary if you don't have the refs set up to automatically fetch and push master.