GIT - Unorthodox commands I use from time to time

Sm0ke - Dec 2 '20 - - Dev Community

Hello Coders,

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Git resources


Thank you! Btw, my (nick)name is Sm0ke and I'm pretty active also on Twitter.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player