How to create a new git repository on a server

  1. On your server:
    1. Install git as per the documentation related to your server
    2. Setup the default git user
      1. mkdir -p ~/.ssh && chmod 0700 ~/.ssh
      2. touch ~/.ssh/authorized_keys && chmod 0600 ~/.ssh/authorized_keys
    3. Create the repository
      1. git init –bare ~/theNewGitRepositoryNameComesHere.git
  2. On your client (suppose you already have some code to push to the new repo):
    1. Add your public key to the /home/git/.ssh/authorized_keys file (created above) on the server
    2. git init
    3. git remote add origin git@yourServerNameComesHere.com:/home/git/theNewGitRepositoryNameComesHere.git
    4. git add .
    5. git commit -m ‘Initial commit’
    6. git push -u origin master

How to create a hotfix

If you follow the Semantic Versioning notation when delivering your code it’s important to know the way to create a hotfix when needed. Here are the basic steps to follow in order to achieve it:

  1. Get the latest changes from your remote
    • git fetch
  2. Checkout the version you would like to patch with the hotfix (ex.: “v1.2.3”)
    • git checkout VERSION
  3. Create the hotfix branch for the related version. It should follow the semver convention (ex.: “v1.2.3-1”)
    • git checkout -b hotfix/HOTFIXVERSION
  4. Do your modifications, cherry-pick, apply patches, etc.
  5. Commit your changes
    • git add .
    • git commit -m “fix: The commit message comes here”
  6. Push them to the remote
    • git push -u origin hotfix/HOTFIXVERSION
  7. Tag the hotfix version and push
    • git tag HOTFIXVERSION
    • git push origin HOTFIXVERSION

Squashing multiple commits into one

In your daily tasks if you need to squash multiple commits into one with a custom message just perform the following commands:

git reset --soft HEAD~n # where n is the number of last commits you want to squash into one

git commit --amend

git push -f

another (interactive) way is to use rebase like below:

git rebase -i HEAD~n

the last n commit messages will appear in your text editor. You will see something similar to:

pick 0123456 Message 1
pick 1234567 Message 2
pick 2345678 Message 3

you could just pick the first commit and squash the rest like that:

p 0123456 Message 1
s 1234567 Message 2
s 2345678 Message 3