Tips from the experts on getting more from Git
Tip 4: Just Ignore!
Git has at least three configuration files in which you can list files that you want Git to ignore:
.gitignore
– this file is the right place for anyone who wants to create an exclusion list for all users in their current repository..git/info/exclude
– the rules listed here for files to exclude only apply to your individual repository; they do not affect anyone else and only apply on the local machine.~/.gitignore
– behaves in a similar way to.git/info/exclude
, but applies to all repositories on the local machine.
If you want to use ~/.gitignore
globally, use the following command:
git config --global core.excludesfile ~/.gitignore
If you want to learn more about ~/.gitignore
, you will find further information online [9]. In addition, a tool from SUSE's senior engineer Adam Spiers might come in handy. Adam developed git check-ignore
[10]. This command is built into Git and checks files and path names to discover whether or not they are on one of the local ignore lists. In this way, developers can avoid surprises when working with third-party repositories.
Tip 5: Retroactive…
Thomas offers another tip, but one that developers will not want to apply to public commits. Git lets you retroactively add whole files to a commit. If you have staged files with git add
and already checked them into the repository with git commit
, but forgot some files, you can add them later with git commit --amend
.
First, simply call git add
again and specify the forgotten file as a parameter. Then git commit --amend
comes into play; this command displays the commit message from the previous commit in the editor. If you want, you can change it now, but it's not absolutely necessary. By the way, amend also supports rm
for later removal of files. More information is available in the Atlassian [11] wiki.
Tip 6: Stashing with Stash
Every developer is familiar with this scenario: A developer is working on an important project when The Boss comes in with a new, even more important task. The new task suddenly has priority, but you will need to continue working on the old task at a later time. You will have to check in the code for the new task before you check in the code for the original task.
Git supports a strategy known as stashing for such situations: Changes that the developer has not yet checked in end up in stashes for processing at some later time.
git status
first displays the list of changed files; git stash
moves these changes to a stash. Now you can work on the new task. After the work is done, call git stash pop
, and you can continue working at the point you reached before the interruption. Git Stash can be nested multiple times, so commands like this exist:
git stash list
– lists all stashesgit stash drop
– removes the last stashgit stash branch test
– generates a new branch based on the stash
Attention: Stash only works on added files by default. If you also want to have unmonitored files in your stash, you have to add git add --all
and git stash
explicitly. You'll find more information at the Git SCM site [12].
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.