DEV Community

Cover image for Understanding Git Commands: The Guide To Version Control
Charles Eugene
Charles Eugene

Posted on

Understanding Git Commands: The Guide To Version Control

Essential Git Commands for Software Development

Image description

So you want to be a software engineer? Git is one of the first tools you'll need to learn and master. Git is the back bone of version control allowing colleagues and individuals to manage code changes seamlessly. Let's explore Git's essential commands and how they help track, share, and maintain their work!

So what is Git and WHY should you use it?

Image description

Ahh Git! Git is a great control tool that helps developers manage project files and it keeps a history of all the changes you're making. It doesn't matter whether you're working by yourself or with a colleague. Git makes code management super smooth lets learn the tools needed to do so!

First steps

Lets get started: Setting up Git

1. Tell Git Who You Are:

  • git config --global user.name (Your Name): This assigns your name to all future commits.

Image description

Verify your settings:

  • git config --list: This is to verify your Git settings.

Starting a Repository

1. Create a new Git repository:

  • git init: To initialize a new Git Repository in your project directory, this tells Git to start tracking changes.

CLONING :O
Image description

  • git clone (Repository Url): This will make a local copy of a remote repository.

Making and Tracking Changes

1. Stage Changes:

  • git status: Displays the state of your working directory

Image description

Adding and Saving Changes

  • git add (file): This stage changes to a specific file.

  • git add .: Is the stage all changes in the directory.

Image description

Your Commit Changes:

  • git commit -m "Your Message": This commit is staged changes with a message.

  • git commit --amend: Use this if you need to change or modify the message or to include missing files.

Image description

Image description

Commit History

Git helps you keep an exact record of every change you made to your project!

  • git log: To view the commit history.

Image description

Branching and Merging

  • git branch: List all branches or if you need to create a new branch.

Image description

Switch Between Branches:

  • git checkout (branch name): This is to switch to a branch.

  • git checkout -b (branch name): This is to create and switch to a new branch in ONE step!

Merge branches:

  • git merge (branch name): This merges another branch into your current one.

Clean up old branches:

  • git branch -d (branch name): This will delete the entire branch.

Teamwork Remote Repositories

Image description

1. Link To A Remote Repository

  • git remote add (person) (url): This will link your local repo to a remote repo [This is my current link to a colleague to see who you're connected to use git remote -v]

Image description

2. Share your work:

  • git push origin (branch name): This will upload the changes you made to a remote branch.

Image description

3. Lets Bring In The Updates

  • git pull: This will merge changes from a colleague or any remote branch you're connected to.

Image description

4. Fetch With No Merging:

  • git fetch: You can get updates from a colleague or any remote branch you're connected to without merging.

Image description

Did you mess up?

Lets undo changes!

1. Unstage changes:

Mistakes do happen but Git's undo commands will save the day!

Image description

git reset (file): This removes a file from the staging area WITHOUT deleting its changes.

2. Discard Changes Completely:
git reset --hard (commit): This rolls back to a previous commit and discarding all changes since then.

Undo a Specific Commit:
git revert (commit): Makes a new commit that undoes changes from a particular commit

Image description

Tagging and Tracking Milestones

Image description

Tags are important moments in your project!

1. Add a Simple Tag:

git tag(tag name): Tags the current commit.

2. Push Tags To Remote Repository

git push origin (tag name): Makes your tag accessible to a colleague.

Advanced Tools for Complex Workflows

Special scenarios Git can help with

1. Streamline Commit Histories:

git rebase (branch name): Moves your commits to a different base branch while keeping the same changes.

Image description

2. Pick specific commits:

git cherry-pick (commit): Makes changes from a specific commit onto your current branch.

3. Track All Repository Actions:

git reflog: Shows your history of all actions performed in your repository with resets.

Image description

Thats all Folks!

Image description

All the git commands listed above will help you navigate your role as a developer. You'll gain more control over your code and work flow whether its the simple commands or the more advanced ones! Use it to your benefit lets work smarter not harder!

Sources
https://cloudstudio.com.au/2021/06/26/git-command/

https://parade.com/947443/parade/best-friend-quotes/

https://www.cert-india.com/belbin-stack/leadership-and-teams

https://www.facebook.com/thebasics14/?locale=is_IS

https://education.github.com/git-cheat-sheet-education.pdf

https://www.atlassian.com/git

https://www.michiganstateuniversityonline.com/resources/leadership/how-to-build-a-culture-of-teamwork/

https://www.nytimes.com/2009/01/01/garden/01clones.html

https://www.archive360.com/blog/when-is-it-ok-to-delete-data

https://www.youtube.com/watch?v=3Qz1GMpOtUY

https://depositphotos.com/photos/reset-button.html

https://godmetea.wordpress.com/2016/01/31/pencils-pens-and-undo-buttons/

Top comments (0)