So I know how to update the repository with my files (the master one at least). And I know I can create a local branch with
$ git checkout -b [branch_name]
but that only creates it on the local git...how do i checkout a branch from the master on github and also overwrite files in my app directory, so I can update my project with the work of other people
Do you mean to ask, how to reset master on your local machine to that of the master on the origin?
If so:
Fetch all remote/origin changes and then hard reset your local master to origin/master's head:
$ git checkout master
$ git fetch --all
$ git reset --hard origin/master
Related
I created a new Rails app and pushed the code to Github directly from master (first commit in the repository). However I made a mistake, I didn't want to commit this new Rails app directly from master, but instead create a new branch from master and push the new Rails app from this new branch.
Therefore, I'd like to:
Delete the commit from master in Github(remote), so master is EMPTY
create a new branch from master and add the previous commit that was in master into this new branch.
push it to Github.
Delete the commit from the master in Github(remote), so master is EMPTY
You can create an orphan branch - orphan branch is branch without any history
# Create "clean" branch
git checkout --orphan <name>
# remove all existing content if you wish
git clean -Xdf && git clean -xdf
create a new branch from master and add the previous commit that was in master into this new branch.
few options:
# Option 1 - Start your branch from the last desired commit
git checkout -b <name> <SHA-1>
# Option 2 - Set your branch to the desired commit
git reset <SHA-1> --hard
# Add the required commit on top of your branch
git cherry-pick <SHA-1>
push it to Github.
# force the update of the new branch
git push <origin> master -f
Please create a new branch from the master with the following command,
git checkout -b branch_name
After that checkout into your new branch and push it to Github.
Now go to the master branch and remove the last commit and push it to Github
You can also try the following steps with your repository:
git checkout master: make sure you are in master.
git checkout -b my-fancy-new-branch: create your new feature branch.
git checkout master: switch back to master
git reset --hard rootcommit: reset master to the state before your own commits.
optional and if you have a remote you pull from: git pull --ff (if this fails because the pull is not fast-forward, you have to reconsider rootcommit. It contains some of your work)
I tried to do this on my test repository, it appears to work.
I have taken a reference from this answer, that can help in finding other approaches too.
I am working with a team on git and my local untracked production branch is missing lines of code from a team member.
My local master branch is tracked on the remote/origin/master and it shows all the code.
The local production branch was checkedout from remote/origin/production with this command
git checkout production
After finishing the development branch I did:
git checkout production
git merge development
I deploy to server and then
git checkout master
git merge development
git merge production
git push origin master
Later I find out that production branch and server are missing some lines of code (while master is ok)
I thought this could be caused by the fact that the remote branch was not tracked with git checkout --track production
Then I found that git diff master..production does not show differences, but analyzing the production branch I can find this commit relative to the merge of the development branch:
git show b17832a
commit b17832ae656f1bd43ebf837934f16b7d1f6efa33
Merge: a9bd850 0873d56
Merge branch 'development'
and here I can find the removed line of code
git diff a9bd850..0873d56 file.rb
diff --git a/file.rb
index e0d1d1b..2343d92 100644
--- a/file.rb
+++ b/file.rb
## -49,12 +49,6 ## angular.module('sp').directive 'spPlayer', [
some text and below I can find
- my missing line of code
- is showing here
how can I fix the production branch and push the change? My commit on master have already been push to the remote.
You mentioned:
Later I find out that production branch and server are missing some lines of code (while master is ok)
Update local production with master then push to remote production (origin/production).
$ git checkout production
$ git merge master
$ git push origin production
Update remote master (origin/master)
$ git checkout master
$ git push origin master
N.B. the issue is solved discussing in OP's comment section actually.
I'm trying to update the 3-2-stable branch on my fork of the Ruby on Rails project. So after cloning rails, I initially did a git checkout -b my_branch remotes/origin/3-2-stable. Then I made my changes. Then I added my fork as a remote repository with git remote add my_fork git#github.com:myusername/rails.git. Does a git push my_fork my_branch update the 3-2-stable branch only in my_fork? Is there something special I need to do?
Does a git push my_fork my_branch update the 3-2-stable branch only in my_fork?
Yes it will. It won't affect any other repo.
Note that the 3-2-stable on your fork won't be impacted either: a new branch my_branch will be created on your fork to reflect your local branch.
But if you want to push/pull that branch from the repo my_fork, then this would make it easier:
git push -u my_fork my_branch
See "What exactly does the “u” do? “git push -u origin master” vs “git push origin master”" for more on setting the upstream branch of a local branch.
I have forked rails git://github.com/rails/rails.git at github. My forked repository is at git://github.com/waseem/rails.git. I want to test out some patches submitted by other users to rails mainline. Lets say I want to test out code in migration_status branch at git://github.com/joelmoss/rails.git.
Lets say I
master $ git remote add joelmoss git://github.com/joelmoss/rails.git. and
master $ git remote add mainline git://github.com/rails/rails.git.
I have been pulling from rails mainline into my master.
master $ git pull mainline master
According to http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#testing-patches I should create a local topic branch and pull in changes from joelmoss/migration_status. So I,
master $ git checkout -b migration_status Create a local topic branch.
And when I do:
migration_status $ git pull joelmoss migration_status
I get a large number of conflicts. I also tried migration_status $ git pull --rebase joelmoss migration_status but I still get conflicts.
In the case of pull --rebase, I think (correct if wrong), git is trying to apply my local changes on top of changes fetched from joelmoss/migration_status. Ideally it should do the opposite. To consider this option, I did following.
master $ git fetch joelmoss
master $ git checkout -b joel_migration_status joelmoss/migration_status and
joel_migration_status $ git rebase master it still gave me lots of conflicts.
How do I pull in patches submitted to one of my local topic branches w/o getting conflicts? I can not resolve those conflicts as I do not know much about what code to keep what not to.
In this case, it looks like joelmoss/migration_status is based off of 3.1.0, which split from mainline/master back in May. So if you merge you're trying to reconcile 4 months worth of development by everyone, in branches that appear to never have been intended to merge.
What you want to do is base your local changes on 3.1 as well. That doesn't guarantee to remove all conflicts, but at least it should be ones you are aware of because it's code you changed directly.
git checkout -b master-3-1 master
git rebase --onto joelmoss/migration_status mainline/master master-3-1
i use the git workflow as described in this blogpost.
In short: everybody develops inside his/her own branch, before merging back to master, you rebase your branch to master again to get clean history.
This works.
Now we have a submodule, and because this is an in-house plugin (Rails), we need to change this often. So most of the times i have changes both in the general branch and in the submodule branch.
What is the best way to work with submodules in the workflow as above.
I first try to push my changes to the submodule (git checkout master, git pull, git checkout branch, git rebase master, git checkout master, git merge branch).
Then, when i try to do the same for my root, i always get an error on my plugin (submodule). I have to resolve the error, before doing git rebase --continue. So if try to git mergetool i converts my folder to a file.
After the rebase has ended, i just restore the <folder_name>.orig to overwrite the file <folder_name> and all is well.
But somehow it feels there should be a better way.
In short: when working via checkout-b/rebase/merge - workflow, how do you handle the changed submodules simultaneously?
Whatever workflow you are following with submodules, there is one rule you shouldn't forget:
(From the Git tutorial)
If you want to make a change within a submodule, you should first check out a branch, make your changes, publish the change within the submodule, and then update the superproject to reference the new commit.
$ git checkout master
$ echo "adding a line again" >> a.txt
$ git commit -a -m "Updated the submodule from within the superproject."
$ git push
$ cd ..
$ git add a # There is a gotcha here. Read about it below.
$ git commit -m "Updated submodule a."
So did you commit the new state of your submodule within the parent project before attempting your rebase/merge from said parent project?