Git is a distributed version control system (DVCS) that tracks file changes and coordinates work on software development projects. It is an essential tool for collaborative software development and helps developers manage code, collaborate with others, and track changes over time.
Git operates on a distributed model, meaning that each developer has a complete copy of the entire project’s history on their local machine. The distributed nature of Git enables developers to work offline, commit changes, and collaborate seamlessly with others. Git also provides a centralized repository, often hosted on platforms like GitHub, GitLab, or Bitbucket, where developers can share their code, collaborate, and review changes.
Here’s a table of basic Git commands:
Command | Description |
---|---|
Getting Started | |
git init | Initialize a new Git repository. |
git clone <repository_url> | Clone an existing repository. |
Basic Commands | |
git status | Check the status of your repository. |
git add <file> | Add changes to the staging area (index). |
git add . | Add all changes to the staging area. |
git commit -m "Your commit message here" | Commit changes with a message. |
git reset <file> | Remove files from the staging area. |
git reset | Unstage all files from the staging area. |
Branching and Merging | |
git branch <branch_name> | Create a new branch. |
git checkout <branch_name> | Switch to a different branch. |
git checkout -b <branch_name> | Create and switch to a new branch in one command. |
git merge <branch_name> | Merge changes from one branch into the current branch. |
git branch -d <branch_name> | Delete a branch (only if it’s merged). |
git branch -D <branch_name> | Force delete a branch (use with caution). |
Remote Repositories | |
git remote add <remote_name> <remote_url> | Add a remote repository. |
git remote -v | List remote repositories. |
git fetch <remote_name> | Fetch changes from a remote repository. |
git pull <remote_name> <branch_name> | Pull changes from a remote repository into the current branch. |
git push <remote_name> <branch_name> | Push changes to a remote repository. |
git push -u <remote_name> <branch_name> | Push changes to a remote repository and set the upstream branch. |
Conflict Resolution | |
git status | Check for conflicts after a merge or pull. |
Manually resolve conflicts in affected files | |
Add resolved files to the staging area | |
Commit the resolved changes | |
Log and History | |
git log | View commit history. |
git show <commit_hash> | View a specific commit. |
git log --oneline | Show a concise log with one-line commit messages. |
git diff <commit1> <commit2> | Show the difference between two commits. |
git diff | Show the difference between working directory and last commit. |
Stashing Changes | |
git stash save "Your stash message" | Save changes to a temporary stash. |
git stash list | List stashes. |
git stash apply | Apply the latest stash. |
git stash apply stash@{n} | Apply a specific stash. |
git stash drop stash@{n} | Delete a stash. |
Other Useful Commands | |
git mv <old_file_name> <new_file_name> | Rename a file. |
git rm --cached <file> | Remove a file from Git (but keep it in the local directory). |
git config --list | Show Git configuration. |
git config --global alias.<alias_name> <git_command> | Create an alias for a Git command. |
git restore <file> | Discard changes in the working directory. |
No comments! Be the first commenter?