git fetch not working for fetch new files - github

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.

Related

Github pull without a pull request

On github, how is it possible for the owner of an original repository to pull the changes that were made in a fork by another user, without a pull request? Or anyway include the fork's changes into the original repository, maintaining information about the fork's author?
Should I clone the fork and then push it to the original repository?
Or I could copy them manually, but that wouldn't be fair at all, because the project would lose any information about the fork's changes author.
You would use the normal distributed workflow of Git here. That is, add the other repository as a second remote to your local repository, and then simply merge their changes in.
git remote add others-fork https://github.com/otheruser/fork.git
git fetch others-fork
git checkout master
git merge others-fork/master
This would merge the changes from their master into your local master. Afterwards, you can push your changes to publish their commits in your repository.

Merging changes head branch to master branch in git

I am currently making a project in eclipse.I made changes in head branch and as well as in master branch.I want to merge those changes and push them to remote repository.Please tell me the correct steps so i merge both branches and push the changes to remote repository without getting non fast forward warning.
I am currently making a project in eclipse.I made changes in head branch and as well as
in master branch.
Normally when people refer to "head" they are talking about HEAD, which is not actually a branch but a reference to the "tip" of the currently checked out branch. So if you
git clone foo
cd foo
git checkout bar
assuming bar is a branch, then HEAD would refer to the "tip" or last commit of the bar branch.
If you are getting a non fast forward warning on a push, changes have been made to the remote repository. You to bring those changes into your local branch before you can push.
This is because git requires merge conflicts be resolved in the local repository, not the (usually shared) remote repository. To bring those changes to your local repository you will need to execute a pair of commands; git fetch ... and git merge ... results in a merge commit, which some prefer -- while git fetch ... and git rebase ... if merging the changes without a merge commit is preferred. Note that git pull ... is the same as git fetch ... and git merge ... and git pull --rebase ... is the same as git fetch ... and git rebase ....
Which ever way your prefer, once you get the changes to your local repository (and resolve any conflicts there may be) you will be ready to push.

Git branch and local changes

Lets say I do a git checkout -b my_new_branch. Making some changes to my local files and after this I add all files with git add. and commit it and push. After this i realize that my branch is messed up and I want to delete it.
So Im going back to my master with git checkout master and delete the branch with git branch -D my_new_branch.
Will all local changes be reversed?
Once you checkout the master branch, all of the changes that you made on my_new_branch will no longer be in your working directory. They will still be on your branch and on the server.
Once you delete my_new_branch, the ref to that branch will be deleted. The commits will still exist in your local repository until a garbage collection is run. The commits and branch will also still exist on the remote server.

git fetch with Xcode

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

cannot see committed changes in repo

I have staged my files like this
git add mydir
git add mydir/myfile
and then pushed them to my repo
git remote add origin https://github.com/usename/myrepo.git
git push origin master
When I look at git status -s it says my files have been added, but I cannot see them in my actual repository. Does anyone know whats going on here?
You have not "added" your files to the repository, only to the staging index.
You need to git commit to get your changes from the staging index to the repository.
git status shows you the status of the working tree and staging index. git log shows you the status of the history - in other words, git log will show you what's been committed to the repository.
As an example, see this git status output:
git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: Game.py
#
In this case, we can see that the changes to be committed involve Game.py. Note that these changes haven't yet been added to the repository - they're just staged, ready to be committed, as the git status says.
The next step is to commit these changes we've prepared in the staging index and add them to the repository. git commit is the command to do this.
This answer, as well as the linked Pro Git book, have some excellent reading which will help you understand the setup and steps in committing to a git repository.
I cannot see them in my actual repository.
What do you mean by "actual" repository? I am presuming you mean you can't see a local commit, but there may be some confusion about the repositories you're using.
You're using two repositories, one local and one remote. The remote repository is, in your case, on GitHub. You do your work locally, then git push to remote.
First, local changes and commits
So the steps to get your code changes into the local repository are as follows:
Make changes in your working directory. This is most often writing some code.
Build a commit by staging changes. This is done using git add.
"Save" the commit you've built into the repository. This is done using git commit.
These steps take place **locally*. None of these steps have any affect on your GitHub repository.
Secondly, git push to the remote repository (in this case, your GitHub one).
To get your code from local to remote repositories, git push is used.
Check what remote repositories are known by your local repository: git remote -v.
If the GitHub repository is not shown, you'll need to add it: git remote add <NAME> <URL>.
Because you didn't git clone, the two repositories (local and remote) don't have any commits in common. That's ok; it just means git doesn't automatically know where to push your changes, so you'll have to specifiy: git push <NAME> master:master. That'll push your local master branch to the <NAME> master branch.