Skip to content

Instantly share code, notes, and snippets.

@dmlogv
Last active July 31, 2020 14:11
Show Gist options
  • Save dmlogv/0903e4e78b1d220e1c1422ed2f1393a2 to your computer and use it in GitHub Desktop.
Save dmlogv/0903e4e78b1d220e1c1422ed2f1393a2 to your computer and use it in GitHub Desktop.
My Git snippets

Configuration

  • Commons:
    • git config --global http.sslVerify false
  • Don't forget to use proxy!
    • git config --global http.proxy dm-logv:pass123@mwg:3128
    • git config --global https.proxy dm-logv:pass123@mwg:3128
  • Don't forget to set name and email:
    • git config user.name "Dmitry Logvinenko"
    • git config user.email "5diezfun@gmail.com"

How to make a clean pull request

Look for a project's contribution instructions. If there are any, follow them.

  1. Create a personal fork of the project on Github.
  2. Clone the fork on your local machine. Your remote repo on Github is called origin.
    • git clone https://github.com/dm-logv/matrix-python-sdk.git
    • View at your remotes (there is origin only now): git remote -v
  3. Add the original repository as a remote called upstream:
    • git remote add upstream https://github.com/matrix-org/matrix-python-sdk.git
    • View at your remotes again :)
  4. If you created your fork a while ago be sure to pull upstream changes into your local repository.
  5. Create a new branch to work on! Branch from develop if it exists, else from master.
    • git checkout -b proxy-support
  6. Implement/fix your feature, comment your code.
  7. Follow the code style of the project, including indentation.
  8. If the project has tests run them!
  9. Write or adapt tests as needed.
  10. Add or change the documentation as needed.
  11. Squash your commits into a single commit with git's interactive rebase. Create a new branch if necessary.
  12. Push your branch to your fork on Github, the remote origin.
  13. From your fork open a pull request in the correct branch. Target the project's develop branch if there is one, else go for master!
  14. ...
  15. Once the pull request is approved and merged you can pull the changes from upstream to your local repo and delete your extra branch(es).

How to work

Merge with --no-ff only (?)

$ git merge python-impl-fixes --no-ff

Tips & tricks

Branch tree

$ git log --oneline --all --graph

Fix last commit (add missed file, edit message)

Link: https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

Remove a branch

$ git push --delete origin python-impl-fixes
$ git branch -d python-impl-fixes

Rename a branch

$ git branch -m old-name new-pretty-name
$ git push origin -u new-pretty-name   
$ git push origin --delete old-name

Remove untracked files

Link: https://stackoverflow.com/a/64966/2106208

Step 1 is to show what will be deleted by using the -n option:

git clean -n

Clean Step - beware: this will delete files:

git clean -f

Rollback commits / undo merge

Link: https://stackoverflow.com/questions/2389361/undo-a-git-merge-that-hasnt-been-pushed-yet

  • Reset to remote branch state: git reset --hard origin/master
  • Reset to SHA: git reset --hard commit_sha
  • Reset last commit: git reset --hard HEAD~1 or git reset --hard HEAD^

Rewrite author & email

$ git filter-branch --commit-filter '
    if [ "$GIT_AUTHOR_EMAIL" = "dm-logv@old.email" ];
    then
        GIT_AUTHOR_NAME="Dmitry Logvinenko";
        GIT_AUTHOR_EMAIL="5diezfun@gmail.com";
        git commit-tree "$@";
    else
        git commit-tree "$@";
    fi' HEAD

Increment date

# GIT_COMMITTER_DATE 
# date -d '+9 hour' -R
$ git filter-branch --commit-filter '
    GIT_AUTHOR_DATE
    GIT_COMMITTER_DATE
    git commit-tree "$@";
    ' HEAD

Merge an unrelated branch

If you want to copy commits to another repository (not fork) you can make it this way:

  • Add a target repo to local one:
    secret-project/$ git remote add upstream https://my-pretty-git-server/my-awesome-project
  • Push a branch notes you want to merge into the repo:
    secret-project/$ git checkout notes
    secret-project/$ git push upstream
  • Let`s switch to the my-awesome-project and fetch a new branch:
    secret-project/$ cd ../my-awesome-project
    my-awesome-project/$ git fetch --all
  • Merge it!
    my-awesome-project/$ git merge --no-ff --allow-unrelated-histories notes
  • Fix merge conflicts and complete a merge:
    my-awesome-project/$ emacs conficted_file.cpp
    ...
    my-awesome-project/$ git merge --continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment