DEV Community

Cover image for Git Basics: Branching, Viewing History, Commands, Best Practices, and Tips
Bellamer
Bellamer

Posted on

Git Basics: Branching, Viewing History, Commands, Best Practices, and Tips

Git is an essential tool for modern software development, enabling teams and individuals to efficiently manage code versions, collaborate on projects, and track changes over time. This post covers Git's fundamentals, including branching, viewing history, essential commands, best practices, and a few useful tips to streamline your workflow.


Table of Contents

  1. What is Git?
  2. Setting up Git
  3. Basic Git Commands
  4. Branching in Git
  5. Viewing Git History
  6. Best Practices
  7. Tips & Tricks
  8. Conclusion

What is Git?

Git is a distributed version control system that tracks changes in files, making it easier to manage projects over time. It allows developers to collaborate by maintaining a history of file changes, providing mechanisms for branching, merging, and versioning. Git is widely used in both open-source and commercial projects.


Setting up Git

To start using Git, follow these steps:

1.Install Git

Download Git from the official Git website. Installation instructions are available for Windows, macOS, and Linux.

2.Configure Git

Once installed, configure your identity:

   git config --global user.name "Your Name"
   git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

3.Check configuration

Verify your setup with:

   git config --list
Enter fullscreen mode Exit fullscreen mode

Basic Git Commands

Here are some of the most commonly used Git commands to get started:

1.Initialize a Git repository

Create a new Git repository in your project:

   git init
Enter fullscreen mode Exit fullscreen mode

2.Clone a repository

Copy an existing Git repository to your local machine:

   git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

3.Add files to the staging area

Stage changes for commit:

   git add <file_name>     # Add a specific file
   git add .               # Add all modified files
Enter fullscreen mode Exit fullscreen mode

4.Commit changes

Record staged changes to the repository:

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

5.Push changes to a remote repository

Send your commits to the remote repository:

   git push origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

6.Pull changes from a remote repository

Fetch and merge changes from the remote repository:

   git pull origin <branch_name>
Enter fullscreen mode Exit fullscreen mode

7.Check the status of your repository

View the current state of your working directory and staged files:

   git status
Enter fullscreen mode Exit fullscreen mode

Branching in Git

Branches in Git are independent lines of development, allowing teams to work on new features or fixes without impacting the main codebase. When you're ready, you can merge branches back into the main branch.

1.Create a new branch

To create and switch to a new branch:

   git checkout -b <branch_name>
Enter fullscreen mode Exit fullscreen mode

2.Switch between branches

To switch to an existing branch:

   git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

3.List all branches

To list all the branches in the repository:

   git branch
Enter fullscreen mode Exit fullscreen mode

4.Merge a branch

Merge changes from one branch into another (usually main or master):

   git checkout main
   git merge <branch_name>
Enter fullscreen mode Exit fullscreen mode

5.Delete a branch

After merging, you can delete the old branch:

   git branch -d <branch_name>
Enter fullscreen mode Exit fullscreen mode

Viewing Git History

Git provides several commands to view the history of changes, allowing you to track when and how the code was modified:

1.View commit history

Display a list of commits in your repository:

   git log
Enter fullscreen mode Exit fullscreen mode

You can also use the --oneline flag to see a more concise history:

   git log --oneline
Enter fullscreen mode Exit fullscreen mode

2.View differences between commits

To see what has changed between two commits or between the working directory and a commit:

   git diff <commit1> <commit2>
Enter fullscreen mode Exit fullscreen mode

3.Show the history of a specific file

To see the history of a particular file:

   git log -- <file_name>
Enter fullscreen mode Exit fullscreen mode

4.View a specific commit

To display the details of a specific commit, use the commit hash:

   git show <commit_hash>
Enter fullscreen mode Exit fullscreen mode

Best Practices

1.Commit Often and Early

Frequent commits with clear, concise messages make it easier to track changes and identify bugs.

2.Use Meaningful Commit Messages

Write descriptive messages to explain why a change was made, not just what was changed. This helps in future code reviews and debugging.

3.Work in Feature Branches

Always create a new branch when working on a feature or bug fix. This isolates your changes and makes it easier to manage your work.

4.Rebase Before Merging

To keep a clean history, consider using git rebase to update your branch with changes from the main branch before merging.

   git checkout <branch_name>
   git rebase main
Enter fullscreen mode Exit fullscreen mode

5.Avoid Committing Unnecessary Files

Use a .gitignore file to exclude files that shouldnโ€™t be committed (e.g., configuration files, temporary build files).


Tips & Tricks

1.Undo the Last Commit

If you made a mistake in the last commit but havenโ€™t pushed yet, you can undo it while keeping the changes in your working directory:

   git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

2.Stash Changes

Stash your current changes without committing them to work on something else:

   git stash
Enter fullscreen mode Exit fullscreen mode

Later, you can reapply the stashed changes:

   git stash apply
Enter fullscreen mode Exit fullscreen mode

3.View a Visual Representation of History

Use git log --graph --oneline --all to see a visual representation of the commit history, including branches and merges.

4.Use Aliases for Common Commands

You can set up Git aliases to simplify long or frequent commands:

   git config --global alias.co checkout
   git config --global alias.br branch
   git config --global alias.st status
   git config --global alias.hist "log --oneline --graph --all"
Enter fullscreen mode Exit fullscreen mode

5.Interactive Rebase

Clean up your commit history by squashing unnecessary commits before merging with git rebase -i (interactive rebase).

   git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

6.Track Upstream Branches

Easily push and pull from the correct upstream branch by setting it with:

   git branch --set-upstream-to=origin/<branch_name>
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering Git commands and workflows is crucial for efficient project management and collaboration. By following best practices, such as working in feature branches, committing frequently, and writing meaningful commit messages, you'll maintain a clean and effective codebase. Explore Gitโ€™s more advanced features like interactive rebase, stashing, and log visualization to boost your productivity.

Happy coding!


Additional Resources:

Top comments (3)

Collapse
 
kubernetic profile image
BC

I prefer the command git switch <branch name> to switch branches.

Collapse
 
sandyabasman profile image
Sunday Abasman

Thank you

Collapse
 
bobbyiliev profile image
Bobby Iliev • Edited

Great post! Well done!

For anyone who wants to learn more, you can check out this free ebook here:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

๐Ÿ’ก Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

๐Ÿš€ Download

To download a copy of the eBook use one of the following links:

๐Ÿ“˜ Chapters