Overwrite local staging or master with remote

Must use Git Bash.

To overwrite local master with remote:

git checkout master
git fetch --all
git reset --hard origin/master

To overwrite local main with remote "upstream"

git fetch upstream
git reset --hard upstream/main

When things go wrong, revert to earlier commit

When things go wrong, for example, caused by a bad commit, we need to revert back to an earlier known good commit. To identify the commit to revert to, read the commit graph carefully, pay close attention to the dots on the lines. The commit to revert to should be a closed dot, that is, not an open ended dot behind the bad commit.

Image

TortoiseGit Steps

  • Stash your changes if you have uncommitted changes
  • After identifying the commit to revert to in the graph in BitBucket
  • Switch to the...

How to remove commits from a branch

I am bad at GIT CLI. I needed to remove a couple of commits from a branch. I did it using TortoiseGit. Here are the steps. First, right click Tortoisegit in the File Explorer and open the Rebase... window:

Image

  1. In the Branch dropdown, select the branch.
  2. In the Upstream dropdown, select the main branch.
  3. Select Force Rebase.
  4. In the main window, click on a commit that is to be removed. Inspect the files in the second window to make sure. Then, right click on the commit and select _Sk...

How to Rebase to Another Branch in GitHUb

For examples,

  1. There are recent commits to branch A, but the commits should have been in branch B. To fix this, in branch A, use TortoiseGit to rebase to branch B.
  2. There is a new default branch called main but your PR was based on an older branch master. To fix this, in master, rebase to main.
  3. When the maintainer requests to rebase to another branch

Apply Upstream Pull Request to Local

There are 2 ways:

Using GitHub CLI gh

If local branch has the same base branch as upstream:

gh PR checkout {PR#}

Using Patch Files

If local branch diverges from the branch in which the PR is based, then we need to create patch files.

git fetch --all
git format-patch -1 <sha>

the -1 flag tells Git how many commits should be included in the patch;

-
Prepare patches from the topmost commits.

Then checkout the main/master branch and use TortoiseGit to apply the patch. Resolve any conflicts that arise.

Merge a GitHub Pull Request to Local Repo

When we need to test a PR in github or we want to incorporate a PR, we can checkout the PR locally.

To check out a pull request locally, use the gh pr checkout subcommand. Replace pull-request with the number, URL, or head branch of the pull request.

gh pr checkout {pull-request#}

[deprecated] How to remove tracked files in upstream before updating our sites

Better way is use .gitattributes, see Merge Strategies

Use Case

If we use an open source in our project, e.g. OpenMage, there are many files, e.g., README.md, install.php, etc., which are tracked but should be removed when we build the actual sites for development, staging, and production.

The method described here is the easiest that will remove the tracked files in our sites.

The git command that does the...

modgit: Deploy multiple Git repositories in a unique folder

Ways to Install Magento Extensions

  1. manual copy paste
  2. modman
  3. composer
  4. modgit

How to Contribute to Open Source Project in GitHub

We use a process called pull request, or PR for short, to contribute to open source.

Prerequisites

We need a fork repo in our GitHub account and in our local machine. See this guide to fork and update our fork.

Steps for First Commit & Push

  1. [local fork repo] Create a new branch, eg, fix_some_bug.
  2. [local fork repo] Modify, add, or delete the source code.
  3. [local fork repo] Commit changes to fix_some_bug.
  4. [local fork repo] Push ...

Error on file permission denied

error: cannot update the ref 'refs/remotes/origin/master': unable to append to '.git/logs/refs/remotes/origin/master': Permission denied
! 1ac19fbc1e..0c0a40cfb4 master -> origin/master (unable to update local ref)
error: cannot update the ref 'refs/remotes/origin/staging': unable to append to '.git/logs/refs/remotes/origin/staging': Permission denied
! 4e40349b66..fc19bfe26d staging -> origin/staging (unable to update local ref)

  • [new branch] upstream_master -> origin/upstream_maste...

Deprecated - How to Remove Unwanted Files Before Merging an Upstream Branch to Master and Staging

Deprecated method. Use this instead. See also this

For example, if we have an upstream remote from magento-lts, and we have a master branch for production, and staging branch for staging server, and we do not want to merge files related to inst...

How to migrate to Magento LTS with git with combined histories

Scenario

Magento 1 is end of life. I want to migrate an existing Mage repo to the latest from Magento - Long Term Support maintained by OpenMage .

Warning: the following is based on my broken knowledge of git. Follow at your own risks.

Steps 1 and 2 Add a New Remote and Fetch

In local repo, add an upstream remote:

Image

After clicking the Add/New Save button, continue to fetch from remote including tags.

St...

How to create new repo on existing code and link to remote

I wrote some code in some files in a folder called AA. I have a repo in remote in Bitbucket. Here're the steps to link my local to remote using TortoiseGit:

  1. Create a repo for the folder AA: Navigate to the folder in windows explorer and right click, in the popup menu, click Git Create repository here...
  2. Create a .gitignore file
  3. Commit the changes
  4. Set the remote: in windows explorer, right click, in the popup menu, point to TortoiseGit and click on Settings, select Git/Remote in the left panel and set the URL:

![Imag...

How to Update a Forked Repository from Upstream

Scenario

2 to 3 remote repositories in GitHub and 2 local repositories:

  1. [GitHub] Go to an owner repository (eg OpenMage).
  2. [GitHub] Fork owner's repository (from item 1).
  3. [GitHub or BitBucket] Our application with custom code.
  4. [Local] Mirror of fork in GitHub, 2 branches: upstream remote to item1, and origin remote to item 2. This local repo is for code contribution to owner.
  5. [Local] Application, 3 or more branches: upstream remote to item1, origin remote to item 2, and a master branch remote to item 3. Master c...

fatal: The remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed

$ git fetch origin
remote: Counting objects: 201, done.
remote: Compressing objects: 100% (178/178), done.
fatal: The remote end hung up unexpectedlyKiB | 10.00 KiB/s
fatal: early EOF
fatal: index-pack failed

To resolve, do

$ git config --global core.compression 0

Set local master the same as remote repository

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

A Simple Git Workflow based on A successful Git branching model

I use TortoiseGit, with default options, ie, merge with ff.

Applicable for hot-fixes and small features.

Branch Naming

Prefix the branch name with the issue# then follow by a short subject, eg, 757_duplicate_order

Commit Message

Start the commit message with issue# in square bracket follow by more object name in square brackets, and then by verb in past tense, eg, [issue#757][order] Fixed duplicate orders.

Subflow 1 Inital commit

  1. Switch/Checkout master.
    This step is crucial. Make sure you are on the **MAS...

How to check compare changes on a topic branch to master

If I want to compare changes on a topic branch to master, I find it easiest and safest to do the following:

git checkout master
git checkout -b trial_merge
git merge topic_branch

After completing the merge, it is easy to see the consolidated change from master

git diff master

When done, simply delete the trial_merge branch

git checkout master
git branch -D trial_merge

This way, the master branch never changes.

In TortoiseGit

  1. Switch/Checkout master
  2. Create Branch... based on master, name it _trialm...

prod log: code deployment

"git is not a deployment tool, so don't do that"

http://gitolite.com/deploy.html

Set up SSH keys

The following setup the SSH for root so we can use SSH to bitbucket when login as root.

  1. Putty to prod, cd to home
  2. mkdir root in /home
  3. ~~chown -R root:root root~~ ~~change owner~~
  4. cd root
  5. mkdir .ssh
  6. cd .ssh
  7. ssh-keygen -t rsa
  8. When prompted name the key bitbucket_rsa. Press enter when asked for a passphrase, which will generate a pass...

Working Tree, Head, Index

"A single git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the "current" or "checked out" branch), and HEAD points to that branch."

My understanding is that a working tree is the dir and sub-dirs within it that contain the source files. It can be anywhere, but normally it is the same dir in which the hidden .git dir is located.

HEAD is the commit at the tip of the current branch. If you've just checked out the branch, i.e. have no modified files, then its content matches...

Using Bitbucket for Automated Deployments

See also:

  • http://jonathannicol.com/blog/2013/11/19/automated-git-deployments-from-bitbucket/
  • http://symmetrycode.com/super-easy-deployment-with-git-and-bitbucket/

Bitbucket provides git hooks that can be used for code deployment. Code deployment means updating the code changes to the production server. The following requires that git client is installed in the production.

Workflow

  • Commit changes and push them up to your Bitbucket repository
  • Bitbucket sends a POST request to a deployment script on your server
  • The deplo...

How to deploy the code to shared host with git

Push Git Repo Into Shared Hosting Account Like Hostgator
What's required is that git client is install in the host.

How to work with git in an evironment with production, staging, dev servers

First, install git server in the machines. Then, install git client in local machine. After creating the remote and local repositories, the trick now is creating a hook in the remote servers to update the code change at the shell. Git hooks are scripts that are executed when specific actions are performed to our repository. In our case, we’ll want the post-receive hook to be executed when we push changes from our local repository.

Remote Server

$ mkdir -p /home/git/project_name.staging.git
$ cd /home/gi...

Pull from Remote Repository

When other made changes and push those changes to remote repository, we can pull those changes to our local repository. However, if a local file will be overwritten with the pull, an error will occur: “Your local changes to the following files would be overwritten by merge”. To allow the overwrite to happen, use:

git checkout HEAD^ file/to/overwrite
git pull

Error fatal: 'git@bitbucket.org' does not appear to be a git repository

Edit /.git/confi...