This article presents a few commands that I use from time to time to cover my programming mistakes or just shortcut things in my GIT repositories.
I must warn beginners (and not only) to double-check the effects using test repositories before applying the mentioned commands on production repositories.
Thanks for reading! - Content provided by App Generator.
GIT - Drop local changes
Warn: you might lose valuable work
$ git reset --hard origin/master
GIT - Reset to Commit
Warn: All commits above the used one are lost
$ # We assume the Commit hash is '288c168' $ git reset --hard 288c168 $ git push --force origin master
GIT - Print modified files
This command is the safest one and I'm using it a lot to scan for changes.
$ # List all changes $ git add -A -n $ $ # Filter the output using grep $ git add -A -n | grep 'something'
GIT - Ignore files permissions on DIFF
This case might occur if the development PC uses Windows and production is on Linux. To extract the relevant changes and ignore the permissions of the files, the below command can be used:
$ # Configure GIT to ignore changes on files perms $ git config core.filemode false
GIT - Update single (LOCAL) file
Useful when we need to update a single local file with the remote version. The update can be done from any branch - our sample command uses the project master branch.
git checkout origin/master -- app/some-file.html
GIT - Update forked project
This is useful when we want to update a forked project with the latest changes from the parent project.
$ # CD inside the forked project $ cd forked-project $ $ # Fetch all branches of remote upstream $ git fetch upstream $ $ # Rewrite your master with upstream’s master using git rebase. $ git rebase upstream/master $ $ # Save changes to our forked project $ git push origin master --force
- GIT - official website
- Top 20 Git Commands - with Examples
Thank you! Btw, my (nick)name is Sm0ke and I'm pretty active also on Twitter.
Top comments (8)
You probably know this, but they're not really lost.
Hello. I don't know any reverse command, to be honest.
If you see this message and have the time, ... thanks.
Well, the reset only moves which commit the branch is pointing to. The commit still exists. The force push does the same to the remote.
As long as that commit is still reachable from another tag or branch it will never be removed.
Even if it is unreachable, the commit can be recovered by creating a branch that points to it.
The commit can be found using the reflog or other commands which find unreachable commits.
Ty! We have a GIT master here.
Definitely not a master!
Just someone who's read a lot of git related stackoverflow questions...
git restoreis the new command to restore files and it replaces both reset and checkout in some cases.
git restore <filepath> --source=<source>
Using push with --force will not warn or stop you if someone else has changed the upstream. Use push
--force-with-leaseinstead to avoid losing other's committs.
You have to love git reset