Blog / GIT Basic Commands - Beginner's Guide

GIT Basic Commands - Beginner's Guide

by SW Team

GIT is one of the most widely used tools in the development world, allowing programmers to manage code and collaborate efficiently. Here's a list of basic GIT commands to help you get started on your version control journey.

What is GIT?

GIT is a distributed version control system, which allows you to track changes to files, facilitating collaboration between multiple programmers.

Projects in GIT have three main sections:

  • Working directory: where changes to files are made.
  • Staging area: This is where files to be included in the next commit are staged.
  • GIT directory (Repository): Where the commit history and committed changes are stored.

Here are some of the most common commands that will help you manage your GIT repository.

Basic GIT commands

  • git init: Create a new GIT repository in the current directory or in a new project.

    git init
    git init [project-name]
    
  • git clone: Copy an existing repository, either remote or local, to your local machine.

    git clone https://github.com/user/repo.git
    git clone user@server:/path/to/repository
    
  • git add: Add files to the staging area.

    git add <archivo.txt>
    git add .
    
  • git commit:Saves the prepared changes to the repository with a descriptive message.

    git commit -m "Description of changes made"
    
  • git status: Displays the status of the repository, indicating which files have been modified, added or are ready for commit.

    git status
    
  • git push: Send your local commits to the remote repository.

    git push origin main
    
  • git pull: Download and merge changes from the remote repository to the local repository.

    git pull
    
  • git branch: Displays the current branches, creates a new branch or deletes an existing branch.

    git branch
    git branch name-branch
    git branch -d name-branch
    
  • git checkout: Switch between branches or create a new branch and switch to it.

    git checkout name-branch
    git checkout -b new-branch
    
  • git merge: Merges one branch with another. This is typically used after finishing development on a feature branch.

    git merge name-branch
    
  • git log: Displays the commit history of the repository.

    git log
    
  • git reset: Undoes local changes and resets the staging area to the last commit.

    git reset --hard HEAD
    
  • git rm: Removes files from the preparation area and the working directory.

    git rm file.txt
    
  • git stash: Temporarily save unconfirmed changes and clear the work area so that you can switch branches or perform other tasks without losing your current work.

    git stash
    
  • git fetch: Download changes from the remote repository without merging them immediately.

    git fetch origin
    

Conclusion

Learning the basic GIT commands will allow you to effectively manage your code projects and collaborate with other developers. With this command guide, you'll be ready to start working in any GIT repository - practice each command and improve your version control skills!

Remember that GIT has a learning curve, but these commands will serve as a solid foundation for you to delve deeper into its use.

i
Email send icon