Git Commands


Git is a widely used version control system for tracking changes in source code during software development.

Some common used Git commands to help you manage your Git repositories:

  1. Initializing a Repository:

    • git init: Create a new Git repository in the current directory.
  2. Cloning Repositories:

    • git clone <repository-url>: Clone a remote Git repository to your local machine.
  3. Basic Workflow:

    • git status: Check the status of your working directory.
    • git add <file>: Stage changes for commit.
    • git commit -m "Commit message": Commit staged changes with a descriptive message.
    • git pull: Fetch and merge changes from a remote repository.
    • git push: Push your local commits to a remote repository.
  4. Branching and Merging:

    • git branch: List all branches in your repository.
    • git branch <branch-name>: Create a new branch.
    • git checkout <branch-name>: Switch to a different branch.
    • git merge <branch-name>: Merge changes from one branch into the current branch.
    • git branch -d <branch-name>: Delete a branch after merging.
  5. Viewing Commit History:

    • git log: View a detailed history of commits.
    • git log --oneline: View a simplified commit history.
    • git log <file>: View commit history for a specific file.
  6. Undoing Changes:

    • git reset <file>: Unstage changes for a file.
    • git checkout -- <file>: Discard changes in a file (use with caution).
    • git revert <commit>: Create a new commit that undoes the changes introduced by a previous commit.
  7. Remote Repositories:

    • git remote -v: List remote repositories.
    • git remote add <name> <url>: Add a new remote repository.
    • git remote remove <name>: Remove a remote repository.
    • git fetch <remote>: Fetch changes from a remote repository.
    • git pull <remote> <branch>: Pull changes from a specific remote branch.
    • git push <remote> <branch>: Push changes to a specific remote branch.
  8. Tags:

    • git tag: List all tags in the repository.
    • git tag <tag-name>: Create a new tag.
    • git tag -d <tag-name>: Delete a tag.
    • git push --tags: Push tags to a remote repository.
  9. Stashing Changes:

    • git stash: Temporarily save changes that are not ready to be committed.
    • git stash list: List all stashes.
    • git stash apply: Apply the most recent stash.
    • git stash pop: Apply and remove the most recent stash.
    • git stash drop: Remove the most recent stash.
  10. Configuring Git:

    • git config --global user.name "Your Name": Set your username globally.
    • git config --global user.email "your@email.com": Set your email globally.
    • git config --global core.editor "editor": Set your preferred text editor.
    • git config --list: List all Git configuration settings.

These are some of the most commonly used Git commands. Git provides a powerful set of features for version control, so it's a good idea to explore the documentation and tutorials to fully utilize its capabilities.

Here are some useful Git commands:

General purpose commands:

  • git init: Initializes a new Git repository in the current directory.
  • git clone <remote repository URL>: Creates a local clone of a remote repository.
  • git status: Displays the current state of the working directory and the staging area.
  • git add <file or directory>: Adds the specified file or directory to the staging area.
  • git commit -m "<commit message>": Creates a new commit with the specified message.
  • git push <remote repository name> <branch name>: Pushes the specified branch to the remote repository.
  • git pull <remote repository name> <branch name>: Fetches and merges the specified branch from the remote repository.

Branching and merging commands:

  • git branch <branch name>: Creates a new branch.
  • git checkout <branch name>: Switches to the specified branch.
  • git merge <branch name>: Merges the specified branch into the current branch.
  • git rebase <branch name>: Rebases the current branch onto the specified branch.

Undoing changes commands:

  • git reset --hard <commit hash>: Resets the working directory and the staging area to the specified commit.
  • git checkout <file or directory>: Reverts the changes to the specified file or directory.
  • git revert <commit hash>: Creates a new commit that undoes the changes made in the specified commit.

Other useful commands:

  • git diff: Shows the difference between the working directory and the staging area, or between two commits.
  • git log: Shows the commit history of the repository.
  • git tag <tag name> <commit hash>: Creates a tag on the specified commit.
  • git stash: Stashes the current changes in the working directory.
  • git stash pop: Unstashes the last stashed changes.

These are just a few of the many useful Git commands. For more information, please see the Git documentation:

https://git-scm.com/doc

Here are some examples of how to use Git commands:

  • To initialize a new Git repository:
git init
  • To clone a remote repository:
git clone https://github.com/bard/bard.git
  • To add a file to the staging area:
git add README.md
  • To commit the changes in the staging area:
git commit -m "Added README.md"
  • To push the changes to the remote repository:
git push origin master
  • To create a new branch:
git branch feature-1
  • To switch to the new branch:
git checkout feature-1
  • To merge the new branch into the master branch:
git checkout master
git merge feature-1
  • To undo the changes made in the last commit:
git reset --hard HEAD~1
  • To stash the current changes in the working directory:
git stash
  • To unstash the last stashed changes:
git stash pop

Git is a powerful tool for version control and collaboration. By learning the basic Git commands, you can start to use Git to manage your code more effectively.

 

Git Commands


Enroll Now

  • Python Programming
  • Machine Learning