Author : MD TAREQ HASSAN

Setup git repository

Local first

Scaffold a simple app locally (i.e., astro app):

npm create astro@latest astro-minimal -- --template minimal --install --no-git
cd astro-minimal

Initialize git repository and first commit

# Initialize git
git init -b main # '-b main' -> default branch 'main'

# First commit
git add .
git commit -m "First commit"

Create repository i.e., in github.com (using GitHub.cli) and push

winget install -e --id GitHub.cli
gh --version
gh auth login
gh repo create --source=. --private
git push --set-upstream origin main # '-u' is short for '--set-upstream'

Remote first

Create an empty private repository (i.e., in Github.com)

Scaffold a simple app locally (i.e., astro app):

npm create astro@latest astro-basics -- --template basics --install --no-git
cd astro-basics

Initialize git repository and first commit

# Initialize git
git init -b main # '-b main' -> default branch 'main'

# First commit
git add .
git commit -m "First commit"

Push to remote

git remote add origin git@github.com:[username]/astro-basics.git
git push -u origin main

#Config Global: user nanme and email for all repositories

# Check: git config user.name
git config --global user.name "Xxx Yyy"

# Check: git config user.email
git config --global user.email "Xxx.Yyy@example.com"

Local: user nanme and email for current repository only

# Check: git config user.name
git config user.name "Xxx Yyy"

# Check: git config user.email
git config user.email "Xxx.Yyy@example.com"

Clone

git clone git@github.com:hovermind/coding-practice.git

Clone a repository with submodules:

Remote

Add remote

git remote add origin <github url here>

git remote add origin git@github.com:xxxx/yyy-zzz.git

Change remote (set new remore url)

git remote set-url origin https://xxx.git

Now push

git push -u origin master

Git Ignore

Branch

Clone repository with specified branch

git clone --branch [branch-name] git@ssh.dev.azure.com:v3/olympusnova/OlySense/infrastructure-platform

Pull specific remote branch to local

git checkout -b [branch-name] origin/[branch-name]

# fetch all remote branches
git fetch origin

Create local branch and push to remote

git checkout master
git pull
git checkout -b [branch-name]

# Make changes and commit
# ... ... ...

# then push
git push origin --set-upstream [branch-name]
Renaming branch
git branch -m oldBranck newBranch
git push origin :oldBranck
git push origin newBranch
git push origin -u newBranch
Deleting branch

Commit

Normal commit: git commit -m "Commit message here"

Modify commit message

Add Upstream for Forked Project

Check if upstream is already added or not

# See origin and upstream
git remote -v

Add upstream

git remote add upstream https://xxx.git

# check again
git remote -v

Syncing with Upstream

# Make sure local master is synced with remote origin master
git checkout master
git pull

# Get updates from upstream
git fetch upstream

# Rebase local master to upstream/master
git rebase upstream/master # local master is now in sync with upstream

# Push synced state to remote origin
git push

Now rebase your local feature branch to local master

git checkout x-feature
git rebase -i master

Create Pull Request for Forked Project

Create ‘x-feature’ feature branch

git checkout master
git pull
git checkout -b x-feature

Now, we are going to create ‘x-feature-dev’ branch that will be squashed to ‘x-feature’ branch. Before creating ‘x-feature-dev’ branch, we can add blank Visaul Studio solution to ‘x-feature’ branch to keep initial history

# Add solution
# 
# git add .
# git commit -m "Add xyz solution"
#

# Now create 'x-feature-dev'
git checkout -b x-feature-dev

Now make changes to your feature branch.

Syncing, rebasing and pushing to remore origin

#
# Sync local master to upstream
#
git checkout master
git pull

git fetch upstream
git rebase upstream/master # local master is now in sync with upstream

#
# Rebase branches and squash
#
git checkout x-feature
git rebase -i master

git checkout x-feature-dev
git rebase -i x-feature

git checkout x-feature
git merge --squash x-feature-dev

# commit squashed changes as a single change
git commit -m "comment for the squash changes i.e. Add xxx feature"


#
# Push to remote origin
#
git push --set-upstream origin x-feature

# Now create PR of 'x-feature' from Github

Github dock - Creating a pull request from a fork

Push

Pushing to a branch
git push -u origin master
Overriding remote (danger, will override code in remote)
git push origin master -f

Revert and Reset

Revert uncommitted changes
git add .
git reset --hard HEAD
Revert ‘n’ number of commits
git reset --hard HEAD~n #(n = number of commits to revert back)
Revert untracted changes

https://stackoverflow.com/a/59154401/4802664

Squash

git checkout x-feature
git merge --squash x-feature-dev

After squash, if you want to delete branch and git does not allow to delete then use force delete

git branch -D x-feature-dev

Now sync your local master with upstream/master and then rebase ‘x-feature’ branch with lcoal master.

Rebase

Rebase ‘x-feature-dev’ to ‘x-feature’

git checkout x-feature-dev
git rebase -i x-feature

Rebase ‘x-feature’ to local master

git checkout x-feature
git rebase -i master

Merge

Before merging a branch to lcoal master

Merge ‘x-feature-dev’ branch to ‘x-feature’

git checkout x-feature
git merge x-feature-dev

Merge ‘x-feature’ branch to master

git checkout master
git merge x-feature

Submodule

Checkout repo with submodule

git clone --recurse-submodules [URL]

Get submodules after cloning (if you did not use –recurse-submodules during cloning)

git submodule update --init --recursive
git submodule update --init --recursive --force
git submodule update --recursive --remote --merge --force
Pull submodule
git submodule foreach git pull origin master
Adding github repo as submodule
git submodule add https://xxx/yyy.git