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

2021年7月29日 星期四

Implicitly loading submodules in Jenkins pipeline stages

I assume you have been using the git step so far to clone a repository.

However for advanced features (like submodules) there's the checkout step available: https://jenkins.io/doc/pipeline/steps/workflow-scm-step/

The checkout steps provides an option to update all submodules and can even update the submodules recursively, e.g.:

checkout([$class: 'GitSCM',
    branches: [[name: '*/master']],
    doGenerateSubmoduleConfigurations: false,
    extensions: [[$class: 'SubmoduleOption',
        disableSubmodules: false,
        parentCredentials: false,
        recursiveSubmodules: false,
        reference: '',
        trackingSubmodules: false
    ]],
    submoduleCfg: [],
    userRemoteConfigs: [[url: 'ssh://myserver/myrepo']]
])

What actually enables cloning of the submodules is the SubmoduleOption extension as seen in the example above.

As the syntax is - let's say - little bit more complex I recommend using the snipped generator. 


from: https://stackoverflow.com/questions/51361907/implicitly-loading-submodules-in-jenkins-pipeline-stages

2021年7月28日 星期三

Get the current commit id of specified submodule

 You can start with git ls-files -s (as in this answer)

cd /path/to/parent/repo
git ls-files -s yourSubmodule


from: https://stackoverflow.com/questions/32327108/get-the-current-commit-id-of-specified-submodule

How to get the last commit ID of a remote repo using curl-like command?

 git log --format="%H" -n 1


from: https://stackoverflow.com/questions/19176359/how-to-get-the-last-commit-id-of-a-remote-repo-using-curl-like-command

2021年7月27日 星期二

using “git submodule foreach” can you skip a list of submodules?

 lets say I have 10 submoules:

module/1
module/2
module/3
module/4
module/5
module/6
module/7
module/8
module/9
module/10

so use the shell:

 git submodule foreach 'case $name in 4|6|7) git status ;; *) git status ;; esac'

from: https://stackoverflow.com/questions/58291315/using-git-submodule-foreach-can-you-skip-a-list-of-submodules

Check if git submodule has been updated on remote

 Based on https://stackoverflow.com/a/3278427/7976758 I'd do something like this:

git submodule foreach git remote update
git submodule foreach git --no-pager diff origin/master master

If submodules use different branches the script needs to take that into account.


from: https://stackoverflow.com/questions/50965094/check-if-git-submodule-has-been-updated-on-remote

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