Git
Specific topics
Basic commands
- Cancel last local commit
git reset HEAD~1
-Remove a file from the index without deleting it
git reset HEAD <filename> # does not delete the file but removes it from the index
- Create new branch
git branch <branchName> # locally
# upstream branch
git branch --set-upstream-to=origin/<branchName>
git push -u origin <branchName>
- Merge some commits together
git rebase -i HEAD~X
# X = number of commits to merge
# 1. make your modification
# 2. if commits were already pushed: git push --force
# 3. else: git push
- Unstagged files and change active branch
git stash push -m "message"
git stash list
git checkout <another branch>
git stash pop
- Delete file from git history
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch <path-to-file>" HEAD
git push -all --force
- Find file in git history
git log --all --full-history -- <path-to-file>
- Apply commits on another branch
git cherry-pick <commit-hash>
- Add & push tags
git tag <tag-name>
git push --tags
- Remove unwanted files that are not tracked by git.
git clean -fd -x
# -x: remove ignored files too
# -X: remove only ignored files
# -n: for dry run
- Use GIT with a GUI
gitk --all
- Basci script using gum to clean-up your merged branch
#!/bin/sh
# check if current path is a git repo
git rev-parse --is-inside-work-tree 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "There is no git repository in the current path."
exit 1
fi
# list of merged branches
choose_branches() {
gum choose \
--cursor-prefix=" [ ] " \
--selected-prefix=" [x] " \
--unselected-prefix=" [ ] " \
--no-limit \
$(git branch --format="%(refname:short)" --merged)
}
echo "Choose branches to delete"
branches=$(choose_branches)
if [ -z "${branches}" ]; then
echo "No branch was selected."
exit 1
fi
echo $branches | tr " " "\n" |while read branch
do
git branch -d "$branch"
done