Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Git - Tips and Notes

Git - Tips and Notes

Git Cheat Sheet

# Checkout a new branch
git checkout -b new-branch

# push new branch to remote
git push -u origin new-branch
git push --set-upstream origin new-branch

# fetch the latest changes from remote
git fetch origin

# Merge the latest main into your branch
git merge origin/main

How to clean Git commit history

# Create Orphan Branch
git checkout --orphan temp_branch

# Add Files to Branch and commit
git add -A
git commit -am "the first commit"

# Delete master Branch
git branch -D main

# Rename temp/Current Branch to master
git branch -m main

# Push Changes to repo as master
git push -f origin main

Original post: How to Delete Commit History in Github

How to get latest content to branch which already checked in

Sync feature branch with latest main

Example scenario

$ git branch
* cs/test
  fix/llm-vars
  fix/order-inventory-path
  main

# 1. Update main branch
$ git checkout main
$ git pull

# 2. Switch to your working branch
$ git checkout cs/test

## 3. Option A: Merge (safe, keeps history)
## Keeps full history, Creates a merge commit, Safe for shared branches
$ git merge main


## 3. Option B: Rebase (clean history)
## Linear history, Replays commits on top of main
$ git rebase main

How to fetch latest content from main repo to forked repo

Follow these steps to update your forked repository with the latest changes from the original (eg. main or master) repository.

1. Add the original repo as upstream (only once)

git remote add upstream <original repo URL>

Skip this step if you’ve already added upstream. You can verify with:

git remote -v

2. Fetch the latest changes from the original repo

git fetch upstream

3. Merge or rebase changes into your local branch

Make sure you’re on your working branch (e.g. master or main):

git checkout master
git merge upstream/master

Optional: Use rebase for cleaner history:

git checkout main
git rebase upstream/main

4. Push the updated branch to your fork

git push origin main

Now your forked repository is up to date with the latest changes from the main/original repository.

Create a PR to main

  1. Raise a PR
gh pr create \
  --base main \
  --head cf-pages \
  --title "Deploy blog updates" \
  --body "Added new blog post and updates to site"
  1. Approve the PR (from CLI)
gh pr review <PR_NUMBER_OR_URL> --approve
  1. Merge the PR
gh pr merge <PR_NUMBER_OR_URL> --merge

Reset to old commit

# Ensure local is wiped clean
git reset --hard e7619b1

# Remove untracked files (Optional)
git clean -fd

# Make the server match your local
git push origin [branch-name] --force
Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

Ansible Labs

Ansible Labs

Ansible Automation Platform Enable RHEL and AAP Subscriptions Red Hat Container Registry Authentication AAP Download Options Extract the AAP …

Ansible and Terraform

Ansible and Terraform

cloud.terraform collection github.com/ansible-collections/cloud.terraform

Ansible for Cisco ACI

Ansible for Cisco ACI

Ansible for Cisco ACI Automation References ACI Ansible community Ansible Modules for Cisco ACI Ansible Cisco ACI Guide Completed Configurations in …

CheatSheets for DevOps Tools

CheatSheets for DevOps Tools

Ansible Cheatsheet kubectl & oc Cheatsheet Docker & Podman Cheatsheet AWS CLI Cheatsheet Vim Editor Cheatsheet Logical Volume Manager (LVM) …

AWS CLI Cheatsheet

AWS CLI Cheatsheet

The AWS Command Line Interface (CLI) is a powerful tool that provides a streamlined way to interact with Amazon Web Services (AWS) directly from your …

Ansible Automation Platform - CLI Cheat Sheet

Ansible Automation Platform - CLI Cheat Sheet

AAP CLI Commands $ aap-gateway-manage update_password --username='<username>' --password='<password>' # For containerized …