Skip to content

Instantly share code, notes, and snippets.

@tothi
Last active August 15, 2024 20:26
Show Gist options
  • Save tothi/f9f17706924551f41afd37321dc5062d to your computer and use it in GitHub Desktop.
Save tothi/f9f17706924551f41afd37321dc5062d to your computer and use it in GitHub Desktop.
git cheatsheet for basic things
# git cheatsheet: some basic git instructions (w.i.p.)
# create bare repo for an existing source tree
git init --bare project.git
cd project.git
# edit exclude
edit info/exclude
# add files to bare repo
git --work-tree=/path/to/project add .
# edit user.email and user.name if needed
git config user.email "[email protected]"
git config user.name "Git Developer"
# commit added files as an initial commit
git --work-tree=/path/to/project commit -m "initial commit"
# clone to local repo
git clone ssh://user@gitserver:port/path/to/project.git
# start working
# list branches (local & remote)
git branch -v
git branch -r -v
# list remote targets
git remote -v
# get repo status
git status
# create (and checkout) a new feature branch
git checkout -b test-feature
# edit files...
# commit (and add new files as necessary)
git add testfeature.html
git commit -a -m 'add test feature'
# push it to the central repo (as a backup)
# note: -u set for upstream tracking reference
git push -u origin test-feature
# switch to branch master
git checkout master
# after switching to master, merge test-feature into master
git merge test-feature
# push changes
git push
# delete branch locally and remotely as well
git branch -d test-feature
git push origin :test-feature
# automatically deploy master branch to production site dir using git hook
-> remote git bare repo: project.git/hook/post-receive
------- BEGIN project.git/hook/post-receive
#!/bin/bash
#
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/var/www/project/site checkout -f
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
------- END project.git/hook/post-receive
# edit commits (delete / join / etc...) backwards until [commit id]
git rebase -i [commit id]
# after local edit, push to remote (WARNING: may mess up work in case of collaboration)
git push -f
# check remote log
git log origin/master
# create testing repo for testing deployment
git init --bare project-testing.git
cd project-testing.git
# copy exclude
cp ../project.git/info/exclude ./info/
# edit user.email and user.name if needed
git config user.email "[email protected]"
git config user.name "Git Developer"
# copy deployment hook
cp ../project.git/hooks/post-receive ./hooks/
# customize deployment hook for testing webroot
edit ./hooks/post-receive
# create testing remote target for local repo
git remote add testing ssh://user@gitserver:port/path/to/project-testing.git
# create development branch
git branch development
# push development to testing remote (to try out development branch)
git push testing development:master
# create a new feature
git checkout -b test-feature
git add testfeature.html
git commit -a -m 'add test feature'
git push -u origin test-feature
# merge the feature into development branch
git checkout development
# push (and create) development branch on origin
git push -u origin development
# try the development version on the test server
git push testing development:master
# working with a fork:::
# create fork on github with a click (ctfs/write-ups-2016)
# clone to local
git clone [email protected]:tothi/write-ups-2016.git
# create upstream link to origin
git remote add upstream https://github.com/ctfs/write-ups-2016.git
# sync from upstream
git fetch upstream
git checkout master
git merge upstream/master
# edit local...
git commit -a
git push
# create pull-request from fork to origin on github (press new pull request in updated fork)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment