Skip to content

Git Tutorial

Samet Alan edited this page Feb 18, 2024 · 4 revisions

What is Git?

Git is a version control system (the practice of tracking and managing changes to software code) . It is used for

  1. Tracking code changes
  2. Tracking who made changes
  3. Coding collaboration

How to Use Git

Installing Git

Git might already be installed on your Linux/WSL computer. You can check it via the command git --version. If it is not installed yet, you may install via the command sudo apt install git.


Configuring Git

In order to configure git, use the commands git config --global user.name "alice-spring" and git config --global user.email "[email protected]" for the name and e-mail, respectively.


Initializing Git

In the directory that you want to use the Git source control, use the git init command, and Git will be initialized.


Status Checking in Git

Imagine that in a project, you have written a file with a code called hello.py and you are sure that this file is in the directory that you have already initialized Git. In such cases, use git status to see whether a file is a part of the repository. See Git does not add the file git status, but it is aware of that file in the directory.


Adding Files into the Staging Environment

To add a file into the Staging Environment (for ready-to-commit files) of Git, one has to type git add hello.py. In this case, hello.py is added to the Staging Environment, which you should add the files to it when you hit a milestone or finish a part of the work. Instead of adding each file one-by-one, git add --all would add all individual filenames, and will stage all changes (new, modified, and deleted files).


Committing in Git

Probably "committing" is the most popular term in the Git universe. Git considers each commit change point or "save point", By adding clear messages to each commit, it is easy for yourself (and others) to see what has changed and when. The commit command performs a commit, and the -m "message" adds a message. For instance, git commit -m "First release of hello.py" is a valid Git commit. To make commits later, type a command like git commit -a -m "Updated hello.py with a new line", where the -a option will automatically stage every changed, already tracked file.


Commit Logs in Git

To view the history of commits for a repository, you can use the log command (git log).


Troubleshooting

Any time you need some help remembering the specific option for a command, you can use git _commandname_ -help (In the case of commit command, type git commit -help). To list all possible commands, use the git help --all command, but keep in mind that this will display a very long list of commands.


Branching in Git

Branching in Git is also made by its specific command. Suppose one wishes to create a new branch called "hello-world-images". He/she has to type git branch hello-world-images. Moving among branches is performed via git checkout hello-world-images

Final Notes

The file names such as hello.py, and the name-surname-emails are given as an example and does not reflect any real files, people, or e-mail address. For further questions or explanations, feel free to contact!

Clone this wiki locally