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:
Initializing a Repository:
git init
: Create a new Git repository in the current directory.Cloning Repositories:
git clone <repository-url>
: Clone a remote Git repository to your local machine.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.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.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.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.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.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.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.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:
Branching and merging commands:
Undoing changes commands:
Other useful commands:
These are just a few of the many useful Git commands. For more information, please see the Git documentation:
Here are some examples of how to use Git commands:
git init
git clone https://github.com/bard/bard.git
git add README.md
git commit -m "Added README.md"
git push origin master
git branch feature-1
git checkout feature-1
master
branch:
git checkout master
git merge feature-1
git reset --hard HEAD~1
git stash
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.