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
- 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.
- Syntax:
- 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.
- Syntax:
- git add
- Syntax:
git add [file]
orgit 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 theindex.html
file to the staging area.
- Syntax:
- 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.
- Syntax:
- 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.”
- Syntax:
- 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 namedfeature-login
.
- Syntax:
- 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 thefeature-login
branch.
- Syntax:
- 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 fromfeature-login
, you’d usegit merge feature-login
.
- Syntax:
— 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!
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.
Hey There. I discovered your weblog the usdage of msn.
That is a very smartly written article. I will be sure to bookmark it and come bacxk tto read extra of
your helpful information. Thanks for the post. I’ll certainly comeback. https://Bandurart.Mystrikingly.com/
If you are going for finest contents like me, only pay a visit this web pave
everyday for the reason that itt presents feature contents, thanks https://www.wir-r.com/gyokai/diary/diary.cgi?no=239
I’m really impressed with your writing skills and also with the
layout onn yohr weblog. Is this a paid theme orr did you
customize it yourself? Anyway keep upp the nice quality writing, it’s rare to see a nice blog like this one these days. http://Pspskorea.com/bbs/board.php?bo_table=free&wr_id=168585
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
I got this site from my friend who told mme on the topic of this web site and
at the moment this time I amm visiting tis web page and reading very informative posts at this
time. https://Socialgem.net/post/159614_slot-machines-are-among-the-most-popular-attractions-in-both-land-based-and-onli.html
It’s a pity you don’t have a donate button! I’d most certainly donate to this fantastic blog!
I suplose for now i’ll settle foor bookmarking and adding your RSS feed to my Google account.
I look forwazrd to neww updates and will talk about this site ith my Facebook group.
Chat soon! http://Www.KT.Rim.Or.jp/~youie/cgi-bin/nanmeri/nanmeri.cgi?no=229
I have been browsing on-line more than 3 hours
as of late, but I nnever found any interesting article like yours.
It is beautiful price enough for me. Personally, if all web ownerss and bbloggers
made just right content material as you probably did, the web can be much more usefu than ever before. https://Www.mygoblet.org/forums/topic/how-to-check-cash-app-balance-hurriedly/
Amazing! Its in fact remarkable post, I have got much clear idea regarding from this piece of writing. https://gis-lab.info/forum/memberlist.php?mode=viewprofile&u=35715
I am regular reader, how are you everybody? This paragraph posted at this webb site is
in fact fastidious. https://Www.Nitrnd.com/posts/157826
Hello everyone, it’s my first go to see at thiis web page, and
paragraph is truly fruitful designed foor me,
keep up posting such posts. http://happiness.lib.net/db/epigone/patio4.cgi?mode=view&no=10238
Hey! Would you mind iff I share your blog with my zynga group?
There’s a lot of folks that I think would really
enjoy your content. Please let me know. Thanks https://S.Surveyplanet.com/dz9w0w9i
Howdy! Do you know if they mak any plugins to
assist wijth SEO? I’m trying to get my blog to rank for some targeted
keywords but I’m nnot seeing very good gains. If you know of any please share.
Cheers! https://profile.hatena.Ne.jp/tedbet21/
It’s really a nice and useful pece of information. I am satisfied that
you just sharewd this useful information with us. Please keep us informed like this.
Thanks for sharing. https://betforum.org/threads/discover-the-thrills-of-tedbet-kr.257104/
Hi there, yeah this post is genuinely gokod and I haave learned lot off things from it regarding
blogging. thanks. https://tsunagu.cloud/users/JeffMunson
It is perfect time to make some plans for the longer
term and it is time to be happy. I havfe learn this put up and
if I maay I wish to recommend you few attention-grabbing things or tips.
Perhaps you culd write next articles relating to thgis article.
I want to learn more things approximately it! https://Daeshintravel.com/free/234471