2023年1月4日 星期三

How to replace local branch with remote branch entirely in Git?

 

  1. Make sure you've checked out the branch you're replacing (from Zoltán's comment).
  2. Assuming that master is the local branch you're replacing, and that "origin/master" is the remote branch you want to reset to:

    git reset --hard origin/master
    

This updates your local HEAD branch to be the same revision as origin/master, and --hard will sync this change into the index and workspace as well.


from: https://stackoverflow.com/questions/9210446/how-to-replace-local-branch-with-remote-branch-entirely-in-git

2022年11月9日 星期三

How to switch git submodule with other branch properly?

 You can edit .gitmodules manually (using an editor) or using git config -f .gitmodules. After modifying sync it to .git/config with the command git submodule sync and update submodules using git submodule update --init --remote.


from: https://stackoverflow.com/questions/58012887/how-to-switch-git-submodule-with-other-branch-properly

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