Git reset not working the way I want it to - ruby-on-rails

I never ran rake assets:precompile before and never needed to. I think when I push to heroku such is done automatically.
After committing to git I was playing around and ran rake assets:precompile for the first time. This kind of seems to have cluttered my public folder. git status indeed showed all these new (untracked) files.
So I wanted to revert back to the last git commit and undo this. I ran git log and determined the id of the last commit. I then ran git reset --hard 4a9aa***f76. It confirmed HEAD is now at 4a9aa72 .... However, git status still shows all these new files. How can I get rid of them?

git reset will not remove untracked files. you can either delete them manually or run git clean -f

Or just add the files you want to delete and stash them.
git add .
git stash
git stash clear

Related

Filetree is not refreshing after switching branches - cloud9

In cloud9 IDE, I am doing simple rails app in master branch. I decided to experiment a little, so I created new branch like this:
git checkout -b experiment-branch
Then I created some controllers, models etc. but experiment fails and I do not committed it. However I dont't want to delete this branch, so I tried to go back to master:
git checkout master
and code (controllers, models etc) from the previous experiment was still there in filetree, ApplicationController etc.
I tried git reset --soft <desired-previous-commit-hash> but it not worked.
I assume the second command should return me the state of my app from before creating the branch experiment-branch. Am I right or I do something wrong?
If you have not tracked the new files you add in the experiment-branch - these are still lying around as untracked files.
Untracked files are not removed when you change branches.
You would need to clean them to remove the untracked files. use git clean -n (dry-run) to identify all the files that are untracked.
Then you could git clean -f to clean all the file shown in the dry-run. Or you could use the interactive mode git clean -I
To revert changes from tracked files, use git checkout .
Refer this post for more details.

Git pull request - how to pull and update Sublime

I've made a stupid error which has messed up my database on a Rails App I'm working on. Luckily I'm on a branch and haven't committed any changes so the version on Git is in working order.
How do I now pull through the Git version and update to Sublime so I can carry on working as if nothing happened?
I've just done git pull origin master but it says up to date so I've obviously done something wrong.
I'm not keen on a db:drop so I'd rather do it this way if possible.
You can't pull again, because you already pulled every commit from your remote.
Add changes, stash them, remove the stash:
git add --all && git stash && git stash drop
This will remove every uncommited change and bring you back to the latest commit on the current branch.
git checkout 01h5y77d (find this in git, a version of the app which works)
This would print "HEAD is now at 01h5y77d..."
You don't have to commit because you did not commit the mistakes yet :)

Git merge conflict with workspace.xml

I'm trying to push my Rails project to Heroku, but Git isn't allowing me to do anything at the moment. Here's what I've done so far:
git push heroku failed because the heroku branch was "ahead" of my local branch, which should not have been possible.
I pulled and there was a conflict with .idea/workspace.xml. I wasn't able to find out what that file is, but it's huge and Git wrote all kinds of garble to it. Too much to manually "resolve" conflicts.
I saw some stackoverflow posts talking about git-ignoring that file (maybe it's some IDE file for RubyMine or something?), so I tried to move the file away to avoid the conflict
I ran git add -A (also tried git add . and git add)
git commit --amend fails because "You are in the middle of a merge"
git merge --abort fails because "Untracked working tree file '.idea/workspace.xml' would be overwritten by merge (despite the fact that the file has been moved)
git reset --merge fails for the same reason.
How can I make Git work again?
.idea/workspace.xml
This file is your idea workspace files. They are generated by IntelliJ tools.
I saw some stackoverflow posts talking about git-ignoring that file (maybe it's some IDE file for RubyMine or something?), so I tried to move the file away to avoid the conflict
Simply add the folder to your .gitignore but since its already committed you will have to remove it from the repository:
# Quit the merge
git merge --abort
# remove the whole folder from the repo
git rm -rf --cached .idea/
# add it to the .gitignore: idea/
# add and commit your changes
git add .- A
git commit -m " Removed idea folder"
git push origin <branch>
If you still unable to do it?
First reset the code to the previous state and then do the above code again.
The reset will take you to your last commit before the pull
git commit -am "message" worked (as opposed to amending a commit)
I have resolved a similar problem by simply deleting the workspace.xml file. By building and running the program again idea will autogenerate a compatible file.

Ruby on Rails - Git Branch Workflow

I know its a simple concept but just not grasping it after researching several sites.
I have a Ruby On Rails project and using git to manage the source. I have a production ready snapshot and initialize git such that it has a master (with git init, git add -A and git commit -m).
Now I want to try out a new feature so I create a branch called 'test' with git checkout -b test
Now while in test I try out a new scaffold with rails g scaffold UserToken username:string
Scaffold created all of the ROR files and I do a rake db:migrate to update the DB. I then go into the rails console and test out adding records to the db and then start working on updating the other generated scaffold model file.
After lunch I come back and decide I want to scrap all of this.
Questions - (after trying this out myself) is the only way to get back to master is to git add -A, git commit -m and then git checkout master ? Do I really have to add and commit to get back to master? (this does work; however I don't think I'm grasping something basic in git that I would do a commit on something that I am going to scrap)
Next, if I do the above (again I don't think that's right) when I get back to master I do see that my scaffold files are gone as are the migration file (which created the table) and the schema.rb reflects that the table generated while in the branch is not there.
So far so good:
HOWEVER, if I go into the actual database the table IS still there. What basic fundamental am I missing in ROR/Git of testing something out in a branch and then abandoning it?
UPDATE #1
So Stash does not appear to help:
Stash does not help.
Steps:
rails new test_app
git init
git add -A
git commit -m 'initial commit'
git checkout -b newfeatures
rails g scaffold UserToken username:string coin:integer
files get generated...
sqlite3 db/development.sqlite3 show that there is now a table called user_tokens:
git stash save
git checkout master
Now in Master however all of the scaffold files still exist (and shouldn't)
You have two questions here:
First: How do I move between branches without commiting work in progress?
To move between branches without having to commit your work you can use git stash.
git stash help
Usage: git stash list [<options>]
or: git stash show [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]
or: git stash clear
Once you stash your work then you can switch between branches without problem. Note that files that you wish to stash must first be added to a git branch via git add. Changes to files, including creation, that have not been added to git are are not tracked. So they are just like any other part of the general file system and will remain visible across branches.
Second: Why do my database migration changes in one branch show up in another?
Because the Database manager is not part of git. Whatever DBMS you are modifying those changes are persisted through the DBMS. If you want to keep your branch migrations separate then you need to have a separate database instance for each.
It may help if you imagine a git branch as a file-system template. When you switch to another branch then that branch's template overwrites your existing file-system with whatever is under its control. Everything else is ignored. When you commit you are updating the template for that branch. However, all of your work is actually done in the one true file-system.
What this means that things that you do to the file-system that are outside of git's control remain visible from all branches.
git reset --hard HEAD deletes everything and goes back to head in your testbranch so you can easily switch back with git checkout master
rake db:reset should do the trick with your data in the DB
UPDATE:
If you really want to get rid of anything:
git reset --hard && git clean -dfx
rake db:drop
rake db:create && rake db:migrate

git push heroku - stop heroku pushing/uploading massive file

Having accidentally put a pretty large file which was not excluded by .gitignore in my working directory i committed my changes, pushed up to github then did a git push heroku up to production.
when i saw it was trying to push 100s of MB of data up to heroku, i killed the process.
I have since done a
git rm filename.extension
followed by
git add ., a new commit and git push. But when i get to git push heroku it insists on continuing to push this impossibly large data file.
how can i check what the offending file is, and how can i make git/heroku forget it was ever there so git push heroku can start working again...?
Thanks!
If you did a git rm large.file, the commit introducing the large.file is still in the history of your repo.
To make git forget about the commit, you'll need to remove it from your repo's history. This could be done using git rebase as described in the answers to these questions (for example):
How do you remove a specific revision in the git history?
How to remove selected commit log entries from a Git repository while keeping their changes?
After you removed the commit from the history, you could git push -f github and then git push -f heroku.
Note that git push -f could cause problems if someone fetched the state of your github repo since your last push. See chapter The Perils of Rebasing in the progit book for an explanation why.

Resources