Add untracked changes to git
git add <full path of changed code>
Add all files in current folder to git
git add .
Add all files in a package to git
git add --all
Add specific folder and all files in it to git. ex folder src/
git add src
Commit changes to git
git commit -m "<provide commit message here>"
If you want to add further changes to your commit, instead of adding new commits you can ammend previous one
git commit --amend
When you are ready to push changes
git push
Or to push to a specific branch my_branch
and remote repository my_origin
git push my_origin my_branch
Get latest changes and rewind yours on top
git fetch
git pull --rebase
Apply a commit from one branch on another
git cherry-pick <commit-sha>
Stash away uncommited work
git add --all
git stash --save
List all branches
git branch
Change branch
git checkout <branch_name>
Change into a new branch
git checkout -b <branch_name>
Merge code from branch source
into branch destination
git checkout destination
git merge source
- Rename your local branch. If you are on the branch you want to rename:
git branch -m new-name
- If you are on a different branch:
git branch -m old-name new-name
- Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
- Reset the upstream branch for the new-name local branch.Switch to the branch and then:
git push origin -u new-name
Delete local merged branches
git branch --merged | egrep -v "(^\*|master|develop)" | xargs git branch -d
Delete local unmerged branches
git branch --no-merged | egrep -v "(^\*|master|develop)" | xargs git branch -d
Find remote branches which have been already merged to delete
git branch -a --merged remotes/origin/develop | egrep -v "(^\*|master|develop|HEAD)" | grep "remotes/origin/" | cut -d "/" -f 3,4
Delete the remote branches already merged
git branch -a --merged remotes/origin/develop | egrep -v "(^\*|master|develop|HEAD)" | grep "remotes/origin/" | cut -d "/" -f 3,4 | xargs -n 1 git push --delete origin
Delete ummerged but abandoned branches
git branch -a --no-merged remotes/origin/develop | egrep -v "(^\*|master|develop|HEAD|unmerged-keep-this-branch)" | grep "remotes/origin/" | cut -d "/" -f 3,4 | xargs -n 1 git push --delete origin
Prune repo to get upstream in sync
git remote prune origin
Cherry pick a commit from one git repo (folder) to another
git --git-dir=<git_repo_path>/.git \
format-patch -k -1 --stdout <commit-SHA> | \
git am -3 -k