Command Line Git

From basic to advanced, this will help you become a Git Pro

Git is a powerful tool for tracking changes in your code and collaborating with others. Whether you're working solo or on a team, understanding Git is essential. This guide will walk you through everything you need to know, from installation to advanced collaboration.

Git as a command line tool works with many repository hosting services , like GitHub and GitLab. Git is a distributed version control system that works independently of any specific hosting service - it can connect to any server that supports the Git protocol. Here are some of the most popular repos:

1. Installing Git

Before you can use Git, you need to install it on your computer. Here’s how:

# Windows
#Go to https://git-scm.com/ and download the installer.
#Run the installer and follow the prompts.

#macOS
brew install git

#linux  Ubuntu/Debian
sudo apt-get install git

#Linux Fedora
dnf install git

#Once installed verify the install by typing
git --version

Now that Git is installed, let’s learn the basic commands and concepts.

A repository (or "repo") is a folder where Git tracks changes to your files. It contains all the project files and the history of changes. This is where you check your code into.

In order to work on a repo you will need to download it to your computer. To do this we use the clone command followed by the URL to your git repo. Many repos provide a link to this URL. Here is how github.com displays the link. Just copy it to your clipboard and you are ready to clone the repo. NOTE: there is an option to just download the zip but this option will not allow you to make changes. Use git clone instead.

#this will create a new folder called somerepo in your current directory. If the folder already exists it returns an error
git clone https://github.com/somename/somerepo.git

#to clone it into a custom folder name, specify the folder
git clone https://github.com/somename/somerepo.git myfoldername

#if your repo is password protected add user/pass to the url
git clone https://myusername:[email protected]/somename/somerepo.git myfoldername

Once you have the repo on your computer there are multiple commands that are really useful.

#to get any new changes that others may have checked in
git pull

#to add a local file to the repo
get add somefilename.txt

#if you have multiple files to add
git add .

#check the status of your local repo. This will show all changes
git status

#to see changes you have made compared to what was there before
get diff somefilename.txt

#To save your changes use the commit command. Add a message with -m NOTE: Write clear and descriptive commit messages so you (and others) can understand what changed.
git commit -m "some clear message" somefilename.txt

#or commit all your changes add -a (a = all, m = message)
git commit -am "some clear message"

#Once you have committed your changes you need push them to the repo
git push

#Use log to see a list of previous commits (with pagination)
git log
git log --oneline

#show the last 100 commits without pagination
git --no-pager log --oneline -n 100

Branching - So far we have taught you how to check out and check in changes the the main code repo. This is sometimes called the trunk, or master, or main. If multiple people are making changes to the same code trunk then it becomes necessary to use a concept called branches. Imagine you're writing a book. A branch in Git is like creating a separate notebook where you can write and experiment without messing up your original manuscript. When you are ready you merge your notebook back into the main manuscript. Creating a branch is like creating a copy of your current project that you can modify freely:

# Create a new branch
git branch somebranchname

# Switch to the new branch
git checkout somebranchname

# Shortcut to create and switch in one command
git checkout -b somebranchname

Now you can make changes, add files, etc without affecting the master code. When you are done making changes and have tested that your changes commit your changes into your branch.

#add the files you have added or changed to your branch
git add /path/to/modified/file
git add /path/to/new/file

#when you are done making changes commit your changes to your branch
git commit -am "made these changes ..."  

So now that your branch has all your changes saved we have to merge your branch into your local main and then push your changes (now in main) to the remote git repo.

# Switch back to main
git checkout main

# Ensure main is up to date
git pull origin main

# Merge the feature branch into main
git merge somebranchname

# Push the merged changes to remote repo
git push origin main

Those are the steps! Lets review them one more time

# 1. Create and switch to a new branch
git checkout -b new-feature

# 2. Make your changes
# (edit files, add new files)

# 3. Stage your changes
git add .

# 4. Commit your changes
git commit -m "Description of changes"

# 5. Switch back to main
git checkout main

# 6. Ensure main is up to date
git pull origin main

# 7. Merge your feature branch
git merge new-feature

# 8. Push merged changes to remote
git push origin main

Now that you have pushed your branch to the remote repo the person in charge of the main codebase will

  1. Create a pull Request

  2. Review your changes

  3. Merge the pull request

The steps above are usually done via the web interface on github.com or your gitlab repo. Once your branch has been merged into the main codebase you can delete your branch as it is no longer needed.

# Delete local branch
git branch -d my-branch-3432

# Delete remote branch
git push origin --delete my-branch-3432

That completes the full workflow when using branching to check code in. However, you may also find the following commands useful.

#you can have more than one branch on your computer. To switch between branches use:
git checkout branch-name

#list all branches
git branch

#discard local changes in your working directory
git checkout -- .

#Remove untracked files from your working directory.
git clean
git clean -n #dry run (shows what will be deleted)
git clean -f #force a clean
git clean -d #include untracked directories
git clean -fd #force clean and include untracked directories

#Search for a string or pattern in your repository
git grep "search term"

#Create custom shortcuts for git commands
#git co instead of git checkout
git config --global alias.co checkout  
#git br instead of git branch
git config --global alias.br branch
#git st instead of git status
git config --global alias.st status

#check of corrupted files in your git repo
git fsck

#if git fsck returns dangling objects use git gc - Git's garbage collection will remove dangling objects and optimize your repository
git gc

Congratulations on making it to the end of this article! Thanks again for reading. Please like and SHARE. 🙂 Note: to learn more there are also a number of online resources you may want to checkout. These are all free.

#Free, interactive courses provided by GitHub to learn Git and GitHub workflows. Includes hands-on labs and step-by-step guides
https://skills.github.com/

#GitHub's documentation - includes Git tutorials and guides
https://docs.github.com/en/get-started/using-git

#the definitive guide to Git. Covers everything from basic to advanced Git concepts
https://git-scm.com/book/en/v2

Sponsor: If you want to git fit or git healthy try the LifePro Vibration Plate. It is a Full Body Workout, Recovery & Lymphatic Drainage system. I recently bought one of these for my wife (and I have also been secretly using it too) https://www.amazon.com/dp/B085WBB48B?tag=fingerpointfo-20