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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.