Git Aliases
Once you start developing some experience with using git on the command line, you will sometimes find yourself typing some long commands over and over again. You can create aliases in git that are very similar to the bash alias shortcuts you use in the shell.
Steps to do this are highlighted in chapter 2.7 of the Git Book authored by Scott Chacon and Ben Straub and published by Apress.
One of the commands that I use a lot is:
Setting Up Git to Ignore ELF Binaries (C/C++ Output on Linux)
The starting point for this experiment was from here
An Example using C Program Source
Let’s say we have a pre-existing directory of some C source code files:
$ lsMakefile print print.c print.h print.o
And we initialize a new git repository in that directory:
$ git initInitialized empty Git repository in /home/user/tmp/printer/.git/$ cat .gitignore# Ignore all*# Unignore all with extensions!*.*# Unignore all dirs!*/# Unignore make files!Makefile# Ignore .o files*.o# Ignorebindirbin/# or*/bin/*
Convert a Git Repo to a Bare Repo
If you have a git repository that you haven’t cloned from a remote location, i.e., one that was created locally, it is easy to convert it to a bare repository. This process describes how to:
- Take a “normal” git repository
- Move the .git directory to another location
- Convert it to a bare repository
Suppose you have a git repository called repo. To convert it to a bare repository, execute the following commands:
Git: Working with Git for the First Time
Here’s the first step every user should start with after downloading and installing git but BEFORE doing anything else: Set up your global environment variables! If you don’t do this git will abort your commit and give you an annoying message like this one:
$ git commit file.txtAuthor identity unknown*** Please tell me who you are.Rungit config --global user.email "you@example.com"git config --global user.name "Your Name"to set your account's default identity.Omit --global to set the identity only in this repository.fatal: empty ident name (for <user@localhost>) not allowed
Git: Committing to a Different Branch
Let’s imaging that you’ve made some changes to files in your work directory and then realized that you aren’t in the correct branch. Now you want to get the changes you’ve made to the right branch but no change the current branch. What do you do?
There is no git command to do this but you can use the git stash command to fix this.
This involves:
- Using the git stash command to temporarily store our changes elsewhere,
- Checkout the correct branch, and
- “Unstash” the changes to the correct branch.
Let’s say that we’ve made some changes in our repository’s new-release branch but they should have been made in the new-feature branch.
Git: Deleting Multiple Commits
Let’s say that you want to delete the last 3 commits that you’ve already pushed to the remote repository. In this example, you want 566dab6 to be the new HEAD revision.
$ git log --pretty=oneline --abbrev-commit57bc36b (HEAD -> master, origin/master, origin/HEAD) 3nd set of bad entries on 6th commitdfb4bd3 2nd set of bad entries on 5th commit0fd1e16 First set of bad entries on 4th commit566dab6 Yet more good entries on third commitd50370a More good entries on second commitb5fbc6d Good entries on first commit2cad4c7 Initial commit
Gitea Server Installation on a Raspberry Pi
Create a ‘git’ user:
$ sudo adduser git
Login as the ‘git’ user and download the gitea binary from the gitea website (choose the latest version):
wget -O gitea https://dl.gitea.io/gitea/1.14.5/gitea-1.14.5-linux-amd64 chmod +x gitea
Create a service. We’re keeping it simple here so we don’t need a lot of the database-specific stuff:
git Tips
A useful tip to beautify your output is to use:
$ git log --graph --decorate --oneline --abbrev-commit --all --date=local
when looking at your log entries. Or you can add
[alias]
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all --date=local
to your ~/.gitconfig file to use $ git lola as an alias.