Skip to content

Instantly share code, notes, and snippets.

@dartov
Created February 14, 2014 12:20
Show Gist options
  • Save dartov/9000104 to your computer and use it in GitHub Desktop.
Save dartov/9000104 to your computer and use it in GitHub Desktop.
A simple demo how one can alter history on git server
# Creating a local git server
mkdir -p git_show/git_server
cd git_show/git_server
git init --bare
# Initiating the repo with first commit
cd ../
mkdir master_user
cd master_user
git init
git config user.name "Master User"
git config push.default matching
echo "hello" > hello.txt
git add hello.txt
git commit -m "initial"
git remote add origin ../git_server
git push origin master
# Checking out as a new user
cd ../
git clone ./git_server developer_user
cd ./developer_user
git config user.name "Developer User"
# Creating new branch
git branch temp
git push origin temp
git checkout temp
# Adding some other file
echo "buy" > buy.txt
git add buy.txt
git commit -m "Added some other file"
# Changing the initial file as a developer (not pushing to the repo!)
echo "developer says hello" > hello.txt
git add hello.txt
git commit -m "Changing initial file as a developer"
# Changing the initial file as a master, and pushing to the repo
cd ../master_user
git pull origin master
echo "master says hello" > hello.txt
git add hello.txt
git commit -m "Changing initial file as a master"
git push origin master
# Getting back to developer user, trying to pull origin master
cd ../developer_user
git pull origin master
# Oops! Conflict! Never mind, resetting hard..
git reset --hard
# Looks like it's all good, trying to push
git push
# Fail! Trying to push harder, altering history on the server
git push -f
# Ok, now let's take a look on this lost commit
git fsck --lost-found
# Ok, now cloning as some other user
cd ../
git clone ./git_server other_developer_user
cd other_developer_user
# Look! No word from our master!
cat hello.txt
# Here's our lost commit:
git fsck --lost-found
# Now do a 'git show' with hash, check the content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment