Git command examples

Git is crucial for version control, enabling teams to collaborate, track changes, and manage code reliably in software development projects

Here are useful git command examples! 😎👇

#coding #programming #softwaredeveloper #github

Find high-res pdf books with all my DevOps related infographics at https://study-notes.org

2025/8/12 Edited to

... Read moreHey everyone! As someone who’s spent a lot of time wrestling with version control, I know how crucial Git is. I often wished I had a straightforward list of commands when I was starting out, and even now, a quick reference is invaluable. So, I wanted to share some of the essential Git commands that have really made a difference in my workflow. Think of this as your personal cheat sheet for navigating most common scenarios! Let's kick things off with basic operations. These are the commands you'll use almost daily. To start a new repository, git init is your best friend. If you're joining an existing project, git clone [repository-url] will get you set up in no time. Checking the status of your working directory with git status is a habit I’ve developed – it tells you exactly what’s going on. Next up, staging and committing changes. This is the heart of Git. After making edits, you use git add [file-name] or git add . to stage your changes. This tells Git, "Hey, I want to include these modifications in my next snapshot." Once staged, git commit -m "Your descriptive message" saves those changes permanently to your repository's history. I always try to write clear, concise commit messages; it makes looking back so much easier! Viewing changes is another area where Git shines. To see the differences between your working directory and the staging area, git diff is incredibly useful. If you want to see the changes between your staging area and the last commit, use git diff --staged. And of course, git log is indispensable for browsing the commit history. I often use git log --oneline --graph --all for a more visual and condensed view of the branches. When it comes to team collaboration, managing branches is key. git branch [branch-name] creates a new branch, and git checkout [branch-name] switches to it. I highly recommend working on separate branches for new features or bug fixes to keep your main codebase clean. Once your work on a branch is complete, git merge [branch-name] integrates those changes back into another branch (like your main development branch). I've found git branch -d [branch-name] useful for cleaning up merged branches afterwards. Managing remote repositories is how we share our code. git remote add origin [remote-url] links your local repository to a remote one (often GitHub or GitLab). To send your local commits to the remote repository, use git push origin [branch-name]. And to fetch the latest changes from the remote, git pull origin [branch-name] is what you need. I always pull before I start working to avoid merge conflicts later on. Finally, sometimes we make mistakes, and Git is forgiving! Undoing changes is possible. git reset --hard HEAD will discard all local changes and revert to the last commit – use with caution! For specific files, git checkout -- [file-name] can revert changes. If you’ve committed something and want to undo it, git revert [commit-hash] creates a new commit that undoes the changes of a previous one, which is safer for shared history. For more advanced scenarios, git rebase can be used to rewrite commit history, but that's a topic for another day! Lastly, git tag is great for marking significant points in your project history, such as release versions. I hope this expanded list of commands gives you a clearer picture of how powerful Git can be. It’s definitely helped me streamline my development process, and I believe understanding these essentials will do the same for you!