Skip to content
Baltasar Brito edited this page Jan 23, 2020 · 3 revisions

Remove file from git but not from local

  • git rm -r --cached FolderName
  • git commit -m "Removed folder from repository"
  • git push origin master

Clean commit history

  • Check status with: git status.
  • If any changes are unstaged, they have to either be staged with git add or unstaged with git stash.
  • Perform a git rebase, using fixup on the commits you want to "merge". The difference between squash and fixup is that during the rebase, the squash operation will prompt you to combine the messages of the original and the squash commit, whereas the fixup operation will keep the original message and discard the message from the fixup commit. For instance, if the git history looks like the following:
d1bff072 2019-11-11 | fix: Code clean up (HEAD -> features/peg/peg-784, origin/features/peg/peg-784) [Baltasar Brito]
80918a05 2019-11-11 | fix: Code clean up [Baltasar Brito]
0793184c 2019-11-08 | feat: Change tags logic [Baltasar Brito]

If the end goal is to keep only the Change tags logic commit but also keep the changes made by the other two, then one could do: git rebase -i HEAD~3. An interactive rebase. Then, in each of the two to be "joined", fixup is to be chosen.

  • Send the changes to the remote branch with: git push --force-with-lease.

Git

  • List branches: git branch -a.

  • Back to original head of the branch, delete all changes until now: git reset --hard HEAD.
    If the flag --hard is not used then instead of being deleted, the changes made so far become unstaged. This is useful in case a restructuring of commits is necessary for example. To reset to a specific commit in the history of the branch just write the hash of the commit after HEAD or the number of commits to reset. The following command will reset the last two commits and delete them from git history: git reset --hard HEAD ~2.

Rebase

  • Change base or parent of branch: git rebase <branch to rebase to>.
    • -i: interactive, and is used to know which changes will be made beforehand. It can be used like: git rebase -i master for example.
  • Remove commit: drop.
  • Use commit: pick.

Push

  • Send local changes to remote: git push.
  • Override remote with local changes: git push --force.
  • Override remote with local changes if state of remote has not been altered since last check: git push --force-with-lease.

Cherry pick

  • Apply the changes of a commit to another branch. From the branch to be altered (git co to that branch) use git cherry-pick <commit-hash> and then push to remote.

Delete Branch

  • Delete a remote branch. git push <remote> --delete <branch-name>, git push origin --delete feature/peg-755-754 is an example.