DEV Community

Cover image for GIT - Unorthodox commands I use from time to time
Sm0ke
Sm0ke

Posted on • Updated on

GIT - Unorthodox commands I use from time to time

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.

Top comments (8)

Collapse
 
evolutionxbox profile image
Jonathan Cousins

Warn: All commits above the used one are lost

You probably know this, but they're not really lost.

Collapse
 
sm0ke profile image
Sm0ke

Hello. I don't know any reverse command, to be honest.
If you see this message and have the time, ... thanks.

Collapse
 
evolutionxbox profile image
Jonathan Cousins

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.

Thread Thread
 
sm0ke profile image
Sm0ke

Ty! We have a GIT master here.

Thread Thread
 
evolutionxbox profile image
Jonathan Cousins

Definitely not a master!
Just someone who's read a lot of git related stackoverflow questions...

Collapse
 
kaddkaka profile image
kaddkaka

git restore is 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-lease instead to avoid losing other's committs.

Collapse
 
sm0ke profile image
Sm0ke

Noted, Ty!

Collapse
 
ericcurtin profile image
Eric Curtin

You have to love git reset