2022年4月20日 星期三

git auto-complete for *branches* at the command line?

 ok, so I needed the git autocompletion script.

I got that from this url:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

No need to worry about what directory you're in when you run this as your home directory(~) is used with the target.

Then I added to my ~/.bash_profile file the following 'execute if it exists' code:

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

Update: I'm making these bits of code more concise to shrink down my .bashrc file, in this case I now use:

test -f ~/.git-completion.bash && . $_

Note: $_ means the last argument to the previous command. so . $_ means run it - "it" being .git-completion.bash in this case

This still works on both Ubuntu and OSX and on machines without the script .git-completion.bash script.

Now git Tab (actually it's git TabTab ) works like a charm!

p.s.: If this doesn't work off the bat, you may need to run chmod u+x ~/.git-completion.bash to grant yourself the necessary permission:

  • chmod is the command that modifies file permissions
  • u means the user that owns the file, by default its creator, i.e. you
  • + means set/activate/add a permission
  • x means execute permission, i.e. the ability to run the script


from: https://apple.stackexchange.com/questions/55875/git-auto-complete-for-branches-at-the-command-line

2022年4月13日 星期三

How change parent branch in git?

 A few days ago, I created new branch. I did some commits there, but after a while I noticed, that I created this new branch from the wrong parent branch. I created it from some feature branch not from the master. What I should do in this situation? I need to merge my current branch before I will merge this other feature branch and I don’t want to add not needed commits to mater branch. How can I handle it? Well, here is the time for git.

The problem which I described above, can be resolved in at least two ways. First is easy to understand, but a little bit time consuming. We can create a new branch with parent master branch and use git cherry-pick command to move each commit from one branch to another. This solution is OK, when you don’t have many commits, because for each commit you need to do git cherry-pick. There is one more inconvenience. In many companies, there is workflow how new functionality should go to master branch. Teams use pull/merge requests to the code review. In case when you need to create a new branch, you need to create also new pull/merge request. You need to ask someone in your team to check again your code and approve it. This solution causes additional work to do.

There is another solution. We can use git rebase --onto command. It can do exactly what we need. Replace the old parent branch with new parent branch. In our case with master branch. This is like our branches tree looks like:

A---B---C---D  master
            \
              E---F---G  feature-branch
                      \
                        H---I---J current-feature-branch (HEAD)

and this is what we would like to achieve:

A---B---C---D  master
            |\
            | E---F---G  feature-branch
            |
             \
              H'---I'---J' current-feature-branch (HEAD)

To replace parent branch with master, we need to be on current-feature-branch branch and do:

git rebase --onto master feature-branch

That’s it. Right now we have our current-feature-branch branch based on master branch, not like before based on feature-branch.

In the end, I would like to say two more things here. First, if you want to move from one parent to another the command should look like this:

git rebase --onto new-parent old-parent

So right now you can adjust it to your situation.

Second, as you see one schema above, after using git rebase --onto we don’t have exactly the same commit like before. Code is the same, but the SHA number (you know the commit identifier, for example 2d4698b) for each commit is different. Everything will be fine, when you work alone on the branch where you want to do the trick. This was my situation. In case other people also work on this branch this command can provide problems. They will have different commits then you have and then are on the remote repository. This is always asking for troubles. Keep it in mind, before you will use git rebase --onto in this solution.


from: https://womanonrails.com/replace-parent-branch

How to recover git push -f with specific sha

1)git reflog show remotes/origin/{BRANCH_NAME}

2) use https://github.url/repo/commit/a921643c to verify change

3) git reset --hard with specific sha

4) git push -f to recover with this specifc sha