2021年5月14日 星期五

Git - how to find first commit of specific branch

 git log master..branch --oneline | tail -1

Where "branch" is your specific branch name. The dot-dot gives you all of the commits that the branch has that master doesn't have. tail -1 returns the last line from the previous output.


from : https://stackoverflow.com/questions/18407526/git-how-to-find-first-commit-of-specific-branch

2021年3月3日 星期三

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

 If you have named a branch incorrectly AND pushed this to the remote repository follow these steps to rename that branch (based on this article):

  1. Rename your local branch:

    • If you are on the branch you want to rename:
      git branch -m new-name

    • If you are on a different branch:
      git branch -m old-name new-name

  2. Delete the old-name remote branch and push the new-name local branch:
    git push origin :old-name new-name

  3. Reset the upstream branch for the new-name local branch:
    Switch to the branch and then:
    git push origin -u new-name


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

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