top of page
Search

GIT

  • Writer: Marko Brkusanin
    Marko Brkusanin
  • Dec 25, 2024
  • 3 min read

Updated: Jun 28

From the moment when GIT was invented, it became the most used tool for file versioning. Git can be used for versioning of any type of files in any industry. You can use it if you are writer, designer, if your profession requires work with files.

GIT gain its popularity among software engineers and became a standard tool for code versioning. Other web platforms like GitHub and GitLab use GIT as a background engine.


GIT is Mostly used via Web UI or some specific GUI desktop tools or IDE. This simplifies daily usage for most of the engineers, because they don't need to know git commands.

Recently I had to use GIT on remote servers in the cloud, where GUI is not an option, and I was forced to learn GIT commands in order to get more details about cloned repositories and changes made to it.


Here are some useful commands which helped me a lot.


Get current branch
It is really important to find out what is the current branch you are working on. To get current branch, run this command:

git branch --show-current

Figure 1 - Get current branch



Create new branch

New branch can be easily created using this command:
git checkout -b "<branch_name>"
You can then make changes on this branch and push it to remote repository.

Push new branch

When you create a new branch and try to push it to a remote repository using command "git push", you will always be prompted with a message:
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

     git push --set-upstream origin <branch_name>
 This can be really annoying and if you want to skip this warning, you need to set global configuration:
git config --global --add --bool push.autoSetupRemote true
Advanced Git log
Sometimes it will be crucial for us to see history of commit on our branch. We can do that by simply running the command:

git log

Figure 2 - Git log output


Git log can also be displayed in one line using this command:


git log --oneline
This will provide a simplified view of git commits

Figure 3 - git log on one line


Decorating git log output

Output of git log commands can be hard to understand and we need additional info in order to better understand code history. Luckily, "git log" can accept additional parameters which can decorate output and provide more info.

git log --stat
"--stat" decorator will display the number of insertions and deletions for each file changed by each commit.

Figure 4 - git log --stat output

git log -p
"-p" decorator will show actual changes in our files by each commit. Here is an example:

Figure 5 - git log -p output


We can also combine decorators in order to get proper output, so you can run git log like this:
git log --stat -p
Visualize git log
The output of the git log can be visualized using ASCII, so we can have graphical representation of our branches. Just simply run this command:

git log --graph --oneline --decorate

Figure 6 - git log graphical representation


Search git repository

You are all familiar with Linux grep command (at least I hope se :-) ), but I can bet that you didn't know that you can use grep to search git repository. Here is how. Just run:

git grep "string you are searching for"

Figure 7 - search git repository


Inspect git changes

If you want to see what change you made comparing to the remote file, just run:

git diff
You will see what changes you made to a specific file.

Figure 8 - git diff output


Conclusion
If you take a look at https://git-scm.com/docs/git you will see that there are many useful git commands which can make your life easier. Using IDE or web tools for git is quick win, but mastering terminal commands can sometimes be really useful, especially in a place where there is no graphical environment.

 
 
 

Comments


Ebisoft
bottom of page