Author : MD TAREQ HASSAN
Approach
Open source forked project
- Create ‘
x-feature
’ branch from master branch - Create ‘
x-feature-dev
’ branch from ‘x-feature
’ branch - Work in ‘
x-feature-dev
’ branch and when done sqauash merge it to ‘x-feature
’ branch - Sync local master to ‘upstream/master’ (also pull from remote origin if multiple members are working on same forked project)
- Rebase ‘
x-feature
’ branch to local master branch (mentioned that local master has synced to ‘upstream/master’) - Push ‘
x-feature
’ branch remote origin - Create pull request (PR) of ‘
x-feature
’ branch to ‘upstream/master’
Private project (working alone or multiple members)
- Create ‘
x-feature
’ branch from master branch - Create ‘
x-feature-dev
’ branch from ‘x-feature
’ branch - Work in ‘
x-feature-dev
’ branch and when done sqauash merge it to ‘x-feature
’ branch - Update local master by pulling from remote origin master (since you or other member pushed from other cloned local master)
- Rebase ‘
x-feature
’ branch to local master branch - Merge ‘
x-feature
’ branch to local master branch - Push local master to remote origin master
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
- Visual Studio project: https://github.com/github/gitignore/blob/main/VisualStudio.gitignore (Raw)
- XCode Swift:
- https://github.com/github/gitignore
Branch
Create local branch and push to remote
- Create local branch:
git checkout -b x-feature
- Push to remote:
git push --set-upstream origin x-feature
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
- Question: https://stackoverflow.com/questions/30590083/how-do-i-rename-both-a-git-local-and-remote-branch-name
- Worked answer: https://stackoverflow.com/a/30590238/4802664
git branch -m oldBranck newBranch
git push origin :oldBranck
git push origin newBranch
git push origin -u newBranch
Deleting branch
- https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely
- Deleting local branch
- normal delete:
git branch -d branch_name
- forced delete:
git branch -D branch_name
- normal delete:
- Deleting remote branch
git push origin --delete branch_name
- Same as above:
git push origin :branch_name
Commit
Normal commit: git commit -m "Commit message here"
Modify commit message
- Not pushed yet:
git commit --amend -m "New commit message before push"
- Already pushed:
git commit --amend -m "New commit message after push"
git push origin master --force
- Links
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
- Requirement: the commit is not pushed to remote yet
- n = 1 or more
git reset --hard HEAD~n #(n = number of commits to revert back)
Revert untracted changes
https://stackoverflow.com/a/59154401/4802664
Squash
- Always squash before rebasing
- Always sync local master with
upstream/master
(and/or pull from remote origin) before rebasing to local master - combining-multiple-commits-into-one: https://stackoverflow.com/a/5721879/4802664
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
- Sync local master to ‘upstream/master’ for forked project
- Pull from remore origin
- Rebase to local 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