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 AppSeed - a platform used by 2k+ developers across the globe.
Warn: you might lose valuable work
$ git reset --hard origin/master
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
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'
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
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
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
Thank you! Btw, my (nick)name is Sm0ke and I'm pretty active also on Twitter.