DEV Community

jmau111⚡⚡⚡
jmau111⚡⚡⚡

Posted on

Git: a few shortcuts and their equivalents

Git has special commands that can save some time. It's usually the equivalent of a series of basic commands.

Renaming a file

You can do:

mv script.py myscript.py
git add myscript.py
git rm script.py
Enter fullscreen mode Exit fullscreen mode

or simply:

git mv script.py myscript.py
Enter fullscreen mode Exit fullscreen mode

Note that you'll have to commit after anyway, but the shortcut looks nice.

Creating a new branch

You might want to do the following:

git branch {BRANCH}
git checkout {BRANCH}
Enter fullscreen mode Exit fullscreen mode

or simply:

git checkout -b {BRANCH}
Enter fullscreen mode Exit fullscreen mode

or, if you have Git 2.23.0, at least:

git switch -c {BRANCH}  # -c is for "create"
Enter fullscreen mode Exit fullscreen mode

Changing remotes

You can do:

git rm origin
git remote add origin {NEW_ORIGIN}
Enter fullscreen mode Exit fullscreen mode

or:

git remote set-url origin {NEW_ORIGIN}
Enter fullscreen mode Exit fullscreen mode

Amending a commit

Suppose you want to modify a file in a previous commit, you might do:

git add myfile.c
git commit --amend
Enter fullscreen mode Exit fullscreen mode

Then, you would get a prompt with the commit message to record your modifications.

However, if you don't need to modify the commit message (and your Git version is greater than 1.7.9), you can do:

git add myfile.c
git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

The --no-edit will skip the prompt that is used to edit the commit message.

Top comments (0)