Command-line Git (Git & Dev Tools)
Learn Command-line Git (Git & Dev Tools) step by step with clear examples and exercises.
Title: Mastering Command-line Git for Developers: A full guide to Version Control and Dev Tools
Why This Matters
In the dynamic world of software development, version control is an essential skill that helps manage code changes effectively. Git, a powerful distributed version control system, has become the industry standard due to its robustness, flexibility, and ease of use. Command-line Git offers developers a versatile toolset for handling complex projects efficiently, making it crucial to master this skill.
In interviews, recruiters often ask about your familiarity with Git and command-line tools. Understanding Git's command-line interface can help you tackle real-world bugs, collaborate with other developers, and showcase your problem-solving abilities during the interview process.
Prerequisites
Before diving into Git, it is essential to have a basic understanding of:
- Operating System (Linux/MacOS/Windows)
- Familiarity with file navigation commands like
cd,ls, andpwd - Text editors such as vi, nano, or vim
- Basic shell scripting concepts including variables, loops, functions
- Understanding of directories and files
- File Navigation: Learn to navigate your file system using commands like
cd,ls, andpwd. These commands will help you move around your project directory structure easily.
- Text Editors: Familiarize yourself with text editors such as vi, nano, or vim. These editors are commonly used for editing files directly from the command line.
- Basic Shell Scripting: Understanding shell scripting concepts like variables, loops, and functions will help you automate repetitive tasks and write custom scripts to streamline your workflow.
- Understanding of Directories and Files: A solid grasp of directories (folders) and files is crucial when working with Git. You should be able to create, delete, rename, and move files and directories using command-line tools.
Core Concept
What is Git?
Git is a distributed version control system designed to track changes in source code during software development. It allows multiple developers to work on the same project simultaneously without overwriting each other's changes.
Key Features:
- Distributed: Each developer has a full copy of the repository, making it easier to collaborate and work offline.
- Versioning: Git keeps track of every change made to the codebase, allowing developers to revert to previous versions if needed.
- Branching: Git allows for multiple branches, enabling developers to work on different features or fixes independently without affecting the main project.
Installing Git
To install Git on your system, follow these steps:
- Linux/MacOS: Use package managers like
apt(Debian-based distributions),yum(CentOS/RHEL), or Homebrew (MacOS) to install Git.
Debian-based distros
sudo apt-get install git
CentOS/RHEL
sudo yum install git
MacOS with Homebrew
brew install git
2. **Windows:** Download Git for Windows from the official website (https://git-scm.com/download/win) and follow the installation wizard's instructions.
### First-Time Git Setup
After installing Git, you need to configure your user settings:
Set your name and email
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
### Getting Help
Use the `git help` command to get more information about Git commands:
Get help on a specific command, e.g., commit
git help commit
Worked Example
In this example, we will create a simple project, make changes, and commit them using Git.
- Create a new directory for your project:
mkdir my-project && cd my-project
- Initialize the Git repository:
git init
- Create a file called
main.cwith some content, e.g., a simple "Hello, World!" program:
echo "#include <stdio.h>\n\nint main() {\n printf(\"Hello, World!\\n\");\n return 0;\n}" > main.c
- Add the file to the Git repository:
git add main.c
- Commit the changes with a message:
git commit -m "Initial commit"
- Make some changes to
main.c, e.g., modify the program to print your name instead of "Hello, World!":
nano main.c
- Add and commit the updated file:
git add main.c
git commit -m "Updated main.c"
- View the commit history using
git logcommand:
git log
- To view more detailed information about a specific commit, use the
git showcommand followed by the commit hash:
git show <commit-hash>
- To create a new branch called
my-branch, use the following command:
git checkout -b my-branch
Now, you can make changes in this new branch without affecting the main branch. When you're ready to merge your changes back into the main branch, use the git merge my-branch command.
Creating and Switching Branches
You can create a new branch using git checkout -b . To switch between branches, use git checkout .
Staging Changes
When you make changes to files in your Git repository, those changes are not automatically staged for commit. You must explicitly stage the changes using git add before committing them:
Stage all changes
git add .
Stage specific file
git add
### Committing Changes
After staging your changes, you can commit them with a message using the `git commit` command:
git commit -m "Your commit message"
Common Mistakes
- Forgetting to add files before committing: Always use
git addbefore committing changes to ensure they are included in the repository.
- Committing unfinished work: Never commit half-done or untested code. It's better to create a temporary commit, test it, and then amend or squash the commit if necessary.
- Ignoring merge conflicts: When merging branches, Git may encounter conflicts that require manual resolution. Ignoring these can lead to lost changes or inconsistencies in the codebase.
Merge Conflicts Resolution
When resolving merge conflicts, Git will highlight the conflicting lines and allow you to choose which version of the code to keep. You can use a text editor like vim or nano to resolve these conflicts manually. Save the file after resolving the conflict, then stage and commit the changes using Git commands.
Practice Questions
- How do you create a new branch in Git?
- What command is used to view the commit history?
- How would you undo the last commit?
- What happens if you forget to add a file before committing changes?
- Explain how to resolve merge conflicts in Git.
- Describe the difference between
git statusandgit diff. - What is a git rebase, and when would you use it?
- How do you view the differences between two specific branches or commits in Git?
- What is a git stash, and why might you need to use it?
- Explain how to create and manage git tags for versioning purposes.
- What does the
git mergecommand do, and when would you use it? - How can you view the differences between two specific versions of a file in Git?
- What is the purpose of the
.gitignorefile, and how can you create one for your project? - Describe the difference between hard links and symbolic links in Git.
- What is a git cherry-pick, and when would you use it?
FAQ
- What is the difference between Git and SVN (Subversion)?
- Git is a distributed version control system, while SVN is a centralized one. Git allows for local commits before pushing changes to a remote repository, making it more flexible and efficient.
- How do I create a GitHub account?
- Go to https://github.com/ and click "Sign up" to create a new account using your email address or social media accounts.
- What is the purpose of
git pull?
git pullfetches updates from the remote repository and merges them into your local branch, updating your codebase with the latest changes.
- How do I revert to a specific commit in Git?
- Use the
git resetcommand followed by the commit hash to revert to that specific commit:
git reset --hard <commit-hash>
- What is the difference between
git checkoutandgit switch?
- In older versions of Git, the command was called
git checkout. As of Git 2.23, it has been replaced withgit switch, which offers a more intuitive and user-friendly syntax for switching branches.