Author : MD TAREQ HASSAN

Approach

Open source forked project

Private project (working alone or multiple members)

Clone

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

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

Create local branch and push to remote
git checkout master
git pull
git checkout -b x-feature

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

# then push
git push --set-upstream origin x-feature
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

Submodules from remote

1.checkout main repo

git checkout path-to-main-repo/foo.git

2.Init update submodule

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