2021年2月3日 星期三

How do I force “git pull” to overwrite local files?

 git reset --hard origin/branch_to_overwrite


from : https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files

Push local branch to remote with another branch_name

git push origin local_branch:remote_branch


EX: git push origin p-other/LUWA-15566-Feature1:p-api/Feature2

2020年11月9日 星期一

How to find the nearest parent of a Git branch?

 

git parent

You can just run the command

git parent

to find the parent of the branch, if you add the @Joe Chrysler's answer as a git alias. It will simplify the usage.

Open gitconfig file located at "~/.gitconfig" by using any text editor. ( For linux). And for Windows the ".gitconfig" path is generally located at c:\users\your-user\.gitconfig

vim  ~/.gitconfig

Add the following alias command in the file: 

[alias]
            parent = "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"

Save and exit the editor.

Run the command git parent

That's it!


from : https://stackoverflow.com/questions/3161204/how-to-find-the-nearest-parent-of-a-git-branch


FINALLY VERSION:

git show-branch -a \

| grep '\*' \

| grep -v `git rev-parse --abbrev-ref HEAD` \

| head -n1 \

| sed 's/.*\[\(.*\)\].*/\1/' \

| sed 's/[\^~].*//'


parent = "!git show-branch -a | grep '\\*' | grep -v `git rev-parse --abbrev-ref HEAD` | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//'"


from : https://archive.hankchizljaw.com/notes/24/

2020年9月28日 星期一

Git how to rollback a rebase

 You can use the reflog to find the first action before the rebase started and then reset --hard back to it. e.g.

$ git reflog

b710729 HEAD@{0}: rebase: some commit
5ad7c1c HEAD@{1}: rebase: another commit
deafcbf HEAD@{2}: checkout: moving from master to my-branch
...

$ git reset HEAD@{2} --hard


from : https://stackoverflow.com/questions/41049711/git-how-to-rollback-a-rebase/41049867

2020年8月18日 星期二

How do I rename both a Git local and remote branch name?

Enter image description here


There are a few ways to accomplish that:

  1. Change your local branch and then push your changes
  2. Push the branch to remote with the new name while keeping the original name locally

Renaming local and remote

# Rename the local branch to the new name
git branch -m <old_name> <new_name>

# Delete the old branch on remote - where <remote> is, for example, origin
git push <remote> --delete <old_name>

# Or shorter way to delete remote branch [:]
git push <remote> :<old_name>

# Push the new branch to remote
git push <remote> <new_name>

# Reset the upstream branch for the new_name local branch
git push <remote> -u <new_name>

Enter image description here


Renaming Only remote branch

Credit: ptim

# In this option, we will push the branch to the remote with the new name
# While keeping the local name as is
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

Important note:

When you use the git branch -m (move), Git is also updating your tracking branch with the new name.

git remote rename legacy legacy

git remote rename is trying to update your remote section in your configuration file. It will rename the remote with the given name to the new name, but in your case, it did not find any, so the renaming failed.

But it will not do what you think; it will rename your local configuration remote name and not the remote branch. 


from : https://stackoverflow.com/questions/30590083/how-do-i-rename-both-a-git-local-and-remote-branch-name

2020年5月11日 星期一

Undoing a git rebase

Actually, rebase saves your starting point to ORIG_HEAD so this is usually as simple as:
git reset --hard ORIG_HEAD
However, the reset, rebase and merge all save your original HEAD pointer into ORIG_HEAD so, if you've done any of those commands since the rebase you're trying to undo then you'll have to use the reflog.

from : https://stackoverflow.com/questions/134882/undoing-a-git-rebase