Post

Git

Git is an open-source version control system, which supports your development tasks, especially in distributed code projects.

Installation

Git can be easily installed on Linux systems with the available package managers. E.g. for Debian-based systems by apt install git.

For other systems, see the download page of Git here.

Using Git

Basic workflow

Clone repository

1
2
3
git clone https://https://github.com/<USER>/<REPOSITORY>.git ./repo

cd repo

Make some changes and push them to GitHub

1
2
3
4
5
git add .

git commit -m "my_commit"

git push

After smo more changes to a local files you need to upload them to GitHub

1
2
3
4
5
git pull

git commit -m "my_second_commit"

git push

The following commands can help work with git.

git commandComment
git initInitialize a directory as git managed repository
git clone <repo_url>Clone a remote repository to your local client
git statusShows uncommited changes, new files etc.
git add <wildcard_or_filename>Stage an updated / new file to the next commit
git rm <wildcard_or_filename>Remove a file and stage the removal for the next commit
git commit -m "<commit message">Commit staged changes under a new commit
git commitWill open an editor to write more descriptive commit messages.
See here for a guide on good commit messages
git checkout <branch_name>Switch to another branch
git branchShows a list of existing branches
git branch <branch_name>Creates a new branch (from the currently checked out branch)
git merge <branch_name>Merge changes from branch_name to the currently checked out branch
git pushPush commited changes to the remote repository
git pullPull current state from the remote repository to your local repo

Working with git-flow

Git-flow assists you by combining multiple steps of git commands into one git-flow command which will do a workflow of steps. Although git-flow makes life easier in some cases, it makes it also more complex sometimes and you need to execute some steps before or after using a git-flow command as regular git command. (See below)

As an example, here is the comparison between the regular git commands and the appropriate the committed git-flow command for creating a release.

git-flow commandgit command
git-flow feature start <feature_name>git checkout -b feature/<feature_name> develop
git-flow feature finish <feature_name> [--squash]git checkout develop
 git merge [--squash] --no-ff feature/<feature_name>
 git branch -d feature/<feature_name>

Another git-flow cheat sheet can be found here.

GitHub Users

Your first time with git and github

If you’ve never used git or github before, there are a bunch of things that you need to do. It’s very well explained on github, but repeated here for completeness.

  • Get a github account.
  • Download and install git.
  • Set up git with your user name and email.

    • Open a terminal/shell and type:

      1
      2
      
      git config --global user.name "Your name here"
      git config --global user.email "your_email@example.com"
      

      I also do:

      1
      2
      
      git config --global color.ui true
      git config --global core.editor emacs
      

      The first of these will enable colored output in the terminal; the second tells git that you want to use emacs.

  • Set up ssh on your computer. Also see github’s guide to generating SSH keys.

    • Look to see if you have files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.
    • If not, create such public/private keys: Open a terminal/shell and type:

      1
      
      ssh-keygen -t rsa -C "your_email@example.com"
      
    • Copy your public key (the contents of the newly-created id_rsa.pub file) into your clipboard. On a Mac, in the terminal/shell, type:

      1
      
      pbcopy < ~/.ssh/id_rsa.pub
      
  • Paste your ssh public key into your github account settings.

    • Go to your github Account Settings
    • Click “SSH Keys” on the left.
    • Click “Add SSH Key” on the right.
    • Add a label (like “My laptop”) and paste the public key into the big text box.
    • In a terminal/shell, type the following to test it:

      1
      
      ssh -T git@github.com
      
    • If it says something like the following, it worked:

      1
      2
      
      Hi username! You've successfully authenticated, but Github does
      not provide shell access.
      

The Silver Bullet

Want Just Works™? This is the magic silver bullet.

Get your access token (see the section in the cheat sheet if you need the GitHub or Gitea instructions for that) and set it in an environment variable (both for local development and deployment):

1
MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

For GitHub, copy and run these lines verbatim:

1
2
3
git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"

Congratulations. Now any automated tool cloning Git repositories won’t be obstructed by a password prompt, whether using HTTPS or either style of an SSH URL.

Not using GitHub?

For other platforms (Gitea, GitHub, and Bitbucket), just change the URL. Don’t change the usernames (although arbitrary, they’re needed for distinct configuration entries).

Compatibility

This works locally in macOS, Linux, Windows (in Bash), Docker, CircleCI, Heroku, Akkeris, etc.

More information

See the “.gitconfig insteadOf” section of the cheat sheet.

Security

See the “Security” section of the cheat sheet.

Using git-crypt

Having secret or sensitive information in your git repository is never a good choice. But sometimes it’s necessary. Never push unencrypted data to your remote repository.

Git-crypt is a transparent encryption tool that works seamlessly with your Git repository. All sensitive information is encrypted before being pushed to the remote repository. Once you’ve unlocked the repository locally, all data will be decrypted automatically when pulled from the remote repo. This makes development with encrypted data effortless.

To install git-crypt, you can use your package manager of choice (e.g. apt):

1
sudo apt install git-crypt

To initialize a new repository with git-crypt, you can use git-crypt init when located in the repository directory. An already encrypted git repository can be unlocked by git-crypt unlock. This requires you to have either the repository encryption key in your GPG keychain or that your private GPG key has been added to the allowed keys in the repository. For more details, see the links below.

For more information, check out the official GitHub repository here. A tutorial on git-crypt can be found here.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.