Skip to content

Instantly share code, notes, and snippets.

@dfop02
Last active July 12, 2024 13:59
Show Gist options
  • Save dfop02/b5c471107e98b0959609c94c6a243143 to your computer and use it in GitHub Desktop.
Save dfop02/b5c471107e98b0959609c94c6a243143 to your computer and use it in GitHub Desktop.
All my git codes and tips
######################### Git tips #########################
===== Git Checkout =====
git checkout . => Delete all changes on current branch
git checkout branch_name => Switch Branch
git checkout -b branch_name => If Branch not exist, create and Switch
git checkout origin/master Gemfile => Reset specific file from master
===== Git Branch =====
git branch -a => Show all branch on local and remote
git branch -D branch_name => Delete that Branch
git branch -m <oldname> <newname> => Rename old branch for new branch
git branch -m <newname> => Rename current branch
git branch --contains branch_name => Check which branchs contains the branch_name
git branch --merged branch_name => List all branches merged on branch_name
git branch --not-merged branch_name => List all branches not merged on branch_name
git push origin :branch_name => Delete Branch remotely
===== Git Add =====
git add . => Add all changes on current branch to commit
git add folder/ => Add all changes inside folder/ to commit
git add app/views/mobile/checkout/payment.html.haml => Add a specific file to commit
===== Git Commit =====
git commit -m "18534 - Adding check validation" => Commit changes with a message
git commit --amend -m "edit msg" => Edit last commit (if already pushed use -f at end)
git commit --no-verify -m "msg" => Ignores hooks before and after commit
===== Git Reset =====
git reset --merge ORIG_HEAD => Undo merge that was not pushed to origin yet
git reset --hard HEAD~1 => Back one commit
git reset --soft HEAD~1 => Revert one commit, keeping commit data
git reset --hard origin/<branch> => Reset local branch to remote branch
===== Git Push/Pull =====
git push -u origin branch_name => Push and set upstream (only first push)
git push origin feature/branch-example => Push local files to Github
git pull origin feature/branch-example => Get new files of this Branch in remote
git push => Push local files of current branch to remote (if branch has set upstream)
git pull => Get new files of this Branch in remote (if branch has set upstream)
===== Git Merge =====
git merge branch/A => Merge branch/A into current branch (branch/B)
git merge --no-ff branch/approved => Merge without "fast-forward" (commits from branch/approved stay separated)
git merge branch/A -X ours => Merge but if has any conflict, prefer current branch
git merge branch/A -X theirs => Merge but if has any conflict, prefer branch/A
===== Git Tag =====
git tag 1.55.1 => Create tag locally
git tag --delete 1.55.1 => Delete tag locally
git push origin :1.55.1 => Delete tag remotely
===== Git Stash =====
If doesnt exist [<stash>], so git uses stash@{0}
git stash [<stash>] => Save all different files temporary and remove from branch
git stash pop [<stash>] => Remove a single stashed state from the stash list and apply
git stash save -u "feature name" => Save stash and give it a name
git --no-pager stash list => List all stash
git stash apply [<stash>] => Apply stash by index from stash list without remove from list
git stash show -p [<stash>] => Show the contents of any stash in patch form
git stash drop [<stash>] => Remove a single stash entry from the list of stash entries
git stash clear => Delete all stashs
===== Git Show =====
git show -s --format=%ci commit => Get time of commit were pushed
git --no-pager show branch_name:path/to/file => Print content of file
===== Git Others =====
pull request (PR) direto pelo Github => Pull Request
git fetch --all => Update all remotes locally but not upgrade the commits
git diff mybranch..master -- myfile.cs => Git show you the diff on a specific file between 2 branches
git cherry-pick commit-SHA1 => Add specific commit at current branch
git rebase branch => Reload specific branch based on origin branch (if was changed after created)
git whatchanged --since='2 weeks ago' => What changed since 2 weeks ago
git checkout master && git branch --no-merged => List all branch is WIP
===== Git Config =====
There is 3 levels:
1. System level (applied to every user on the system and all their repositories)
2. Global level (values specific personally to you, the user)
3. Repository level (specific to that single repository)
git --no-pager config --global -l => List all global config
git config --global --add module.name option => --add prevent to overwrite the option, creating a second option
git config --global user.name "Full Name" => Set name of user
git config --global user.email "email@address.com" => Set email of user
git config --global core.mergeoptions --no-edit => Always run these options when execute git merge (ex.: --no-edit)
git config --global merge.ff false => Prevents from executing a "fast-forward" when merge
git config --global pull.ff only => Always execute "fast-forward" when pull
===== Git Alias =====
=> Push new branchs up-stream automatically by using "git pushu branch" (nowdays has a built-in git)
git config alias.pushu = ![[ $(git config "branch.$(git symbolic-ref --short HEAD).merge") = '' ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push
========== Complex Git Codes ==========
=> Show all current tickets on specific release/branch (if all commits starts with ticket number)
git --no-pager log --no-merges --pretty=format:'%s' release/branch --not origin/master | sort | sed 's/[^0-9.]*\([0-9.]*\).*/\\n\1/; /\./d' | uniq
=> Delete all branches already merged into master (local and remote)
git branch --merged master | grep -v -e 'master' -e '\*' | xargs -n 1 git branch -d && git remote prune origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment