Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Last active February 2, 2022 09:38
Show Gist options
  • Save jesugmz/d7f6a818d4fe06d4b9ffaae1524cde39 to your computer and use it in GitHub Desktop.
Save jesugmz/d7f6a818d4fe06d4b9ffaae1524cde39 to your computer and use it in GitHub Desktop.
git spreadsheet

git spreadsheet

Apply commit from another branch

git cherry-pick <COMMIT_HASH_FROM_ANY_BRANCH>

Apply changes of a file from another branch

git checkout <SOURCE_BRANCH> -- <PATH_TO_FILE>

This can be used, for example, to recover deleted files from another branch.

Apply changes from exactly one file from git stash

git checkout stash@{<STASH_NUMBER>} -- <FILE_NAME>

Commit specific lines

git add -p <PATH_TO_FILE>

After adding, commit the changes as usual.

Modify changes from last commit

# do changes in the file if applies
git [add|rm|reset] <PATH_TO_FILE>
git commit --ammend

Change last commit message in local and remote

Same mechanism as before:

git commit --amend

And change the message in the top of the editor.

If exist in remote, after change it, push it with --force to overwrite the last commit message in remote.

More info in GitHub.

Modify previous commits message

Example: having 3 commits, we want to modify all the previous commit messages (valid for any of them individually as well):

git rebase -i HEAD~3 # or use the last commit reference

the editor will be prompted and should remain as follow:

pick 2f05aba ...   # will be preserved
reword daed25c ... # it's message will be modified; or just `r` instead of `reword`
reword e2b2a84 ... # it's message will be modified

From this on, rebase will prompt one editor per reword commit in order to modify it's message, just modify, save and continue.

Once all the changes are made, force push is needed to overwrite remote commit log:

git push --force

Remove previous commits

Example: having 3 commits, we want to skip the 2th:

git rebase -i HEAD~3 # or use the last commit reference

the editor will be prompted and should remain as follow:

pick 2f05aba ... # will be preserved
drop daed25c ... # will be dropped; or just `d` instead of `drop`
pick e2b2a84 ... # will be preserved

Once all the changes are made, force push is needed to overwrite remote commit log:

git push --force

Clear the entire stash

git stash clear

Ignore permission changes

This change affect to the global git configuration, every git project that does not define core.filemode directive will be affected:

git config --global core.filemode false

To apply to a specific project, under the git project root folder:

git config core.filemode false
```## Reset remote git commit

```sh
git reset HEAD~<NUMBER_COMMITS_BACK>

or

git reset --hard <COMMIT_HASH>

After this, push it to remote overwriting the last message:

git push origin HEAD --force

Set commit author

This change affect to the global git configuration, every git project that does not define core.filemode directive will be affected:

git config  --global user.name "User name"
git config  --global user.email "my@email.tld"

To apply to a specific project, under the git project root folder:

git config user.name "User name"
git config user.email "my@email.tld"

Change commit author of last commit

git commit --amend --author="Author Name <email@address.com>"

https://stackoverflow.com/a/3042512/4411354

Ignore files locally

Add entries into .git/info/exclude.

In case of having unstaged changes, you need to explicitly ignore them: git update-index --assume-unchanged <PATH_TO_FILE>.

Rename branch

If you are in the branch to rename:

git branch -m <NEW_BRANCH_NAME>

or if you are in another branch:

git branch -m <OLD_BRANCH_NAME> <NEW_BRANCH_NAME>

To remove the old branch from remote and push the new one:

git push origin :<OLD_BRANCH_NAME> <NEW_BRANCH_NAME>

Reset the upstream branch:

git push origin -u <NEW_BRANCH_NAME>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment