DEV Community

svhl
svhl

Posted on • Edited on

Some useful Git commands

Git is an essential tool in every developer's workflow. This article covers a few Git commands — some common, some not so much. Some of these commands are specific to GitHub, so make the necessary changes if you're using a different platform like GitLab.

I've also provided a wrapper script here that shortens the commands for ease of use. Download the script to ~/.local/bin. A few changes need to be made to the script, which will be mentioned below.

Clone repo

  • Using Git
git clone https://github.com/[user]/[repo] --branch [branch]
Enter fullscreen mode Exit fullscreen mode
  • Using script
git copy [user]/[repo] [branch]
Enter fullscreen mode Exit fullscreen mode

Clones the specified branch of the user's GitHub repo to your local machine. If you want to clone the default branch (usually main or master), the branch argument isn't necessary.

Set remote repo

  • Using Git
git remote set-url origin https://[token]@github.com/[user]/[repo]
Enter fullscreen mode Exit fullscreen mode
  • Using script
git sync [user]/[repo]
Enter fullscreen mode Exit fullscreen mode

Since "support for password authentication was removed" on GitHub, you'll need to use a token in order to commit. From GitHub, go to Settings > Developer settings > Personal access tokens > Tokens (classic) to create a new token. From the dropdown, select Generate new token (classic), set the parameters, and hit Generate token.

Set the token to commit to the user's repo. If you're using the script, set the TOKEN variable within the script.

Undo last commit

  • Using Git
git reset --hard HEAD~1
git push origin HEAD --force
Enter fullscreen mode Exit fullscreen mode
  • Using script
git undo
Enter fullscreen mode Exit fullscreen mode

Revert to the state before the last commit. Requires you to be authorized to do so.

One more thing...

Here's how to properly commit via Git.

git config --global [username]
git config --global [email]
Enter fullscreen mode Exit fullscreen mode

where [email] is your noreply email address found on GitHub at Settings > Emails.

Then, generate a new GPG key.

gpg --full-generate-key
Enter fullscreen mode Exit fullscreen mode

When prompted, use the defaults. Set username and email, and also a password (this will only be stored locally).

Run the below command and copy the GPG key ID.

$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot <hubot@example.com>
ssb   4096R/4BB6D45482678BE3 2016-03-10
Enter fullscreen mode Exit fullscreen mode

In this case, the [id] is 3AA5C34371567BD2. Next, run

gpg --armor --export [id]
Enter fullscreen mode Exit fullscreen mode

Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.

To add this key to your GitHub account, go to Settings > SSH and GPG keys > New GPG key. In the key field, paste the above copied text.

Finally, to always sign commits, run the below commands.

git config --global commit.gpgsign true
git config --global user.signingkey [id]
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
taslim_shaik_b6490b29bb9b profile image
Taslim Shaik

Git reset
Git revert
Git merge