Skip to content

Daily used Git commands.

Published: at 08:44 AM (2 min read)

Let’s consider an example where you have a project named “my-project” and you want to use Git for version control. Here’s how you can use Git commands in your daily workflow:

  1. Initializing a Git Repository:

    cd my-project
    git init
    
  2. Adding and Committing Changes:

    git add <file1> <file2>  # Add specific files
    git add .               # Add all files
    git status              # Check the status of the repository
    git commit -m "Initial commit"
    
  3. Cloning a Remote Repository:

    git clone <repository-url>
    
  4. Pulling and Pushing Changes:

    git pull origin <branch>  # Fetch and merge remote changes
    git push origin <branch>  # Push local changes to remote repository
    
  5. Creating and Switching Branches:

    git branch                   # List all branches
    git branch <new-branch>      # Create a new branch
    git checkout <branch>        # Switch to a different branch
    
  6. Merging Branches:

    git checkout <target-branch>  # Switch to the target branch
    git merge <source-branch>     # Merge the source branch into the target branch
    
  7. Viewing Commit History:

    git log                # Show commit history
    git log --oneline      # Show condensed commit history
    
  8. Checking Differences:

    git diff               # Show differences between working directory and staging area
    git diff --staged      # Show differences between staging area and last commit
    
  9. Adding Remote Repositories:

    git remote add <name> <url>    # Add a remote repository
    git remote -v                  # List all remote repositories
    
  10. Discarding Changes:

    git checkout -- <file>    # Discard changes in a specific file
    git reset --hard          # Discard all local changes and reset to last commit
    

These commands should give you a good starting point for using Git in your project. Remember to replace <file>, <branch>, <repository-url>, and <new-branch> with the appropriate values specific to your project.