fbpx

Basic Git commands

Git commands are essential for managing version control in your projects. Here’s a list of some basic Git commands to get you started:

**1. *Initializing a Repository:*

  • git init
  • Initializes a new Git repository in the current directory.

**2. *Making Changes:*

  • git add <file>
  • Stages changes for commit.
  • git commit -m "Your commit message"
  • Commits staged changes to the repository.

**3. *Checking Status:*

  • git status
  • Shows the status of changes as untracked, modified, or staged.

**4. *Viewing Changes:*

  • git log
  • Displays a log of commits with their messages, authors, and commit IDs.
  • git diff
  • Shows changes between the working directory and the last commit.

**5. *Branching:*

  • git branch
  • Lists all branches in the repository.
  • git branch <branch-name>
  • Creates a new branch.
  • git checkout <branch-name>
  • Switches to a specified branch.
  • git checkout -b <branch-name>
  • Creates and switches to a new branch.

**6. *Merging:*

  • git merge <branch-name>
  • Merges changes from one branch into the current branch.

**7. *Remote Repositories:*

  • git remote add origin <repository-url>
  • Adds a remote repository.
  • git push -u origin <branch-name>
  • Pushes changes to a remote repository.
  • git pull origin <branch-name>
  • Fetches and merges changes from a remote repository.

**8. *Working with Tags:*

  • git tag
  • Lists all tags in the repository.
  • git tag -a <tag-name> -m "Your tag message"
  • Creates an annotated tag.

**9. *Undoing Changes:*

  • git reset <file>
  • Unstages changes for a file.
  • git reset --hard
  • Discards all changes in the working directory.
  • git revert <commit-id>
  • Creates a new commit that undoes changes of a previous commit.

**10. *Inspecting and Cleaning:*

  • git show <commit-id>
  • Shows details of a specific commit.
  • git clean -n
  • Shows untracked files to be deleted.
  • git clean -f
  • Deletes untracked files.

**11. *Help:*

  • git help <command>
  • Displays help information for a specific Git command.

These commands cover some of the basic Git operations. As you become more familiar with Git, you can explore additional commands and advanced features to enhance your version control workflow.