Useful Git Commands

Commands to discard unstaged changes

git restore .

To discard unstaged changes for a specific file

git restore path/to/file/to/revert

Commands to discard all local changes that haven’t been committed:

  1. git reset –hard

  2. git clean -fxd

Switch branch

git switch name_of_branch

Delete branch

git branch -d name_of_branch

Force Delete Branch Where Branch Has Not been Merged (should roll back local changes first)

git branch -D name_of_branch

Using Git To Deploy Laravel App From A Private Git Repository

The first step is to clone a private repository is to connect to the remote server. For this example I’ll call the user - “user1” (very original).

ssh user1@ip_address

If you have not yet generated an ssh key for this user, you must do it now:

ssh-keygen - this generates a ssh. Do not use sudo.

Then cd .ssh - this will switch you into the hidden directory .ssh

cat id_rsa.pub - will show contents of public key

copy the contents of the public key.

Switch to the repository in github that you want to clone, then go to settings -> Deploy keys.

Click on the button Add deploy key

Give this key a title and paste in the contents of the public key for user1.

Go back to the remote server to the home directory of user1 - /home/user1.

mkdir repository (this will create a directory called repository) - do not use sudo. Because you are in you user’s home directory, you do not use sudo. Otherwise, the directory will belong to root and not to the actual user.

Remember, you did NOT use sudo to create the user’s ssh key. If you did, you will need to delete the public and private keys using sudo and then regenerate the keys without sudo.

cd repository - this is the directory where the github repository will be cloned.

The command to clone a repository is git clone git_address.

For my project it is:

git clone git@github.com:murwell/shopping.git

To clone without the top level folder shopping.

git clone git@github.com:murwell/shopping.git . - Notice the period after the space.

Do not use sudo.

This cloned the project to \home\user1\repository.

Done cloning.

Now, the cloned app is in \home\user1\repository\shopping.

cd \home\user1\repository - this switched me into the repository directory.

Since, sudo was not used to create the directory, the ownership of the directory is user1:user1.

Group permissions need to be worked out. This is something I still need to work out.

I gonna go back to the tutorial by Decode Web.

The tutorial is a few years old, so I don’t know what’s changed since then. Hopefully, nothing.

Here’s a different tutorial from Kenean that discusses how to automate a laravel app deployment using Github actions.

Visit Emlekezik.com