Skip to content

Instantly share code, notes, and snippets.

@epcim
Last active October 23, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save epcim/110301f92a0d6f84d093 to your computer and use it in GitHub Desktop.
Save epcim/110301f92a0d6f84d093 to your computer and use it in GitHub Desktop.
git howto usage guide

Various GIT usage tutorials/examples

Configuration

git config core.trustctime false
git core.filemode false

git config user.name "Alice Bob"
git config user.email Alice_bob@example.com

git config --global user.name "Alice Bob"
git config --global user.email Alice_bob@example.com

whitespaces

git config core.whitespace nowarn

Also, for Unix/Mac users:

Execute:
git config --global core.autocrlf input
git config --global core.safecrlf true

And for Windows users:

Execute:
git config --global core.autocrlf true
git config --global core.safecrlf true

git config user.name    ""
git config user.email   ""
git config --global user.name ""
git config --global user.email ""

git config --global user.name "Petr Bob"
git config --global user.email petr_bob@example.com

Git Workflow

##Git-svn workflow

##Git SVN manualy

git init
svn ls -R | grep -Z -v '/$' | xargs -0 git add
OR # svn ls -R | grep -v '/$' | xargs git add
git commit -m "message"

##git subnmodule

git init .
git submodule add https://github.com/opscode-cookbooks/python.git cookbooks/python
git submodule add https://github.com/opscode-cookbooks/yum.git cookbooks/yum

Get new tags from the remote

$ git fetch --tags                                                      

Get the latest tag name

$ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

Checkout the latest tag

$ git checkout $latestTag

Check out external repo with all pull requests

https://gist.github.com/piscisaureus/3342247

To your origin add:

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Or:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"

To fetch only one pr:

git fetch origin pull/7324/head:pr-7324

Explained:

  • origin points to the remote server.
  • pull/7324/head is the remote pull request.
  • pr-7324 is the local pull-request branch.

Howto checkout remote branch

git fetch
git branch -b aBranchName origin/aBranchName

Export

git archive master | bzip2 >source-tree.tar.bz2
git archive --format zip --output /full/path/to/zipfile.zip master

Revert

to previous commit (destroying/updating workdir) - without HARD it just update HEAD and do not touch workdir

git_WORK_TREE=$PWD git reset --hard bc16dd2db065fcd1718c7e621e89e5ac61497a2e

Bare repository

mkdir -p git/example.git
cd git/example.git
git --bare inito

git remote add exampleserver ssh://exampleuser@example.com/~/git/example.git

git push exampleserver master

Cherry pick

git remote add philomat git://github.com/philomat/django-cms-2.0.git # start tracking remote repo
git fetch philomat # fetch data of the repo locally
git cherry-pick efd7e5 # apply commit to active branch
git remote rm philomat # stop tracking remote repo (until next time... :-)

Pull change from somebody else fork

git pull https://github.com/USER/PROJECT/ BRANCH

alternatively: http://stackoverflow.com/questions/6022302/pull-requests-from-other-forks-into-my-fork

git remote add otherfork git://github.com/request-author/project.git
git fetch otherfork

git rebase master otherfork/pullrequest-branch

git cherry-pick <first-SHA1> <second-SHA1> <etc.>

Howto add a changed file to an older (not last) commit in Git

Use git rebase. Specifically:

  • Use git rebase -i HEAD~10 (or whatever you need to see far enough back).
  • Mark the commit in question (a0865...) for edit.
  • Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.
  • Add your file with git add.
  • Amend the commit with git commit --amend.
  • Do a git rebase --continue which will rewrite the rest of your commits against the new one.

Howto update from original repo to forked repository on github

git remote add upstream "https://github.com/opscode-cookbooks/openldap.git"
git fetch upstream
git merge upstream/master master
#or
git rebase upstream/master

Howto move recent commits to new branch

From:

master A - B - C - D - E

To:

newbranch     C - D - E
            /
master A - B 

To new branch:

git branch newbranch
# Go back 3 commits. You *will* lose uncommitted work.*1
git reset --hard HEAD~3
git checkout newbranch

WARNING The method above works because you are creating a new branch with the first command: git branch newbranch. If you want to use an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3. If you don't merge your changes first, they will be lost. So, if you are working with an existing branch it will look like this: Moving to an existing branch:

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

Step by Step

GIT SVN SYNC TUTORIAL (with transport host)

WORKSPACE=~/hg2g/cli/myparty/projects/poc_myorg-cloud2/workspace_dev cd $WORKSPACE ./_mount_dev-wkst.sh

git svn clone https://gator.blueit.cz/myorg/svn/cloudpilot_src/trunk/solution/apde blueit-apde.git

###git clone blueit-apde.git $WORKSPACE/_sshfs_dev-wkst/apde_workspace2

git reflog XXX

######################################################################### ######UPDATE SVN FROM PETRS' WKST #########################################################################

git svn fetch -all
git svn renase
git svn checkout -b newFeature

##MODIFY git commit git checkout master git merge newFeature git svn dcommit

######################################################################### ######UPDATE SVN FROM DEV WKST #########################################################################

FIRST UPDATE DEV myorg-MASTER WITH SVN

##On Developement workstation ############################### git branch -b dev

develop a feature

add and commit

git rebase master 		## PRIDANO POZDEJI
git checkout master
git merge dev

##On Petr's computer ############################### WORKSPACE=~/hg2g/cli/myparty/projects/poc_myorg-cloud2/workspace_dev cd $WORKSPACE ./_mount_dev-wkst.sh cd blueit-apde.git cd blueit-custom_web.git

git remote add dev-wkst $WORKSPACE/_sshfs_dev-wkst/apde_workspace2

git remote add dev-wkst $WORKSPACE/_sshfs_dev-wkst/cweb_workspace

IN myorg UNCOMENT PROXY HOST VARIABLE IN; vim ~/.gitconfig +21

git checkout -b merge-myorgdev
git pull dev-wkst master
git status
git checkout master
git merge merge-myorgdev

git tag "Release3"

git svn dcommit
git branch -d merge-myorgdev

######################################################################### #####UPDATE DEV WKST FROM SVN #########################################################################

##On Petr's ############################### WORKSPACE=~/hg2g/cli/myparty/projects/poc_myorg-cloud2/workspace_dev cd $WORKSPACE ./_mount_dev-wkst.sh cd blueit-apde.git cd blueit-custom_web.git

git branch -d merge-svn
git checkout -b merge-svn		#new
git svn fetch --all
				# do commit ? merge ?
git svn rebase master			#new
git checkout master			#new
git merge merge-svn			#new
git branch -d merge-svn
git push dev-wkst master:remoteUpdates

##On Dwks ############################### git checkout master git merge remoteUpdates git branch -d remoteUpdates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment