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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.