General Git Tips

Note: this can be broadly applied when using Git, this is not GNOME-specific.

If you have already learned the basics and feel comfortable with git, here is some tips and tricks you may want to employ to improve your workflow. Feel free to subscribe to the page to learn about new tricks as people add; or contribute your own.

git-config goodness

The "git config" command enables all kinds of interesting features that enhance one's experience with git. In general, when you set something with "git config --global" it will be saved in ~/.gitconfig and will apply to all your repositories, whereas without --global it will only apply to the repository of your current directory.

Running "git config -l" lists all the current configurations.

Shortcut URLs

Always thought checking out a repository is easier done using a script than typing on the command line? No more. Just setup a URL scheme for your site of choice:

git config --global url.ssh://[login@]git.gnome.org/git/.insteadof gnome:

Now you can simply do:

git clone gnome:pango

to checkout the pango tree. Yay!

Alias commands

You can define new git commands using the alias configuration. For example, to define "git up" to perform the "svn up" functionality, do:

git config --global alias.up "pull --rebase"

Or for "git ci" to work like "svn ci", do:

git config --global alias.ci "commit -a"

You get the idea.

Rebasing by default

Many users find that they happen to do "git pull --rebase" most of the time. Would be nice if "git pull" simply did that by default. You can set that up for the master branch of all your repositories by:

git config --global branch.master.rebase true

Rewriting history

Rewriting history is possible as long as none of the commits involved have been pushed out to the central repository yet (on the master or main branch, or branches for stable releases). Say, you spend an evening implementing a feature. The session may generate 10 commits. After you are done, you see that some of the previous commits were buggy and the newer commits fix bugs introduced by them. Those commits would look better if they didn't have the bug to begin with. Or you may want to reorder commits to group them more logically by what they do. Or you may just want to improve the commit messages before making the changes public. You are more than welcome to do all those as long as the commits involved are part of a "wip" (work-in-progress) branch. Once commits are pushed on a main branch, after that, well, forget it :).

Amending last commit

If you committed something (still on a "wip" branch) and you found a bug in your commit that you want to fix or simply want to change the commit message, or in any other way amend the commit, simply use the --amend option of "git commit". That is, to amend all your changes to the previous commit, do:

git commit --amend -a

and you get a chance to edit the commit message. If you just want to edit the commit message, a simple:

git commit --amend

will do.

Reshuffling multiple commits

The interactive rebase command is your friend. If you want to edit / reorder / squash-into-one the last five commits, do:

git rebase -i HEAD~5

That will bring up an editor page with the last five commits listed. You can reorder the lines to reorder the commits. Or change the word at the beginning of the line to get a chance to edit the commit, or squash it (ie. merge it) into the commit before it.

git goodness for bash users

Here's a few tips to improve your life if you bash as your main shell.

bash-completion for git

If you have ever seen bash-completion, chances are you are addicted to it. Search for a bash-completion package in your distribution and install it, most distributions package the git support with it, or in another related package.

Alternatively, you can install bash-completion for git from source (see the documentation there).

git branches in shell prompt

Wouldn't it be rad if your shell prompt included the name of the currently checked-out branch? Well, if you enabled git bash completion as described above, you're just one step away! Try adding something like the following in your ~/.bashrc:

source /usr/share/git-core/contrib/completion/git-prompt.sh
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

See the git-prompt.sh documentation for more tips, such as the GIT_PS1_SHOWDIRTYSTATE environment variable.

Open a new terminal window to see the changes take effect.

Adjust to your taste. Enjoy!

Git/General/Tips (last edited 2022-05-03 02:44:40 by SébastienWilmet)