5 Git examples to make your life easier

5 Git examples to make your life easier

Git is one of the most popular version control systems available, designed by Linux creator Linus Torvalds. It is used in both private and public websites for all kinds of development work. No matter how fluent you are with git, there will always be features to discover. Here are 5 examples that can change the way you work with this system.


1. Copy a single file from another branch

While we are working, we may delete a file or introduce a mist in it, and we would like to start from the beginning. It may also happen that, working on a particular branch, we realize that we need a file from another branch, which we do not have at home. This command will help us retrieve such a file from another branch:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
$ git checkout release_5.59.3 -- projects/documents/src/app/modules/documents/components/zwrot-do-dost-panel/zwrot-do-dost-panel.component.ts

To copy the specified file from a specific commit, use:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
$ git checkout 2657d3e -- projects/documents/src/app/modules/documents/components/zwrot-do-dost-panel/zwrot-do-dost-panel.component.ts

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
$ git status
On branch release_5.59.2
Your branch is up to date with 'origin/release_5.59.2'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   projects/documents/src/app/modules/documents/components/zwrot-do-dost-panel/zwrot-do-dost-panel.component.ts

Instead of a single file, we can also download the entire directory, specifying the path to it. Note that all paths are relative, so if you are not in the root directory of the repository, you must specify a relative path to your files.


2. Improve last commit message

In everyday work, it sometimes happens that the description that we put in the commit is too laconic and we would like to supplement it or we find that it should be completely changed. Sometimes we forget about an important change that did not enter the commit, and we would not like to make a separate one for it. Then the command comes to the rescue:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (issue/FS-12345)
$ git commit --amend
[issue/FS-12345 5bca6ccdb] new: Poprawa commanda (#FS-12345)
 Date: Mon Mar 8 08:19:47 2021 +0100
 1 file changed, 2 insertions(+)


3. Search for a commit that contains a keyword

If we need to search for a specific piece of code where the changes were made, or if we want to find a specific keyword, we can search log by code using the command:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (issue/FS-12345)
$ git log -S "prepareApDokViewInfoRequestBody.idMag"
commit 8c54da8462cd2e2ca2c93d99a37ebb53fac32c19
Author: Kamil.Majeranek <SQ075@SQ075.poland.asseco.corp>
Date:   Wed Apr 29 11:39:51 2020 +0200

    fs-24290
    stworzenie odpowiednika ShowAptekaDokumentyCommand


4. View history without mergy commits)

Commits after merge can be annoying and sometimes displaying history, we would prefer not to see them. If we want to see history with filtered merge commits, we can use the command:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (issue/FS-12345)
$ git log --oneline --no-merges
5bca6ccdb (HEAD -> issue/FS-12345) new: Poprawa commanda (#FS-12345)
8cb5ae0af fix: Poprawa importów - budowa z prod
06f3d6f6c Merge branch 'bugfix/FS-51663' into 'FS-drugBase'
e8a00e5c4 fix: poprawa akcji Edytuj - stany mag (#FS-51663)
6d8f4459e CP and resolve conflicts
7f75e9d85 Merge branch 'bug/FS-50921' into 'FS-drugBase'
9b39ffd21 Merge branch 'ZAC3240824' into 'FS-drugBase'

However, please note that merge commits are an important part of our project history and should not be overlooked in the workflow.


5. Get rid of all untracked changes

If we create a new file that was not previously in Git history, we make an unapproved change. For Git to start tracking this file, place it in the repository (commit). It happens that in the middle of work we change the concept and want to get rid of all the untracked files and start over. The first idea would be to use the git checkout command, but this command will only remove changes to the tracked files, and the rest will remain. With help comes the command:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
$ git status
On branch release_5.59.2
Your branch is up to date with 'origin/release_5.59.2'.

Untracked files:
 	 (use "git add <file>..." to include in what will be committed)
        	projects/documents/src/app/modules/documents/components/zwrot-do-dost-panel/

nothing added to commit but untracked files present (use "git add" to track)

        Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
        $ git clean -f -d
        Removing projects/documents/src/app/modules/documents/components/zwrot-do-dost-panel/


6. Secret list of helpful commands

Finally, the last very useful and obvious command:

Agnieszka.Pietka@aps00082350-mb MINGW64 /c/Projekty/Flexit/module-pharmacy (release_5.59.2)
$ git help -g

The common Git guides are:
   attributes          Defining attributes per path
   cli                 Git command-line interface and conventions
   core-tutorial       A Git core tutorial for developers
   cvs-migration       Git for CVS users
   diffcore            Tweaking diff output
   everyday            A useful minimum set of commands for Everyday Git
   glossary            A Git Glossary
   hooks               Hooks used by Git
   ignore              Specifies intentionally untracked files to ignore
   modules             Defining submodule properties
   namespaces          Git namespaces
   repository-layout   Git Repository Layout
   revisions           Specifying revisions and ranges for Git
   tutorial            A tutorial introduction to Git
   tutorial-2          A tutorial introduction to Git: part two
   workflows           An overview of recommended workflows with Git

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.


Summary

Git is a tool that is full of neat tricks that make our daily work easier. The above list is by no means exhaustive or complete, so it is worth delving into the git documentation yourself.

Go to our cases Get a free quote