So I have a commit to my local branch that has yet to be pushed to my remote. I worked a little after my local commit and decided to go back to what I had committed. I selected the Discard All Changes . . . under Source Control but it seemed to go back farther then I wanted. I can see the commits under my History . . . but when I select Pull . . . all I get is options for my remote branch (which still shows a branch that I terminated, so helping me refresh that would be great too but the other thing is more important right now).
Any and all suggestions are greatly appreciated.
Solution: So what I ended up doing was using git log to find the commit that I wanted. I copied the commit number. Then git reset --hard *commit #*. The files were in my Finder window but no in my Xcode project, so I went to Files->Add Files to "projectName" and added the files. After I assured myself that everything was fixed I committed my build using git push --force.
Hope that is helpful to someone, feel free to comment if you have any questions on my solution.
CGTheLedgend,
So when you git pull you are actually pulling source code from your remote repository into your local one. Instead what you want to do is a git log which will show all your commits in the following format:
commit f5c5cac0033439c17ebf905d4391dc0705dbd5f1
Author: CGTheLegened
Date: Fri Sep 6 14:36:59 2010 -0500
Added and modified the files.
commit c14809fafb08b9e96ff2879999ba8c807d10fb07
Author: CGTheLegened
Date: Tue Sep 4 08:59:32 2010 -0500
Just simple test for core.editor.
... etc ...
From here, you want to use the git reset command to go to a specific commit. If you do:
git reset --hard c14809fa
To make your local code and local history be just like it was at that commit.
Please let me know if you have any questions!
Related
Originally on my Github are some outdated styles.css and JavaScript files and others.
I was working on my project last night but didn't push it to Github or back it up. It was just saved in my local repository using a series of local, unpushed commits.
Being a newbie that I am, I did a git pull request master, and all the files in my local repository got replaced with the original styles.css and JavaScript that was in my github.
Is there a way to get those files back?
I did a git reset head#{2} where I believe the state of the repository before the pull request was, and it showed some unstaged files.
In Gitshell my command line has "master [+10 ~19 -42 !]" with the text master being yellow.
At this point, what do I do? Currently I seem to have lost a lot of work.
If you have performed commits often you can pretty much get to any of them.
Use git reflog first (https://git-scm.com/docs/git-reflog). It will show you all the interim commits you've made. Once you find a relevant one you can do git reset #commit_id.
I am facing issues with the git checkout command. Please help.
Here is what I did.
Created a local directory. Created some files. Initiated a git repo locally.
Created a repo in bitbucket & pushed my local repo to bitbucket
Created a new branch locally, b01
Added new files to b01, committed them and successfully pushed the branch to bitbucket
Now I am trying to merge b01 with master and that is where I am facing issues. I am not able to checkout master.
Lists of commands used and the error details:
git clone [my repo]
git checkout -b b01
Hereafter I made multiple changes, including creating new files and directories, committed those changes and successfully pushed the changes on that branch to the repo. like:
git push -u origin b01
Now I am done with the changes and want to merge it to the master branch. So I do a:
git checkout master
Here is the error it throws:
fatal: cannot create directory at 'workfolder': Permission denied
Git keeps me in b01, but deletes ALL the files I had created in the branch.
So if I now do a
git status
it shows me:
On branch b01
Your branch is upto date with 'orgin/b01'
Changes not staged for commit:
(use "git add/rm .....)
deleted: new file1
deleted: new file2
<list of the new files I had added but which got deleted as a result of the command>
I am having to run a
git checkout -- .
to restore the files.
Please help.
I had a similar situation. Though it was due to me having the solution open in visual studio. Closing visual studio and doing a git reset put me into a good spot.
I.e. check to see if any processes are locking the folder/files.
As it is already said, these could happen when another process is already accessing the files, so closing any app that could potentially be using it will help, as of me Visual Code was opened and closing it did help.
I know the question is a little bit old, but as top answer suggests using --hard flag, which may not be necessary, I decided to post this.
Probable problem cause: some other process is using/locking file/folder you're trying to remove
Solution: stop this process, then try again. One way to found it is via Resource Monitor (way I've learned from here):
Open Task Manager
Navigate to Performance tab
Click Open Resource Monitor at the bottom
Navigate to CPU tab
At Associated Handles section, there's a search field - enter path to dir/file for which you're getting 'Permission denied' error
(Review and) stop any process that appeares as search result.
Just change permission to allow one or group user for this 'workedfolder' by this
sudo chmod u+w <exact_workedfolder_permission_denied>
or
sudo chown -R user_name:user_name <exact_workedfolder_permission_denied>
First, between every operation with git I would recommend doing a quick git status. I cannot tell you how many times this has saved me lots of headaches.
A couple of things I might try to shed some light on the problem(s):
You said you created directories and made lots of changes to b01, then pushed it to the origin. Look at the commit for this action `git show . Did you change permissions on the parent directory of "workfolder"? This might be of help.
Are you on windows? If you are, how deep in directory paths are you? Is the path length exceeding windows max? Reference here.
If none of these work, have you tried creating a throwaway branch off of b01, and merging "master"? This sometimes smokes out a cause.
The ugly hail-Mary. Have you tried making a copy/paste of "b01" outside the repository, rolling back to the matching last commit of "master", and then pasting the changes in the "b01" copy over master? When things are totally upside down and not making sense, sometimes it is easier to work backwards through the problem to figure out what went wrong and learn what not to do in the future.
In my case I just checked out to the offending commit using the commit hash rather than the branch name.
304/5000
The answer to that can be quite simple. Say you want to mash your branch with a master, you can do the following:
git checkout -b master-2 // from your current branch where all the changes are
git branch -D master
git branch -m master-2 master
git push origin master
Ready! Remember that this is in case you want to overwrite everything in the branch
We had a number of developers working on a large website project using Git. We have a GitHub repository and then we have the website on the server, plus all the developers have their local versions.
When we finally launched the project, I got lazy (hangs head in shame) and started making changes directly to the server, without pushing them back to the Github repo. However, other people made changes to the repo, for reasons I don't quite understand, that were never pushed down to the server and are now either outdated or wrong. We have been doing this for almost seven months.
Now the server and repo are hopelessly out of sync. I would now like to get the most updated version of the site (which is the server) back up to the Git repository so we can begin another round of development. I basically want to start with a fresh copy of what is on the server.
How would you recommend I proceed? That was the first time I had used Git. It didn't seem like such a big deal at the time but now seems like it is harder to start up again than I thought.
I have looked for instructions and don't really see anything that fits. Because I am not super confident in my Git skills, I am afraid to just start trying the few ideas I did find and losing what I have on the server.
(I know I could restore from a backup if I really messed it up but would prefer not to do that as it would take the site down.)
Can I uninstall git and start again with a fresh repo? Or is there a safe way to push the current version up to the repo?
Thanks for your help.
UPDATE: I found this answer elsewhere (Replace GitHub repo while preserving issues, wiki, etc) but I am not sure how to do this:
cd into "new" repository
git remote add origin git#github.com:myusername/myrepository (replacing myusername & myrepository accordingly)
git push --force origin master
Possibly delete other remote branches and push new ones.
Not sure what they mean by "new repository"
Make a new branch and push it to GH.
Make a new branch based on the previous
branch.
Switch to the new branch (created on #2).
Delete all the files and folders on this branch repository
except the .git folder and contains
(maintain the README.md,
.gitignore and other files if you want it).
Copy all the files from the server except
.git folder.
Commit.
Switch to local Master (created on #1)
Merge this new branch with the previous one.
Solve conflicts
(I use SmartGITthat have a visual conflict solver and helps me a lot, but you can use gitdiffif you don't want a visual interface)
Commit
Push it to GH.
I hope this helps
I figured this out. What I did was:
Make a new branch on Github to effectively store a backup.
$ git add . to stage all changes
$ git commit -m "Commit message" to commit changes
$ git push --force origin master to force changes from server to remote branch master
Once I did this, there were still hundreds of files I had deleted on the server that were not reflected on the remote github.com repository. I used the following:
$ git rm $(git ls-files --deleted)
See Removing multiple files from a Git repo that have already been deleted from disk
Then repeated git commit and git push. Now my github repo matches my server exactly.
I have not yet deleted the "backup" branch I created on github but I will.
Hope that helps someone.
I am beginner for gitHub. I have used svn before but I am not getting grip on github after going through many tutorials on the net. I have no idea about basic work flow. I created clone by using github. If I make any change in file suppose file_A then what is right step to push this file on server:
I am assuming :
git status->git commit->git pull -> git push
I have doubt that it push all project files on server But i was expecting to update only file_A. Please suggest me complete command/syntax with file path.
You are not pushing files but changes. So if you have cloned a repository with a lot of files and only changed one of them, you're only sending in the change to that one file. In your case that would be:
git clone git#github.com/some/repo .
git status # nothing has changed
vim file_A
vim file_B
git status # file_A and file_B have changed
git add file_A # you only want to have the changes in file_A in your commit
git commit -m "file_A something"
git status # file_B is still marked as changed
You can and should continue doing changes and commiting them until you're pleased with the result. Only then you should push the changes back to GitHub. This assures that everybody else cloning the repository in the meantime will not get your potientially broken work-in-progress.
git pull origin master
git push origin master
will send in all commits you made after you cloned the repository.
If I could make a suggestion - you might find it easier to not focus first on Github. Instead, if you have not already, go through an online book/tutorial for git (not Github) and learn the basics of git using the command line on your local machine and without involving a remote server or service like Github. In fact you do not even have to be connected to the Internet to learn much of git. This online book is excellent and starts at the beginning and teaches you how git works. Once you are confident in the basics you can start connecting to remote machines like Github.
Git Commands to update files in git:
To Add Upstream: (Initially You need to do it)
git remote add upstream
Save Local Changes to Temp
git stash save
Updating Local From Master
git pull --rebase upstream master
Apply Local changes Done earlier on latest Code taken from git master
git stash apply
To Update Fork:
git Status
git add "Resource Name to Add to Fork"
git Commit -m " Comments"
To Put these changes in Master:
git Push
Then Create Pull Request from Fork
Updating Fork From Master
git push origin master
Late yesterday I finished up a day's worth of work and went to github and commited, then hit the sync button. I briefly saw a message on the screen saying something about needing to fix merge conflicts, but the message was self-dismissed before I could read it completely.
My outstanding commit disappeared, so I figured all was well. This morning I started work again and ALL the new code that I did had been reverted to the prior commit from two days before. There is no mention of any of the changes I had made nor of the commit I did just before finishing up my work day... they are just gone.
Any suggestions on recovery?
UPDATE
For what it's worth, I was browsing the log files in my git folder and I see my commit:
37c661799950211c713630301cf8cbe609de6c59 f94366329fa93cec529b83b34a607449725f5270 [user info removed] 1360710557 -0700 commit: Shout page
f94366329fa93cec529b83b34a607449725f5270 b588ed19e77ef55611a4a82896c5ef2c26f3e4cd [user info removed] 1360710567 -0700 checkout: moving from master to b588ed19e77ef55611a4a82896c5ef2c26f3e4cd^0
b588ed19e77ef55611a4a82896c5ef2c26f3e4cd b588ed19e77ef55611a4a82896c5ef2c26f3e4cd [user info removed] 1360710576 -0700 rebase finished: returning to refs/heads/master
Try to use git reflog to find the hash of the lost commit. You can then either use git cherry-pick, git merge or git reset --hard (depending on what it is you want to do with the lost history) to recover. If you need to see diffs in order to identify your commit, use git reflog -p.
You probably did a local commit first, don't you? Check your local branch first:
git log --oneline
Not sure what you did, but if there was a merge conflict while pushing, you should still have your changes locally.