Git Usage Guide: Basic Commands and Tips

Fatih Delice
Fatih Delice

Git is a popular distributed version control system used to track and manage code changes during software development. In this article, we will cover the basics of Git usage and its most important commands.

1. Git Installation and Configuration

After installing Git on your system, you can configure your user information with the following commands:

# Set your username
git config --global user.name "Your Full Name"
 
# Set your email address
git config --global user.email "email@example.com"
 
# Enable colored output for the command line
git config --global color.ui auto

2. Starting a Git Repository

To manage an existing project with Git, you can initialize a Git repository using the following command:

git init

This command creates a .git folder in the root directory of your project and turns the project into a Git repository.

3. Adding Files and Saving Changes

You can follow these steps to add changes from your working directory to Git:

  1. Check the file status:
    git status
  2. Add the file:
    git add file_name
    To add all changes, you can use the git add . command.
  3. Save the changes with a commit:
    git commit -m "A descriptive message"

4. Using Branches

You can create branches in Git to develop different features or fix bugs:

# List existing branches
git branch
 
# Create a new branch
git branch new_branch_name
 
# Switch branches
git checkout new_branch_name
 
# Create and switch to a new branch
git checkout -b new_branch_name

5. Merging Changes

To merge changes from another branch into the current branch, you can use the following commands:

git checkout main_branch
 
git merge new_branch_name

6. Working with Remote Repositories

Git lets you store your projects in remote repositories such as GitHub or GitLab. You can follow these steps to connect a repository and upload code:

  1. Add a remote repository:
    git remote add origin <repository_url>
  2. Push changes to the remote repository:
    git push -u origin main_branch
  3. Pull changes from the remote repository:
    git pull origin main_branch

7. Undoing Changes

You can use the following commands to undo accidental changes:

# Removes changes from the staging area without discarding them
git reset file_name
 
# Undoes the last commit while keeping the changes
git reset --soft HEAD~1
 
# Discards all changes and returns to the previous commit
git reset --hard HEAD~1

8. Using Git Stash

You can use the git stash command to save some changes temporarily:

git stash          # Saves changes temporarily
git stash list     # Lists stashed changes
git stash pop      # Restores the latest stashed changes
git stash drop     # Deletes stashed changes

9. Ignoring Files and Directories (.gitignore)

If you do not want Git to track certain files and directories, you can create a .gitignore file:

echo "node_modules/" >> .gitignore
git add .gitignore
git commit -m "Add Git ignore file"

Conclusion

In this guide, we covered the basic usage of Git and its important commands. Git is a powerful tool that makes collaboration easier in software projects. For more information, you can explore the official Git website or GitHub Docs.