«

»

Dec 20

GIT CHEAT SHEET

Adding a remote branch called staging to myapp local and remote:

cd /myapp
git branch staging (creates the local branch called staging)
git push git@github.com:TLC-Tech/myapp.git staging:refs/remotes/origin/staging
(creates the remote branch called staging)


Fixing conflicts when merging YOUR newly updated branch to an OBSOLETE branch

 

git checkout YOUR_branch
git merge -s ours obsolete_branch

 

Configuring a local directory to a remote github repo by the same name

1. cd into the local directory

cd mydir

2. Initialize the parent directory as a local git repository

git init

3. Configure directory references to the remote git server

git remote add origin git@github.com:TLC-Tech/mydir.git

mydir – Needs to be replaced with the actual directory and/or repository name.


Using GIT to Install a Directory

Git does not know about files or directories, only commit ids and branches.  Thus, create a branch whose sole purpose is to house the directory or file you want.  In this example, install the ‘scripts’ directory to /usr/local on a generic server.

1. cd to the parent directory for the directory to be installed

cd /usr/local

2.  Initialize the parent directory as a local git repository

 git init

3.  Pull the scripts directory references from the remote git server (unixteam repository on github.com)

git pull git@github.com:TLC-Tech/myteam.git scripts:refs/remotes/origin/scripts

script – Needs to be replaced with the actual repository name.


Error Pushing Local Changes to Remote Git

1. After git checkout of the branch, changes made, run git add, git commit (examples further down), push in dry mode

 git push –dry-run

 

 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to ‘git@github.com:TLC-Tech/myteam.git’
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. ‘git pull’) before pushing again.  See the
‘Note about fast-forwards’ section of ‘git push –help’ for details.

This error means newer changes are in the remote branch.  Run:

 git pull

If you are unsure of the changes in the remote branch and worried they will over-write your changes; copy your changed files to a separate directory (outside of the repo path) before running git pull.  After you’ve copied the changed files back into your local path, run git add and git commit before trying to push again.


Using GIT to Update an Existing Directory

Similar to installing a new directory, except use ‘git checkout branchname‘ instead of ‘git init’ to preserve data

cd /usr/local
git checkout scripts

git pull git@github.com:TLC-Tech/myteam.git scripts:refs/remotes/origin/scripts

 


GIT Setup For Developers & Vendors

 

For these examples, we will work with the EF-Magento repository.  Initialize a local repository.
Configure a remote repository, defining a remote master called ‘newbie’, have it track ‘staging’ branch.

mkdir /tmp/newbie
cd /tmp/newbie
git init
git remote add -t staging -m newbie origin ssh://git@github.com/TLC-Tech/MY-Product.git

Pull GIT ‘staging’ branch:

git pull

create a new branch and switch to it (in this case, newbie):

 

 git checkout -b newbie

 

Add the files you pulled from staging to your local newbie branch

git add .

Commit files pulled (may claim nothing to commit, but still required):

git commit -m “Newbie Branch”

Do not merge.  Just push local ‘origin’ to local ‘newbie’ branch

git push origin newbie

The master branch may change during the
day or days of editing the ‘newbie’ branch.  To update local ‘newbie’ branch with the newest changes from the
‘master’ that came while working on newbie, but also to preserve all
the changes made to ‘newbie’; do this while still in ‘newbie’ branch:

 

 git rebase master

 

Create a remote branch named myvendor using staging as the reference

 

 git push origin staging:refs/heads/myvendor

 

=========== Optionally, REMOVE a remote branch — this is potentially dangerous! =======

 

git push origin :heads/newbie

 

So, the previous command makes only a little sense: push a local
branch without reference to the remote branch’s origin, thus the branch is deleted from the remote repository.

To delete the local version of the ‘newbie’ branch:

 git branch -d newbie


GIT Branch Set-up For Remotes and Merges



Functional branches each act as their own repository. Though primarily useful for keeping contributors’ sources separate until ready to merge; a generous use of branching in a single repository can also help reduce costs while also reducing headaches associated with managing multiple repositories.

As an example, unixteam utilizes many independent branches that are never merged, under one repository called ‘unixteam’.  Each branch manages different files, many share the same name but have different content as found in their branch counter-parts.  To manage how the branches are merged, sources and push destinations, git uses a config file.

Though there are git commands to set the environment configuration, it is usually easier to copy/paste a branch section of an existing (working) config to a new section of the same config, changing the local and remote names, as needed.

 cd /unixteam

cat .git/config

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “origin”]
    fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:TLC-Tech/
myteam.git
[branch “master”]
remote = origin
merge = refs/heads/master
[branch “prod”]
    remote = origin
merge = refs/heads/prod
[branch “dev”]
remote = origin
merge = refs/heads/dev[branch “scripts”]
remote = origin
merge = refs/heads/scripts
[branch “db”]
remote = origin
merge = refs/heads/db

Copy, paste and edit sections from the example above if you do not have a good config to sample on your local machine.





GIT Routines For Support, Vendors & Developers

 

Remove the existing local repo and all branches and clone (this may be overkill, see git rebase below):

rm -rf BC-SERVICEgit clone git@github.com:TLC-Tech/BC-SERVICE


change directory to the new local home:

 

 cd BC-SERVICE

change to using a branch, below uses the newbie branch:

git checkout -b newbie

optionally, verify an asterisk is next to your branch:

 git branch -a

optionally, instead deleting and cloning above, start from cd
EF-Magento, checkout the existing branch, then ‘re-clone’ the branch
while preserving recent changes (it works!):

 git rebase master

 

copy in your changes to the correct path under trunk:

 cp /path_to_new_files    /full_path_under_trunk/

see if local changes are noticed by git — don’t panic if this step yields nothing:

 git diff –name-only

while still in git home MY-Product, git add the files you just copied:

 git add .

commit the files to the local repo.  comments are required.

 git commit -m “Updates for 12-02-2011 (file1.phtml, etc)”

merge the local repo with the local origin

git merge origin/newbie

ready to push to the remote master — because you are still in newbie branch, that’s where it goes:

 git push

THAT’S IT!   To see
the differences between the merged (local) newbie branch and the
(local) master, this command yields the diffs until newbie is merged
with master:

 git diff –name-only master newbie –OR — git diff –summary master newbie

Resolve merge conflicts by keeping YOUR changes
 

 



Fetch a branch (called staging) from My-Repo without cloning entire repository

 

 mkdir staging

 

 cd staging

 

 git init

Initialized empty Git repository in /staging/.git/
git fetch git@github.com:TLC-Tech/MY-Product.git staging:refs/remotes/origin/stagingremote:
Counting objects: 19177,
done.
remote: Compressing objects: 100% (9896/9896),
done.

remote: Total 19177 (delta 7038), reused 18988 (delta
6852)

Receiving objects: 100% (19177/19177), 66.33 MiB | 8.94 MiB/s, done.
Resolving deltas: 100% (7038/7038), done.
From github.com:TLC-Tech/My-Product
* [new branch]      staging    -> origin/staging

 


 


Start tracking a local branch (i.e. not from a clone) to fix push error


 git push origin origin:refs/heads/newbie

error: src refspec origin does not match any.
error: failed to push some refs to ‘https://admin@github.com/TLC-Tech/myteam.git


1)  Switch to the new branch (called staging), then use “–set-upstream” option to push:

 

git checkout staging

 

 

git push –set-upstream origin staging  Counting objects: 192, done.
Compressing objects: 100% (72/72), done.
Writing objects: 100% (100/100), 76.71 KiB, done.
Total 100 (delta 17), reused 83 (delta 10)
To git@github.com:TLC-Tech/MY-Product.git
7473bf3..d97efcd  staging -> staging
Branch staging set up to track remote branch staging from origin
.

 



Reset a commit

 

1)  Undo and remove history of the most recent commit:

 

 git reset HEAD^

 

 git push –force



 

Revert a commit

 

1)  Undo the commit (keep history) and push the fixed branch to remote:

 

 git revert HEAD

 

 git push

 

 

 

Recover/Un-Revert/Re-do a previous commit

 

1) Find the commit number in github.

 

2) Clone the current database, then ‘cherry-pick’ the commit
you found in step 1:

git clone git@github.com:TLC-Tech/MY-Product.gitMY-Productgit cherry-pick c85b54b6b3f6c3223ea3653faf980bb9f6a7e310

 

Files are now updated on your local system.

 

3) To see the diff, rename the cherry-picked clone and
create a new clone and diff on your local system:

mv MY-Product MY-CHERRY-PICKgit clone git@github.com:TLC-Tech/MY-Product.gitgit diff –name-only MY-PRODUCT/ MY-CHERRY-PICK/

 

4) To update remote, cd into the cherry-picked directory, and
commit/push back to github

cd MY-CHERRY-PICKgit push