I just cloned a git repository and checked out a branch. I worked on it, and then decided to remove all my local changes, as I wanted the original copy.
In short, I
Use:
git checkout -- <file>
To discard the changes in the working directory.
The best way is checking out the changes.
Changing the file pom.xml in a project named project-name you can do it:
git status
# modified: project-name/pom.xml
git checkout project-name/pom.xml
git checkout master
# Checking out files: 100% (491/491), done.
# Branch master set up to track remote branch master from origin.
# Switched to a new branch 'master'
Reason for adding an answer at this moment:
So far I was adding the conclusion and ‘answers’ to my initial question itself, making the question very lengthy, hence moving to separate answer.
I have also added more frequently used git commands that helps me on git, to help someone else too.
Basically to clean all local commits
$ git reset --hard
and
$ git clean -d -f
First step before you do any commits is to configure your username and email that appears along with your commit.
#Sets the name you want attached to your commit transactions
$ git config --global user.name "[name]"
#Sets the email you want atached to your commit transactions
$ git config --global user.email "[email address]"
#List the global config
$ git config --list
#List the remote URL
$ git remote show origin
#check status
git status
#List all local and remote branches
git branch -a
#create a new local branch and start working on this branch
git checkout -b "branchname"
or, it can be done as a two step process
create branch: git branch branchname
work on this branch: git checkout branchname
#commit local changes [two step process:- Add the file to the index, that means adding to the staging area. Then commit the files that are present in this staging area]
git add <path to file>
git commit -m "commit message"
#checkout some other local branch
git checkout "local branch name"
#remove all changes in local branch [Suppose you made some changes in local branch like adding new file or modifying existing file, or making a local commit, but no longer need that]
git clean -d -f
and git reset --hard
[clean all local changes made to the local branch except if local commit]
git stash -u
also removes all changes
Note:
It's clear that we can use either
(1) combination of git clean –d –f
and git reset --hard
OR
(2) git stash -u
to achieve the desired result.
Note 1: Stashing, as the word means 'Store (something) safely and secretly in a specified place.' This can always be retreived using git stash pop. So choosing between the above two options is developer's call.
Note 2: git reset --hard
will delete working directory changes. Be sure to stash any local changes you want to keep before running this command.
# Switch to the master branch and make sure you are up to date.
git checkout master
git fetch
[this may be necessary (depending on your git config) to receive updates on origin/master ]
git pull
# Merge the feature branch into the master branch.
git merge feature_branch
# Reset the master branch to origin's state.
git reset origin/master
#Accidentally deleted a file from local , how to retrieve it back?
Do a git status
to get the complete filepath of the deleted resource
git checkout branchname <file path name>
that's it!
#Merge master branch with someotherbranch
git checkout master
git merge someotherbranchname
#rename local branch
git branch -m old-branch-name new-branch-name
#delete local branch
git branch -D branch-name
#delete remote branch
git push origin --delete branchname
or
git push origin :branch-name
#revert a commit already pushed to a remote repository
git revert hgytyz4567
#branch from a previous commit using GIT
git branch branchname <sha1-of-commit>
#Change commit message of the most recent commit that's already been pushed to remote
git commit --amend -m "new commit message"
git push --force origin <branch-name>
# Discarding all local commits on this branch [Removing local commits]
In order to discard all local commits on this branch, to make the local branch identical to the "upstream" of this branch, simply run
git reset --hard @{u}
Reference: http://sethrobertson.github.io/GitFixUm/fixup.html
or do git reset --hard origin/master
[if local branch is master]
# Revert a commit already pushed to a remote repository?
$ git revert ab12cd15
#Delete a previous commit from local branch and remote branch
Use-Case: You just commited a change to your local branch and immediately pushed to the remote branch, Suddenly realized , Oh no! I dont need this change. Now do what?
git reset --hard HEAD~1
[for deleting that commit from local branch. 1 denotes the ONE commit you made]
git push origin HEAD --force
[both the commands must be executed. For deleting from remote branch]. Currently checked out branch will be referred as the branch where you are making this operation.
#Delete some of recent commits from local and remote repo and preserve to the commit that you want. ( a kind of reverting commits from local and remote)
Let's assume you have 3 commits that you've pushed to remote branch named 'develop
'
commitid-1 done at 9am
commitid-2 done at 10am
commitid-3 done at 11am. // latest commit. HEAD is current here.
To revert to old commit ( to change the state of branch)
git log --oneline --decorate --graph
// to see all your commitids
git clean -d -f
// clean any local changes
git reset --hard commitid-1
// locally reverting to this commitid
git push -u origin +develop
// push this state to remote. + to do force push
# Remove local git merge: Case: I am on master branch and merged master branch with a newly working branch phase2
$ git status
On branch master
$ git merge phase2
$ git status
On branch master
Your branch is ahead of 'origin/master' by 8 commits.
Q: How to get rid of this local git merge? Tried git reset --hard
and git clean -d -f
Both didn't work.
The only thing that worked are any of the below ones:
$ git reset --hard origin/master
or
$ git reset --hard HEAD~8
or
$ git reset --hard 9a88396f51e2a068bb7
[sha commit code - this is the one that was present before all your merge commits happened]
#create gitignore file
touch .gitignore
// create the file in mac or unix users
sample .gitignore contents:
.project
*.py
.settings
Reference link to GIT cheat sheet: https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf
I think git has one thing that isn't clearly documented. I think it was actually neglected.
git checkout .
Man, you saved my day. I always have things I want to try using the modified code. But the things sometimes end up messing the modified code, add new untracked files etc. So what I want to do is, stage what I want, do the messy stuff, then cleanup quickly and commit if I'm happy.
There's git clean -fd
works well for untracked files.
Then git reset
simply removes staged, but git checkout
is kinda too cumbersome. Specifying file one by one or using directories isn't always ideal. Sometimes the changed files I want to get rid of are within directories I want to keep. I wished for this one command that just removes unstaged changes and here you're. Thanks.
But I think they should just have git checkout
without any options, remove all unstaged changes and not touch the the staged. It's kinda modular and intuitive. More like what git reset
does. git clean
should also do the same.
git reset --hard
This command will completely remove all the local changes from your local repository. This is the best way to avoid conflicts during pull command, only if you don't want to keep your local changes at all.
If you want to pull the new changes from remote and want to ignore the local changes during this pull then,
git stash
It will stash all the local changes, now you can pull the remote changes,
git pull
Now, you can bring back your local changes by,
git stash pop
As with everything in git there are multiple ways of doing it. The two commands you used are one way of doing it. Another thing you could have done is simply stash them with git stash -u
. The -u
makes sure that newly added files (untracked) are also included.
The handy thing about git stash -u
is that
git stash pop
(it's like deleting an email in gmail where you can just undo if you change your mind afterwards)As of your other question git reset --hard
won't remove the untracked files so you would still need the git clean -f
. But a git stash -u
might be the most convenient.