Git Commands for Software Engineers

Bitlearners - Jul 10 - - Dev Community

A key function of Git is to manage version control and collaborate on software development, making it a must-have tool for software engineers. Whether you're a beginner or an experienced developer, mastering Git commands will help you manage codebases, track changes, and contribute to projects seamlessly. You will learn the fundamental Git commands that are at the core of version control workflow in this introduction.

What is Git?

A distributed version control system, Git, is designed to handle both small and large projects quickly and efficiently. This allows multiple developers to work on the same project without overwriting each other's changes, ensuring code integration that is cohesive and conflict-free.

Why Use Git?

Version Control: Using Git, you can track every change made to your codebase and revert to previous versions if necessary.

Collaboration: When multiple developers are working on a project simultaneously, their changes are seamlessly merged.

Branching and Merging: GitHub's branching model enables developers to work independently before merging new features or bug fixes.

Distributed System: Developers have their own copies of the entire project history, which allows for faster operations and backups.

Setting Up Git

Before you can start using Git, you need to set it up on your local machine.
1. Install Git

# For Debian-based distributions like Ubuntu
sudo apt-get install git

# For Red Hat-based distributions like Fedora
sudo dnf install git

# For macOS
brew install git

# For Windows, download and install from https://git-scm.com/

Enter fullscreen mode Exit fullscreen mode

2. Configure Git
The username and email you specify when configuring Git are associated with your commit messages. Using the --global flag, you can set these configurations for all repositories on your system.

# Set your name
git config --global user.name "Your Name"

# Set your email
git config --global user.email "your.email@example.com"

# Verify your settings
git config --list
Enter fullscreen mode Exit fullscreen mode

Key Git Commands

Basic Git Operations

These are the foundational commands that you will use frequently.
1. Initialize a New Repository
Initializes a new Git repository in the current directory, along with a subdirectory called .git.

git init
Enter fullscreen mode Exit fullscreen mode

2. Clone an Existing Repository
Makes a copy of an existing remote repository on your local machine.

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

3. Check the Status of Your Repository
It shows the current state of the working directory as well as the staging area. Git shows which changes have been staged, which haven't, and which files aren't being tracked.

git status
Enter fullscreen mode Exit fullscreen mode

4. Add Files to the Staging Area
Prepares the changes in the working directory for inclusion in the next commit by adding them to the staging area.

# Add a single file
git add filename

# Add all files
git add .
Enter fullscreen mode Exit fullscreen mode

5. Commit Changes
A message is added to the repository that describes the changes made to the staging area.

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

6. View Commit History
Display Lists all commits in the current branch, starting from the most recent.

git log
Enter fullscreen mode Exit fullscreen mode

Branching and Merging

Branching allows you to create separate environments for development, while merging integrates changes from different branches.

1. Create a New Branch
This command creates a new branch called new-branch.

git branch new-branch
Enter fullscreen mode Exit fullscreen mode

2. Switch to a Branch
Changes the working directory to the branch specified

git checkout new-branch
Enter fullscreen mode Exit fullscreen mode

3. Create and Switch to a New Branch
Switches immediately to the newly created branch.

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

4. Merge a Branch
The changes from new-branch are merged into the main branch. It is typically done after a feature is complete or a fix is made on a separate branch.

git checkout main
git merge new-branch
Enter fullscreen mode Exit fullscreen mode

5. Delete a Branch
This command deletes the specified branch. A branch is usually removed after it has been merged into another branch.

git branch -d new-branch

Enter fullscreen mode Exit fullscreen mode

5. Delete a Branch
This command deletes the specified branch. In most cases, this is done after the branch has been merged into another branch and is no longer needed.

git branch -d new-branch
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

Sometimes you need to undo changes, whether they are in your working directory, staging area, or committed history.

  1. Discard Changes in Working Directory Reverts the file to its last committed state by discarding changes in the working directory.
git checkout -- filename
Enter fullscreen mode Exit fullscreen mode

2. Unstage Changes
Keeps changes in the working directory but removes them from the staging area.

git reset HEAD filename
Enter fullscreen mode Exit fullscreen mode

3. Amend the Last Commit
Commits a new message or additional changes to the most recent commit.

git commit --amend -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

4. Revert a Commit
An undo commit is created by undoing a commit specified in the commit string.

git revert commit-hash
Enter fullscreen mode Exit fullscreen mode

5. Reset to a Previous Commit
This command resets the current branch to the commit specified. If the --soft option is selected, the changes will be kept in the working directory, whereas if the --hard option is selected, they will be discarded.

# Soft reset (keeps changes in working directory)
git reset --soft commit-hash

# Hard reset (discards changes)
git reset --hard commit-hash
Enter fullscreen mode Exit fullscreen mode

Collaboration

Collaborating with others involves working with remote repositories.

1. Add a Remote Repository
The URL of a remote repository is associated with a name, typically the origin.

git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

2. Push Changes to Remote Repository
The local branch commits are uploaded to the corresponding branch in the remote repository.

git push origin branch-name
Enter fullscreen mode Exit fullscreen mode

3. Pull Changes from Remote Repository
Syncs the remote branch with the current branch and fetches and integrates the changes.

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

4. Fetch Changes from Remote Repository
Updates are retrieved without being merged into the local branch from the remote repository.

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Advanced Git Commands

For more complex workflows, these advanced commands can be very useful.

1. Stash Changes
Stacks unfinished changes in the working directory, allowing you to move on to something else without losing your work.

git stash
Enter fullscreen mode Exit fullscreen mode
  1. Apply Stashed Changes Updates the working directory with the most recent changes.
git stash apply
Enter fullscreen mode Exit fullscreen mode

3. View Stashed Changes
All stashes are listed, along with their names and commit messages.

git stash list
Enter fullscreen mode Exit fullscreen mode

4. Cherry-pick a Commit
The current branch is updated with the changes from a specific commit.

git cherry-pick commit-hash
Enter fullscreen mode Exit fullscreen mode

5. Rebase a Branch
Creates a linear project history by reapplying commits from the current branch onto another branch.

git rebase branch-name
Enter fullscreen mode Exit fullscreen mode

Tips and Tricks of git

Enhance your Git experience with these helpful tips.
1. Aliases
Save time and keystrokes by defining shortcuts for commonly used Git commands.

# Create an alias for git status
git config --global alias.st status

# Create an alias for git log with a specific format
git config --global alias.lg "log --oneline --graph --all"
Enter fullscreen mode Exit fullscreen mode

2. Autocorrect
Ensures that mistyped commands are automatically corrected after a short delay after they are entered.

git config --global help.autocorrect 1
Enter fullscreen mode Exit fullscreen mode

3. Colorize Git Output
Provides color-coded output for Git commands to make them easier to read.

git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode

Conclusion

Having a clear understanding and use of these fundamental Git commands will greatly improve your workflow as a software engineer as well as your productivity. As a result of Git's robust version control capabilities, as well as its robust branching and merging features, Git has become one of the most indispensable tools for modern software development. Integrate Git into your projects today in order to take advantage of the benefits of efficient version control and collaboration that Git provides.

. . .