master-git-basics
master-git-basics

Basic Git Commands

Introduction to Git

Ah, Git! The very cornerstone of modern software development, yet a concept that tends to put beginners on edge. But fear not! We are here to demystify this powerful tool, making it accessible and, dare we say, even fun to learn. Whether you’re a developer just starting out, a project manager looking to get your hands dirty with code management, or simply curious about the tech powering collaborative projects, this guide is for you

In this blog, we are going to walk through the basics of Git and explain its complex nature into something easy to understand and digest. You’ll get to know why Git is the de facto for version control, how it can make your life easier, and the key commands that shall be your daily bread and butter. So, strap in! By the time you are done with this read, you will have understood the basic things about Git and be very able to go ahead and use it in your projects.

Why Git?

  • Track Changes: Git tracks every change. In case you screw up, just go back to the previous version.
  • Collaboration: A number of developers can work on the same project without confusion.
  • Branching and Merging: Create branches from the main one to work on new features or experiments in a separate branch and merge them back into the main branch when ready.

Getting Started with Git

Before going into the commands, please make sure that Git is installed on your system. You can download it from git-scm.com, where detailed installation guides are given for Windows, Mac, and Linux systems.


Basic Key Git Commands

  1. git init
    • Syntax: git init
    • Use: Initializes a new Git repository. This command creates a new .git directory in your project, signaling that Git is ready to start tracking changes.
    • Example: In your project directory, open a terminal and type git init. This will make your current directory a Git repository.
  2. git clone
    • Syntax: git clone [URL]
    • Use: Creates a copy of an existing Git repository. This is useful when you want to get a local copy of a project hosted on a server or a platform like GitHub.
    • Example: git clone https://github.com/example/project.git clones the repository into a local directory.
  3. git add
    • Syntax: git add [file] or git add . to add all changes
    • Use: Adds files to the staging area, preparing them to be committed. It’s like telling Git, “Okay, keep an eye on these changes.”
    • Example: git add index.html adds the index.html file to the staging area.
  4. git commit
    • Syntax: git commit -m "Your commit message"
    • Use: Saves your changes to the local repository. It’s akin to taking a snapshot of your project at that moment.
    • Example: git commit -m "Fixed the login bug" records a snapshot of your project with a descriptive message.
  5. git status
    • Syntax: git status
    • Use: Displays the status of your working directory and staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
    • Example: git status might show some files as “modified” or “untracked.”
  6. git branch
    • Syntax: git branch [branch-name]
    • Use: Creates a new branch. Branches allow you to develop features, fix bugs, or experiment with new ideas in a contained area of your repository.
    • Example: git branch feature-login creates a new branch named feature-login.
  7. git checkout
    • Syntax: git checkout [branch-name]
    • Use: Switches between branches. It allows you to move to the branch where you want to work.
    • Example: git checkout feature-login switches your working directory to the feature-login branch.
  8. git merge
    • Syntax: git merge [branch-name]
    • Use: Merges changes from one branch into another. It’s often used to combine the work done in different branches.
    • Example: If you’re in the master branch and want to include changes from feature-login, you’d use git merge feature-login.

— Syntax of Git —

Create a Repository

# Make a fresh local repository.
$ git init [project_name]

# Clone a repository
$ git clone git_url

clone a repository and place it in a given directory
$ git clone git_url your_directory

Configuration

Choose the name that will appear next to your tags and commits.
$ git config --global user.name "name"

Set an email address that will be attached to your commits and tags
$ git config --global user.email "email"

Enable some colorization of Git output
$ git config --global color.ui auto

Edit the global configuration file in a text editor
$ git config --global --edit

Remote

Add a git URL as an alias
$ git remote add [alias] [url]

Show the names of the remote repositories you've set up
$ git remote

Show the names and URLs of the remote repositories
$ git remote -v

Remove a remote repository
$ git remote rm [remote repo name]

Change the URL of the git repo
$ git remote set-url origin [git_url]

Working with Branches

List all local branches
$ git branch

List all branches, local and remote
$ git branch -av

Switch to my_branch, and update working directory
$ git checkout my_branch

Create a new branch called new_branch
$ git checkout -b new_branch

Delete the branch called my_branch
$ git branch -d my_branch

Merge branchA into branchB
$ git checkout branchB
$ git merge branchA

Tag the current commit
$ git tag my_tag

Make a change

Show modified files in working directory, staged for your next commit
$ git status

Stages the file, ready for commit
$ git add [file]

Stage all changed files, ready for commit
$ git add .

Commit all staged files to versioned history
$ git commit -m "commit message"

Commit all your tracked files to versioned history
$ git commit -am "commit message"

Discard changes in working directory which is not staged
$ git restore [file]

Unstage a stagged file or file which is staged
$ git restore --staged [file]

Unstages file, keeping the file changes
$ git reset [file]

Revert everything to the last commit
$ git reset --hard

Diff of what is changed but not staged
$ git diff

Diff of what is staged but not yet commited
$ git diff --staged

Apply any commits of current branch ahead of specified one
$ git rebase [branch]

Observe your Repository

Show the commit history for the currently active branch
$ git log

Show the commits on branchA that are not on branchB
$ git log branchB..branchA

Show the commits that changed file, even across renames
$ git log --follow [file]

Show the diff of what is in branchA that is not in branchB
$ git diff branchB...branchA

Show any object in Git in human-readable format
$ git show [SHA]

Temporary Commits

Save modified and staged changes
$ git stash

List stack-order of stashed file changes
$ git stash list

Write working from top of stash stack
$ git stash pop

Discard the changes from top of stash stack
$ git stash drop

Synchronize

Fetch down all the branches from that Git remote
$ git fetch [alias]

Merge a remote branch into your current branch to bring it up to date
$ git merge [alias]/[branch]
# No fast-forward
$ git merge --no-ff [alias]/[branch]
# Only fast-forward
$ git merge --ff-only [alias]/[branch]

Transmit local branch commits to the remote repository branch
$ git push [alias] [branch]

Fetch and merge any commits from the tracking remote branch
$ git pull

Merge just one specific commit from another branch to your current branch
$ git cherry-pick [commit_id]

Tracking path Changes

Delete the file from project and stage the removal for commit
$ git rm [file]

Change an existing file path and stage the move
$ git mv [existing-path] [new-path]

Show all commit logs with indication of any paths that moved
$ git log --stat -M

Ignoring Files

/logs/*

# "!" means don't ignore 
!logs/.gitkeep

/# Ignore Mac system files
.DS_store

# Ignore node_modules folder
node_modules

# Ignore SASS config files
.sass-cache

A .gitignore file specifies intentionally untracked files that Git should ignore

#Git Tricks

Rename branch

Renamed to new_name
$ git branch -m <new_name>

Push and reset
$ git push origin -u <new_name>

Delete remote branch
$ git push origin --delete <old>

Log

Search change by content
$ git log -S'<a term in the source>'

Show changes over time for specific file
$ git log -p <file_name>

Print out a cool visualization of your log
$ git log --pretty=oneline --graph --decorate --all

Branch

List all branches and their upstreams
$ git branch -vv 

Quickly switch to the previous branch
$ git checkout -

Get only remote branches
$ git branch -r

Rewriting history

Rewrite last commit message
$ git commit --amend -m "new message"

Git Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

Conclusion: Taking the Next Steps

Congratulations! You’ve just scratched the surface of what’s possible with Git. With these basic commands and concepts under your belt, you’re well on your way to becoming a Git pro. The journey of mastering Git is ongoing, and there’s always more to learn. Experiment with commands, try contributing to open-source projects, or collaborate with others on something exciting. Remember, practice makes perfect, and the more you use Git, the more intuitive it will become.

As you continue your Git journey, always keep exploring, learning, and sharing your knowledge. Happy coding!

Show 16 Comments

16 Comments

  1. Hello there I am so thrilled I found your blog page, I really found you by
    error, while I was looking on Aol for something else, Regardless I am here now and would just like
    to say thanks a lot for a remarkable post and a all
    round enjoyable blog (I also love the theme/design), I don’t have
    time to go through it all at the moment but I have saved it
    and also added your RSS feeds, so when I have time I will
    be back to read more, Please do keep up the fantastic work.

  2. Hi there! I realize this is sort of off-topic however
    I had to ask. Does operating a well-established blog like yours
    take a large amount of work? I am brand new to writing a blog however I do
    write in my diary everyday. I’d like to start a blog so I can share my personal experieence and views online.
    Please let me know if you have anny suggestions or tips
    ffor new aspiring bloggers. Thankyou! https://Jobhub.jp/co_workers/ninabang05

Leave a Reply

Your email address will not be published. Required fields are marked *