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

2021年11月21日 星期日

How do I force "git pull" to overwrite local files?

 

⚠ Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven't been pushed will be lost.[*]

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.


First, run a fetch to update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch:

git branch backup-master

Then, you have two options:

git reset --hard origin/master

OR If you are on some other branch:

git reset --hard origin/<branch_name>

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master


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

2021年10月1日 星期五

csv class writer : a bytes-like object is required, not 'str'

 open(path, 'w')

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'

from: https://stackoverflow.com/questions/43413018/csv-class-writer-a-bytes-like-object-is-required-not-str

Remove files from Git commit

 I will explain to you with example.

Let A, B, C be 3 successive commits. Commit B contains a file that should not have been committed.

git log  # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>  


from: https://stackoverflow.com/questions/12481639/remove-files-from-git-commit

2021年9月30日 星期四

Git: Move Your Latest Commits to Another Branch

 Sometimes we start writing code without managing our branches in advance. Then we make commits in master (or some other base branch) when we intended to carve off a new feature branch. In this guide, we'll learn how to move our latest commits to another branch, leaving our original branch unchanged.

Before we do anything, we need to assess the situation. Assuming you have checked out the branch that we're going to modify, we need to see the history.

Use git log to deteremine how many commits to move.

git log

You'll see output similar to this:

commit 5576dbf62182ac1d263e9777e31ff7f35ac6eee3 (HEAD -> master)
Author: Tyler <tyler@email.com>
Date:   Fri Nov 8 12:04:42 2019 -0800

    Another commit to move

commit edec499e8c85adf8c6fd79bc1b6993bfb233a5a0
Author: Tyler <tyler@email.com>
Date:   Fri Nov 8 12:04:29 2019 -0800

    One commit to move

commit 896cfcd0ae55d95fa81915a60460948b40fa55fa (origin/master, origin/HEAD)
Author: Zach Levine <zach@email.com>
Date:   Thu Nov 7 10:40:13 2019 -0500

    Awesome code added to the repository.

We can see that that HEAD is two commits ahead of origin/HEAD, and these are the two commits we want to move to another branch.

In the remaining steps, we'll cover to how move these commits to a new branch or an existing branch.

The following steps will show you how to move your latest commits to a new branch.

Create a new branch
git branch feature/newbranch

This will create a new branch including all of the commits of the current branch.

Move the current branch back two commits
git reset --keep HEAD~2
Checkout the new branch
git checkout feature/newbranch

That's it! Your two latest commits have been removed from master (or your current branch), and they've been included in a new branch called feature/newbranch.

The following steps will show you how to move your latest commits to an existing branch. This is useful if you've been working out of a feature branch, but accidentally started making commits in the wrong branch.

We'll assume that the "current" branch, with the commits that need to be removed, is master.

Check out the existing branch
git checkout feature/existingbranch
Merge master
git merge master

This will add the additional commits to the existing branch.

Checkout master
git checkout master
Move the current branch back two commits
git reset --keep HEAD~2

This is it! The latest two commits have been removed from master and added to an existing branch.


from: https://howchoo.com/git/git-move-your-latest-commits-to-another-branch