Skip to content

Instantly share code, notes, and snippets.

@mamedshahmaliyev
Last active December 2, 2020 07:09
Show Gist options
  • Save mamedshahmaliyev/dca49d3c4cb1e2d1e99094ff2c600825 to your computer and use it in GitHub Desktop.
Save mamedshahmaliyev/dca49d3c4cb1e2d1e99094ff2c600825 to your computer and use it in GitHub Desktop.
Git tips

Transfer latest commits into new branch

Source: https://stackoverflow.com/questions/1628563/move-the-most-recent-commits-to-a-new-branch-with-git

Note: Any changes not committed will be lost.

git branch newbranch       # Create a new branch, saving the desired commits
git reset --hard a1b2c3d4  # or git reset --hard HEAD~3 to move master back by 3 commits
git checkout newbranch     # Go to the new branch that still has the desired commits

Transfer latest commits into existing branch

git checkout existingbranch
git merge master
git checkout master
git reset --hard a1b2c3d4  # or git reset --hard HEAD~3 to move master back by 3 commits
git checkout existingbranch

Merge commits

source: https://www.internalpointers.com/post/squash-commits-into-one-git

Choose the hash of the commit until which the merge will be done. The selected commit itself will not be included into the merge.

git rebase -i 72eae16d4f8af8db6
  1. in the first popped up editor put pick to the commit that is used to merge into
  2. put squash to the commits that will be merged into the previous first picked commit, save and close
  3. in the second popped up editor delete all and put your commit message
  4. use git push origin your_branch (git push origin +your_branch, + is needed to force update if you need to merge into remote branch as well)

Delete branch

Source: https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely

git branch -d branch_name # delete local branch
git branch -D branch_name # delete local branch force
git push origin --delete branch_name # delete remote branch

Migrate repository

git clone old_url . # clone from the old repo
git checkout master # checkout to master

# download all branches
for remote in `git branch -r`; do git branch --track $remote; done 
git fetch --all
git pull --all

# add location for the new repo
git remote add origin2 new_url

# push repo to the new location
git push --all origin2

Delete commit history and leave single commit

source: https://tecadmin.net/delete-commit-history-in-github/

git checkout --orphan temp_branch
git add -A
git commit -am "the first commit"
git branch -D master
git branch -m master
git push origin +master

Reset local repository to the remote

git fetch origin
git reset --hard origin/master

Turn off CRLF/LF replacements

source: https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf

git config --global core.autocrlf false    # per-user solution
git config --local core.autocrlf false     # per-project solution

Cancel local commits

source: https://stackoverflow.com/questions/4850717/how-to-cancel-a-local-git-commit

git reset --soft commit_id_or_branch    # keep the local changes
git restore --staged  ..                # unstage your changes after soft reset
git reset --hard commit_id_or_branch    # delete the local changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment