Skip to content

Instantly share code, notes, and snippets.

@ShinobiWPS
Last active January 19, 2024 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShinobiWPS/07ca62bb34ead0b40e33cd21edffa7ab to your computer and use it in GitHub Desktop.
Save ShinobiWPS/07ca62bb34ead0b40e33cd21edffa7ab to your computer and use it in GitHub Desktop.
Git usefull commands :).md

Forcefully apply local changes to the origin

If the commit has already been pushed and the changes need to be applied to the remote source (aka origin): the origin implies the remote branch of the current local one you are on

git push origin --force-with-lease #this will OVERWRITE the GIT history only if no one else has pushed something before you

Delete a local branch

git branch -d the_local_branch

Delete a remote branch

git push origin --delete the_remote_branch

Created a local commit not yet pushed to the wrong branch => move N of such commits to a new branch:

git branch WRONG_BRANCH git reset --hard||--soft HEAD~NUMBER_OF_LOCAL_COMMITS MOVE TO THE NEW BRANCH (assuming it already exists)

git checkout NEW_BRANCH

Created a local commit not yet pushed to the wrong branch => undo this last commit to restore the working-tree (changes that git shows you when you modify a file and see with git status):

git reset --soft HEAD^

Modify something of the last commit made (to amend a commit):

  1. Add files forgotten before:

    git add FILE_TO_ADD_TO_STAGE git commit --amend --no-edit

  2. Change the author of a commit:

    git commit --amend --author "Name and Surname "

  3. Change the commit message:

    git commit --amend -m "an updated commit message"

How to combine more commits into one or some (aka Squashing):

git checkout mybranch git rebase -i origin/REFERENCE_BRANCH # Usually the reference is the branch from which you branched off Each commit has a prefix which is pick

-For how many commits you want at the end of the squash process, the same number of pick prefixed commits you must have.

For each commit replace pick with s (or squash) to keep also the commit message OR f (or fixup) instead to discard it and keep only the commit changes.

-save and exit

Stage (git add) and solve each conflict you could have and move forward after each step with git rebase --continue after staging the changes.

Merge a branch without having the famous commit "merge of branch X into Y" in the git commit history and all commits in ONE unique

git merge --squash

Show the commits of a user for the day

git log --oneline --author EMAIL_ADDRESS --after="yesterday"

Get all recent commits from the reference branch

git pull --rebase origin REFERENCE_BRANCH # where --rebase[=(false|true|merges|preserve|interactive)]

Remove a tracked/tracked file if you put it in the .gitignore

git rm --cached FILEorFOLDER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment