Skip to main content

Git Cheat Sheet / Guide

https://ittools.fifthdread.com/git-memo

 

Install Git on MacOS


brew install git

 

Install Git on Windows

https://git-scm.com/download/win

 

Set Config

git config --global user.name "[name]"
git config --global user.email "[email]"

example:

git config --global user.name "testuser"
git config --global user.email "email@emailtest.com"

Set Editor to nano on MacOS

git config --global core.editor "nano"
export GIT_EDITOR=nano
export VISUAL=nano
export EDITOR="$VISUAL"

Generate SSH Key

On Windows

ssh-keygen.exe -t rsa -b 4096

On MacOS / Linux

ssh-keygen -t rsa -b 4096

Press Enter - No Passphrase, leave empty

 

Take public key and uploadadd into Gitea

Key is located in /Users/<username>/.ssh

Take id_rsa.pub - Open in text editor. Copy contents.

Go to profile on Gitea HERE: https://gitea.fifthdread.com/user/settings/keys 

image.png

Add Key

Paste public key information into Content Box.

image.png

It should auto-complete the Key Name. Click Add Key to complete.

Your machine can now trusted to access the Git Repository via SSH. You can now pull and push to the repo.

Cloning a project / Pulling a Project

To Clone a project from the repo, go to the project on gitea.

Switch to SSH and click the Copy button to copy the SSH access URL.

image.png

Navigate to the directory you want to clone your project into. In the example below, I go to a ProjectFiles folder.

Type git clone "URL" "DirectoryName"

git clone <PASTE URL HERE> <BRANCH NAME HERE>

The branch name is not required. It is simply the name of the directory you are going to use for the files. I like to stay organized and make it the name of the branch.

image.png

it will now download the repo.


Renaming your local git branch

 

This is important, as if you do not rename your branch prior to pushing it to the repo, it will override the main branch. Not good!

Check what branch you are on, and what the branch name is:

git branch


Rename Main branch to something else via the below command:

git branch -m <NAME>

image.png

 


Branches

To upload changes to a project, make sure you are working on a separate branch- RENAME YOUR BRANCH 


Never upload directly to the main branch on a collaborative project, as branch merges need to be reviewed by the team prior to being merged.

 

Committing Changes

First verify you have renamed the Branch

git branch

If it is something other than main, we can proceed.

image.png


To see the changes you made since the repo was pulled, type:

git diff

image.png

Commit the changes with the following command:


git commit -a

image.png


The NANO editor will appear if on MacOS, or your preferred editor on Windows, such as Notepad++. Edit the top of the document with your commit comments. Save when complete.

If you opened in VIM and not nano, it looks like this... ew. Don't ask me how VIM works.

image.png

This is what it should look like if NANO opens the file.

image.png

CTRL+X to exit, and yes to save

image.png

This indicates success.


To check the status of your branch.

git status

image.png

 

Uploading your new Branch / Pushing Changes

Again, make sure you aren't on main branch.

git branch

Now type in the following to upload your changes!

git push origin HEAD

This will upload a branch based off the name defined in "git branch"

image.png

Alternatively, define a branch name in the command.

git push origin HEAD:branchtestname

image.pngDone!

 

Looking at different branches on Gitea

 

Go to Branches. Click on New Branch from Main.

image.png

 

In this example, I made a test branch called another_mac_test

image.png

See how it is selected here- it shows "another_mac_test" indicating we are viewing the new branch.

 

Pull Requests

When you have made changes and want to pull them into the main branch. Open Gittea, go to branches, and click New Pull Request on your branch.

image.png

 

You will open a pull request with all your changes outlined. Click on New Pull Request again.

image.png

You will now comment on the pull request, then click Create Pull Request.

image.png

 

The request will now be reviewed.




Pull Requests

Pull Requests must be reviewed prior to merging. You can see the "1" indicating 1 request is waiting for review.

image.png


Clicking on Pull Requests shows the waiting requests.

image.png

This pull request can now be reviewed and merged.

Screenshot 2023-09-25 at 1.16.15 PM.jpg

The branch can also be deleted after it is merged with the main branch successfully.

You're done!