DEV Community

Master Git in 7 minutes

Valeria on July 11, 2021

Essentially, Git keeps tabs on text changes, but the definition is a version control system. Chances are you've already used git one way or another...
Collapse
 
nerd profile image
Dhananjay Panage

As a beginner got to know a lot because of this, I would like to add one more to it git rm --cached filename it removes files from your repo but still is available locally.

Collapse
 
valeriavg profile image
Valeria

Thank you for the addition!

Collapse
 
zohaib546 profile image
Zohaib Ashraf

this command removes file from your staging area but not locally

Collapse
 
vipinkrishna profile image
vipinkrishna

It removes files from from staging/index only

Collapse
 
safinghoghabori profile image
Safin Ghoghabori

I think it wont remove from remote repo but staging area.

Collapse
 
valeriavg profile image
Valeria

Git rm is the exact opposite of add.
So if you *add*ed files and then called git rm --cached smth it will remove that something from the staged changes. If you didn't change or add anything since the last commit, calling git rm will create this change (as if you would delete the file), staging it for your next commit.

Collapse
 
myerschris78 profile image
Chris Myers

I tried git merge -X theirs hello.txt and received the message merge: hello.txt - not something we can merge Have you encountered this?

Collapse
 
valeriavg profile image
Valeria

Try git merge -X theirs dev instead.
I apologise for this mistake, git merge requires a branch name, of course, not the file name. I've corrected that in the article, thank you!

Collapse
 
myerschris78 profile image
Chris Myers

thanks!

Collapse
 
spha88 profile image
Siphamandla Mehlomakulu

Thank for this amazing article, could you please help me with one more issue.

How do I add changes made after a commit to the last commit. For example, I just created a nav menu and committed, after which I made a small change which I don't want to commit on its own but to add to the last commit.

Hope you understand and thank you in advance.

Collapse
 
valeriavg profile image
Valeria

Sure thing! Firstly, you add the changes with git add, then change the last commit with git commit --amend.
If you pushed your commit to the remote repository to push this change you'll need to use git push --force.

Collapse
 
emptypockets profile image
Andrey Kondratyuk

Does it matter if branch changes are pushed to a remote before rebasing? Would the steps be any different?

Collapse
 
valeriavg profile image
Valeria

No, the steps would be the same, but force push would wipe remote branch commits that have not been pulled and may require hard reset on all local repositories, apart from your own.

Collapse
 
emptypockets profile image
Andrey Kondratyuk • Edited

I don't think I 100% follow.

If I created a branch git checkout -b my-new-branch and then stage, commit, and push:

  • git add .
  • git commit -m "my message"
  • git push origin my-new-branch

Does it matter that it the code was pushed to the remote? Could I then rebase as you described?

Thread Thread
 
valeriavg profile image
Valeria

Yes you can rebase my-new-branch from main with git rebase main locally, but you will need to git push --force my-new-branch to push these changes to a remote origin

Thread Thread
 
emptypockets profile image
Andrey Kondratyuk

Thanks for the clarification. I think I understand. My plan at the moment is to try it with different scenarios and make sure I really do get it.

Great guide. Very much appreciated!

Collapse
 
mayankpathak profile image
Mayank Pathak • Edited

Great Article @valeriavg , helpful for those, whose beginning with Git.

Collapse
 
mohmmadmoussa1988 profile image
mohmmadmoussa1988

Thank you, Your way is very useful, could you do another one for mid and advanced levels please

Collapse
 
valeriavg profile image
Valeria

There's a very good book that covers git in depth should you seek more knowledge on the subject.

Collapse
 
mohmmadmoussa1988 profile image
mohmmadmoussa1988

Thank you for your input, much appreciated

Collapse
 
joshuapatel profile image
Joshua Patel

How are you being compatible with GitHub by changing the default branch name? It's completely unnecessary. If you're using "master" as the default branch name, GitHub will still let you use it as the default branch name.

Collapse
 
valeriavg profile image
Valeria

When you create a repository on GitHub the default branch is main. It's not prohibited to use master as a default branch, but that would require a bit of a setup on GitHub to change the default name. Now I personally don't have a preference on how the branch is going to be called, but if a service, that I rely on a daily basis, asks me to do a one tiny naming change I prefer to comply out of respect.

Collapse
 
dallascat profile image
Chris Adams

I got to the part of remote repository, made an example repository in github and tried the commands above and get a RSA key fingerprint "not known by any other names". I try to continue and get permission denied (publickey). Ideas?

Collapse
 
valeriavg profile image
Valeria

Try this solution from stack overflow: stackoverflow.com/questions/956089...

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great article! Well done!

Here is also a free open-source eBook on how to get started with Git that might be helpful for some people:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters

  • About the book
  • Introduction to Git
  • Version Control
  • Installing Git
  • Basic Shell Commands
  • Git Configuration
  • Introduction to GitHub
  • Initializing a Git project
  • Git Status
  • Git Add
  • Git
Collapse
 
ebdonato profile image
Eduardo Donato

After read this, I am a git master now! 😎

Collapse
 
swaranan profile image
Swaranan Singha Barman

Very useful

Collapse
 
renas63 profile image
Renas

Very helpful article! Thanks for sharing

Collapse
 
johnjacobkenny profile image
Kenny John Jacob

Great article, and I hope people will try all the commands out and spend more than 7 minutes in practicing these 🙂

Collapse
 
all_stacks_developer profile image
All Stacks Developer

Great article!

Collapse
 
chamsedinebouhouch profile image
BOUHOUCH chamseddine

thank you for sharing

Collapse
 
stuartcmd profile image
Stuart

Thanks for a marvelous Git tutorial.

Collapse
 
mehtab2899 profile image
Mehtab Multani

If you using latest version of git then use:
git config --global init.defaultBranch main

Collapse
 
bwildenhain profile image
Benedikt Wildenhain • Edited

I had to use "git config ––global init.defaultBranch main" to configure my git, the syntax using "=" isn't accepted with git 2.30.2.

Collapse
 
valeriavg profile image
Valeria

Thank you, I'll update the article to reflect this

Collapse
 
qq449245884 profile image
qq449245884

Dear Valeria,may I translate your all dev articles into Chinese?I would like to share it with more developers in China. I will give the original author and original source.

Collapse
 
valeriavg profile image
Valeria

Yes, you have my permission to translate my articles to Chinese and publish them on a medium of your choice, provided you mention the original article source and author.
P.S. I'm flattered :-)