Tech with Tyler
LinkedInGitHubYouTube
  • 👋Welcome!
    • whoami
    • !!! Disclaimer !!!
  • 🎓Academy
    • AWS Security Cookbook by Tyler
      • AWS Control Tower
        • Lab: Deploying AWS Control Tower via Terraform
      • AWS CloudTrail
      • AWS GuardDuty
        • Lab: Deploying AWS GuardDuty via Terraform
        • Lab: Logging GuardDuty Findings to S3
        • Lab: Adversary Simulation Detection with Stratus Red Team and GuardDuty
      • AWS Organizations
        • Lab: Deploying AWS Organizations via Terraform
      • AWS Root Account Management
        • Lab: Deploying AWS Root Account Management via Terraform
      • AWS Service Control Policies (SCPs)
        • Lab: Deploying AWS Service Control Policies (SCPs) via Terraform
      • TBD - Coming Soon!
        • [TBD] AWS Account Factory
        • [TBD] AWS Identity Center
    • My content on Cybr
      • Course - Terraform on AWS: From Zero to Cloud Infrastructure
      • Lab - Create Static AWS S3 Website with Terraform
      • Lab - Secure EC2 Access with SSM Session Manager and KMS
      • Lab - Encrypt and Decrypt Data with KMS and Data Encryption Keys
    • My content on PwnedLabs
      • Cyber Range - Electra
      • Lab - Abusing Identity Providers in AWS
      • Lab - Prowler and AWS Security Hub
      • Blog - Abusing Identity Providers in AWS
      • Blog - Building Security Guardrails with AWS Resource Control Policies
      • Blog - Defending Against the whoAMI Attack with AWS Declarative Policies
    • My content on YouTube
      • AWS Security Engineering
      • Linux in 60 Seconds!
  • ☁️Cloud Security
    • AWS Attacks and Techniques
      • Enumerate AWS Account IDs
      • Enumerate AWS IAM Users
      • Enumerate (Unauthenticated) IAM Users and Roles
      • Enumerate AWS Public Resources
      • Enumerate Secrets in AWS
      • Generate AWS Console Session
      • Generate IAM Access Keys from CloudShell
      • Password Spraying AWS IAM Users
      • Subdomain Takeovers
    • AWS Privilege Escalation
      • Identity Access Management (IAM)
      • IAM Trust Policies
      • Key Management Service (KMS)
      • Lightsail
      • OpenID Connect (OIDC)
      • S3
      • Secrets Manager
      • Security Token Service (STS)
    • AWS General Info
      • Amazon Bedrock
      • EC2
      • KMS
      • S3
      • SNS Topic
    • AWS CLI Cheat Sheet
    • Capture the Flags (CTFs)
      • Flaws.Cloud
        • Level 1
        • Level 2
        • Level 3
        • Level 4
        • Level 5
        • Level 6
      • PwnedLabs
        • Escalate Privileges by IAM Policy Rollback
        • Exploiting Weak S3 Bucket Policies
        • Leveraging S3 Bucket Versioning
        • S3 Enumeration Basics
        • Pillage Exposed RDS Instances
        • EC2 SSRF Attack
        • Hunt for Secrets in Git Repos
      • Cybr
        • Challenge - Secrets Unleashed
    • Tools
      • Tooling Index
      • dsnap
      • Pacu
      • s3-account-search
      • GoAWSConsoleSpray
      • aws_consoler
      • cloudenum
  • 📦Containers & Orchestration
    • Kubernetes
  • 👨‍💻Coding & CLI Tooling
    • CLI Tools
      • AWS CLI
      • Git
      • GitHub Copilot (CLI)
      • Homebrew
      • jq
      • ngrok
      • ssh
    • Coding and Scripting
      • Bash
      • Python
    • Terminal Customization
  • ⚙️DevSecOps
    • CI/CD
      • GitLab
    • Hashicorp Terraform
    • Hashicorp Vault
    • IAC Scanning
      • tfsec
    • Secrets Scanning
      • Trufflehog
  • 🎁Miscellaneous
    • Jenkins
  • 💻Operating Systems
    • Linux
      • APT Package Manager
      • CLI Tools Cheat Sheet
      • Man Pages
      • Services
      • Users and Groups
  • 🏗️Projects
    • Active Directory Homelab Automation
    • AWS Cloud Resume Challenge
    • Proxmox Homelab as Code
  • 📌Other
    • Useful Resources
Powered by GitBook
On this page
  • What is Git?
  • Install
  • Pushing Local Code to a Remote Repository
  • Cloning Repositories Locally
  • Merging Code
  • Merge Conflicts
  • Fork
  • Rebasing
  • Merging
  • Rebasing
  • Squashing Commits
  • Cherry Picking
  • Reverting Commits
  • Keeping Commit History
  • Undoing Commit
  • Stashing
  • Reflog
  • Useful Commands
  • General
  • Branches
  • Modify Terminal to show current git branch

Was this helpful?

  1. Coding & CLI Tooling
  2. CLI Tools

Git

What is Git and how to use it.

PreviousAWS CLINextGitHub Copilot (CLI)

Last updated 11 months ago

Was this helpful?

What is Git?

  • Git is a popular open-source version control tool commonly used for code but can be used with all types of files

  • More information about Git can be found on its

Install

  • Git is typically installed by default on Linux distributions and MacOS however it will need to be installed on Windows

    • Windows: winget install git.git

    • Linux: sudo apt install git

    • MacOS: brew install git

Pushing Local Code to a Remote Repository

  • With an empty remote repository, we can add local code to it

# set the origin to the remote repo
git remote add origin <repoUrl>

# push local code to the remote main branch
git push -u origin main

Cloning Repositories Locally

  • We can download a repository locally if we have permission

# via HTTPS
git clone <URL> 

# via SSH
git clone <sshLink>

Merging Code

  • When you want to merge your changes to a repo

  • GitLab - Merge Request (MR)

  • GitHub - Pull Request (PR)

Merge Conflicts

  • This can occur during a merge when your file is different than an existing file and git is unable to determine which changes should be committed.

  • The error may look like this after running git push origin <branchName>

! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'http://gitlab.com/tyler/myproject.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  • To fix, run git pull origin <branchName> (the conflicting branch) and identify the problem file(s) in the output.

[snip]
CONFLICT (add/add): Merge conflict in file.txt
Auto-merging file.txt
Automatic merge failed; fix conflicts and then commit the result.
  • Open the file (GUI, IDE, etc.) and select the data to keep removing all the git added stuff and leaving only the data you want.

# file.txt

<<<<<<< HEAD # your changes
My changes are here!
=======
The data in the branch!
>>>>>>> 2lk3lm23lrl2k3ml2m3l2n3ln2l3lsdkglsadg # the commit hash on the remote branch
# file.txt

My changes are here!

Fork

  • Someone with READ access can download your repo/code, make changes, and then open a Merge Request for you to approve

Rebasing

  • When the branch you're working on is behind commits from others

Merging

  • Running this command from your branch brings changes from others into your branch and keeps the git commit history intact

git merge main

Rebasing

  • Running this command moves your branch on top of the updated branch, changing the git commit hashes from others.

  • Not a big deal but your coworkers may not like this due to the commit history changing

git rebase main

Squashing Commits

  • When you have multiple commits and want to squash them into one

  • The final commit will be Addng new feature

# view commit history
git log

# squash the last 3 commits into one
git rebase -i HEAD~3

# replace `pick` with `squash`
pick 3h3ih Adding new feature
squash 4j69j Fixing typo
squash 6k49g Adding change
  • If you've already deployed a commit to a remote branch, but now need to revert it or change it, here's how you can squash them,

# view commit history
git log
commit ljsdflhsd3o2h8hd
Author: <author>
Date: <date>

    <latest commit message>
    
commit 9sdfh98sh9f839hh3
Author: <author>
Date: <date>

    <previous commit message>
  • Let's say you want to squash the new changes into the old commit

# select last two commits
git rebase -i HEAD~2

# merge remote branch into yours
git pull 

# push latest commit to remote branch
git push

Cherry Picking

  • When you want to incorporate a particular commit from another branch into your branch

git cherry-pick <commitHash>

Reverting Commits

  • You've committed some changes and now want to undo them

Keeping Commit History

  • Removing the commit shows the file(s) as deleted in the commit history

git revert <commitHash>

Undoing Commit

  • This undoes your commit

  • Use --soft to undo the commit but keep your modified file(s)

  • Use --hard to undo the commit and delete your modified file(s)

# remove last commit ~1
git reset --soft HEAD~1
git status

    On branch featureA
    Changes to be committed:
        added:    file.txt
    

# remove last commit ~1
git reset --hard HEAD~1
git status

    On branch featureA
    Nothing to commit

Stashing

  • Storing your code when you don't want to commit it yet but need to hop to another branch and don't want your modified code to come with you

# stash code
git stash

# list code kept in stash
git stash list

    stash@{0}: WIP on main: added new feature X (1 hour ago)
    stash@{1}: Bugfix for issue#123 (2 days ago)
    stash@{2}: Update README.md (yesterday)


# show contents of stash
git stash show stash@{0}

    diff --git a/new_feature.py b/new_feature.py
    new file
    +++ b/new_feature.py
    

# remove stashed item
git stash pop stash@{1}

Reflog

  • Shows the state of the repository and enables you to undo mistakes e.g., you deleted a file

# show changes
git reflog

    34o4on43mf0sd  HEAD@{0}: checkout: moving from main to feature (HEAD@{0})
    has34o4omf0sd  HEAD@{1}: commit: added feature X (2 hours ago)
    64osafdamf0sd  HEAD@{2}: commit: initial commit (yesterday)
    78o4sdsdh330s  ORIG_HEAD@{0}: commit: initial commit (yesterday)
    

# undo a change
git reset --hard <commitHash>

Useful Commands

General

# get help
git --help
man git

# download/clone repo
git clone <repositoryLink>

# view changes made since last commit or initial download 
git status

# queue files for commit
git add <fileName>
git add *

# commit files
git commit -m "description of change"

# push files to branch
git push
git push -u origin <branchName>
git push --set-upstream origin <branchName> 

# view git log
git log
git log -p <commitHash>

# checkout a commit
git checkout <commitHash>

Branches

# view all local branches
git branch

# view all remote branches
git branch -r

# view all local and remote branches
git branch -a

# create new branch and switch to it
git checkout -b <newBranchName>

# switch branch
git branch <branchName>

# rename branch
git branch -m <oldBranchName> <newBranchName>

Modify Terminal to show current git branch

  • Update your shell's configuration file e.g., ~/.zshrc or ~/.bashrc

  • Save and then reload your shell for the changes to take effect source ~/.zshrc

parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
# color #s found here: https://misc.flogisoft.com/bash/tip_colors_and_formatting
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
👨‍💻
website
Git Download