Lasted edited: January 21, 2025

A work in progress. Maybe.

I haven’t been coding for a year or two. Just been doing other important stuff like playing Fortnite.

Anyway, whenever I a take a break coding, it seems that all of the previously gained wisdom has disappeared. On a positive note, in the last couple of years ai coding has taken off and I’m gonna see how I can use chatgpt, cursor, gemini, and so on.

But I thought that I should start with refreshing my GitHub commands.

Learning Github

Github is a way to track changes to files. At this point, I’m hoping to use Github as follows:

  1. Develop Locally with VSCode.
  2. Push code to github
  3. Push code from github to server.

At this point, I have no idea what I’m doing.

Time for a Github Tutorial.

Github One Hypen Versus Hyphen

Before I begin, I want to point out that sometimes a github command uses one hyphen (-) and aometimes two hyphens (–).

One hyphen is used for abbreviated flags. Two hyphens are used for complete word flags.

Abbreviated flag

git add -a

Complete word flag

git add --all

Github Intro

So, I tried using the github extension in vscode and it was too confusing. Instead, I used the command line.

At this point, my laravel project was contained on my local computer. Let’s say it’s in /home/laravel_app.

With github, I want to track different versions. To do that,Github uses a concept called repository.

A repository contains all of your project’s files and each file’s revision history. You can discuss and manage your project’s work within the repository.

Git Hub Docs

So, to get it working a little, I created an empty repository on github that is called shopping.

Then I’ll push files from /home/laravel_app to the repository on github. The address to use to push files to Github is:

https://github.com/user_name/repository_name.git`

So for me, it will be:

https://github.com/murwell/shopping.git

Local Github Repository

Now, I have to set up a repository on my local computer. This is done in the command line.

Change into the folder of where the laravel is stored:

cd /home/laravelapp

Laravel already contains a file in root folder called .gitignore. The purpose of this file to tell git to omit certain files and folders so that they do not get pushed/pulled to github. For example, .env is included in .gitignore because it contains sensitive info.

This is the contents of laravel.gitignore.

/node_modules
/public/build
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
/.idea
/.vscode

After making sure that you app folder has a .gitignore folder, run the following command while in /home/laravel_app folder to create a hidden folder called git:

git init

After you run git init, it’ll ask you if you want to rename master. You should because the main branch on github is called main.

The local branch should be renamed to “main” with:

git branch -M main

With a -m or -M option, oldbranch will be renamed to newbranch. If oldbranch had a corresponding reflog (reference log], it is renamed to match newbranch, and a reflog entry is created to remember the branch renaming. If newbranch exists, -M must be used to force the rename to happen.

See git-scm.com

Then run the following commands and substitute your name and email address between the quotation marks (“ “).

git config --global user.name "user_name"

git config --global user.email "user@email.com"

This will add your name and email so that if you’re working with someone, you can see who made the changes.

Now, you have to add the local files to your local repository. Since you are in the folder of the laravel app, run:

git add --all                                                   

This will add all the files and folders in the directory to the local repository. It will ignore the files and folders listed in .gitignore.

These files are added but sill need to be committed. Commit will not work unless a file has been added prior to running it. (i.e. -git add <FILENAME>)

This process of adding a file is called staging. Commit comes after stage (add).

git commit creates a commit, which is like a snapshot of your repository. These commits are snapshots of your entire repository at specific times. You should make new commits often, based around logical units of change. Over time, commits should tell a story of the history of your repository and how it came to be the way that it currently is. Commits include lots of metadata in addition to the contents and message, like the author, timestamp, and more. Commits are the building blocks of “save points” within Git’s version control.

Github Docs

git commit -m "Committing initial files to the repository"   

The flag -m indicates that you’re going to add a message to the commit so you can explain why you’re taking the snapshot.

Common Commit Commands

From Github Docs:

git commit

This starts the commit process, but since it doesn’t include a -m flag for the message, your default text editor will be opened for you to create the commit message. If you haven’t configured anything, there’s a good chance this will be VI or Vim. (To get out, press esc, then :w, and then Enter. :wink:)

git commit -m  "descriptive commit message" 

This starts the commit process, and allows you to include the commit message at the same time.

git commit -am "descriptive commit message" 

In addition to including the commit message, this option allows you to skip the staging phase. The addition of -a will automatically stage any files that are already being tracked by Git (changes to files that you’ve committed before).

git commit --amend 

Replaces the most recent commit with a new commit.

git branch

Shows the branches with an asterisk next to the active branch

git branch name_of_branch

Creates branch that is called “name_of_branch”

git branch -d name_of_branch

Deletes branch if merged (only use lowercase “d” to prevent a delete of a branch that has not yet been merged)

git branch -m new_name

Renames the active branch to “new_name”

git checkout name_of_branch

Switches to the “name_of_branch”

git checkout -b new_branch

Creates a new branch with the name “new_branch” and switches to that branch.

To merge branches with main

git checkout main

Switches to main

git merge branch_to_be_merged

Merges branch to main

git branch -d branch_to_be_merged 

Deletes branch that has been merged

Adding Remote Repository

So far, I’ve been mainly working with the local repository.

I previously made a repository on Github that I want to now use to sync my local files.

The remote github repository is:

https://github.com/murwell/shopping.git

To add this remote repository info to my local repository so that the two can be synced, use:

git remote add <remote_name> <remote_url>

Traditionally, origin is used as the remote name. You can use anything. Recommend to stay with origin.

git remote add origin https://github.com/murwell/shopping.git

Now, to push the files:

git push origin main

This pushes to the main branch on my remote repository.

At this point, these two commands should work if all I’ve done is change exisiting files.

git commit -am "add message"
git push

If I’ve added a file, I should run:

git add -a
git commit -am "add message"
git push

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

Google Gemini’s Summary On How To Use Git

I thought I’d ask Gemini for a summary on how to use Git. Here’s its response.


**1. Initialization:**

*   **Create a Git Repository:** If you're starting a new project, initialize a Git repository in your project's directory using:
    ```bash
    git init
    ```

**2. Staging Changes:**

*   **Track Changes:** Use `git add` to stage files or directories that you want to include in your next commit. 
    *   `git add .` stages all changes in the current directory.
    *   `git add <filename>` stages a specific file.

**3. Committing Changes:**

*   **Create a Snapshot:** Use `git commit` to create a snapshot of your staged changes. 
    *   Include a descriptive message explaining the changes you made.
        ```bash
        git commit -m "Your descriptive commit message"
        ```

**4. Working with Branches:**

*   **Create a Branch:** Create a new branch to work on a feature or experiment in isolation:
        ```bash
        git branch <branch_name> 
        ```
*   **Switch Branches:** Switch between branches using:
        ```bash
        git checkout <branch_name>
        ```
*   **Merge Branches:** Merge changes from one branch into another:
        ```bash
        git merge <branch_name> 
        ```

**5. Pushing Changes to Remote Repository:**

*   **Set Up Remote:** If you're working with a remote repository (like GitHub, GitLab, or Bitbucket), add it as a remote:
        ```bash
        git remote add origin <remote_repository_url>
        ```
*   **Push Changes:** Push your local commits to the remote repository:
        ```bash
        git push origin <branch_name>
        ```

**6. Pulling Changes from Remote Repository:**

*   **Fetch and Merge:** Fetch changes from the remote repository and merge them into your current branch:
        ```bash
        git pull origin <branch_name> 
        ```

**7. Resolving Conflicts:**

*   **Handle Merge Conflicts:** If Git encounters conflicting changes during a merge, you'll need to manually resolve them by editing the affected files and then:
        ```bash
        git add <resolved_files>
        git commit -m "Resolved merge conflicts"
        ```

**8. Viewing History:**

*   **See Commit History:** Use `git log` to view the history of commits in your repository.

**Key Concepts:**

*   **Repository:** A central location where your code and its history are stored.
*   **Commit:** A snapshot of your code at a particular point in time.
*   **Branch:** An independent line of development.
*   **Merge:** Combining changes from different branches.

This is a basic overview. Git has many more features and commands. I recommend exploring the official Git documentation for a deeper understanding.

I hope this helps! Let me know if you have any other questions.

Gemini’s answer is just a little too complicated for me.

Anyway here is how I Deploy Laravel App From A Private Git Repository

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.

Procedure To Update the App Where Git Is Used

Again, this tutorial from laravel daily helped.

cd /var/www/html/laravelapp

Changes into the laravel app directory

php artisan down

Shuts down laravel app for web users

cd .. 

Moves into /var/www/html

sudo chown -R user1:user1 laravelapp

Changes ownership of laravelapp directory to user1. Change user1 to the actual name of the user. Also, this is cumbersome. I will probably add user1 to the www-data to stopping these steps.

cd laravelapp

Changes directory into laravel app

git pull

Pulls the changes from to Git

composer install

Adds in any new dependencies

If you have created some new migrations, this is the time to run php artisan migrate

npm run build

Rebuilds the app. The first time I had made changes, I forgot to do this step. Without running npm run build, changes were not being seen by the web user.

cd ..

Changes directory up one level to /var/www/html

sudo chown -R www-data:www-data laravelapp

Changes ownership of laravelapp directory back to www-data

cd laravelapp

Changes directory back into laravelapp directory

php artisan up

Puts the laravel app back up

TO BE CONTINUED

Visit Emlekezik.com