How to use Git Bash for Complete Beginners: 11 things you should learn first

code with lek
5 min readJul 27, 2018

--

You have probably downloaded and installed Github desktop version, even though the GUI looks just fine, it can be really confusing!

Using the Git Bash ( command lines ) is the best alternative, yes it’s intimidating, it was for me a few weeks ago but I decided to learn to use and write a cheat-sheet Git Bash look alike in order not to forget the commands, and today I’m writing this article to share what I learned, enjoy!

Getting Started

I’m assuming you already have a github account and the Git Bash installed on your machine if not, you can download it for free here and create your Github account here

Now let’s start, first create a folder, right click it and select Git Bash Here

right click menu

A cool black window should open like this one:

Git Bash Window

1- Adding files to your folder

a simple “touch newFile.extension” will create a file inside your folder

2- Initialize your folder as a ‘git folder’ (repository)

$ git init

To check the state/status of your folder and files we will constantly use $ git status

3- Add your credentials to your git folder

Add your username :

Add your email (the email of your Github account)

4- Adding files to your git repository

You can do this in 3 ways:

  1. Add a file at a time: $ git add file.extension
  2. Add all files with a certain extension: $ git add *.extension (ex: $git add *.css)
  3. Add all the files at once: $ git add .
  • Use $ git status to verify the commands work

5- Remove files

To remove the added files use

  • If you edit a file after the “git add” command you will need to perform the same command again, so your changes will be saved

6- Commit your work

This can be done in two ways:

  1. $ git commit: this will open the vim Editor

Press ‘i’ to start editing, uncomment the ‘ #from initial commit ‘ by deleting the ‘ # ‘, then press ‘ ESC’ followed by ‘ :wq ’, hit ENTER

2. $ git commit -m ‘This is the commit message’:

This way you won’t have to edit using the vim editor

7- Ignore Files And Directories (folders)

Sometimes you will be lazy and want to add everything with a ‘ $ git add . ‘ , however, you want to exclude some files or directories. Create a .gitignore file: $ touch .gitignore

Open it in a text editor and add the files you want to ignore, let’s say we have a fileIgnoreMe.txt and a folderIgnoreMe inside a folderIgnoreMeParent, this is how it should look like:

.gitignore

Remember to add and commit after each change

8- Branches Branches Branchez
Basically, it’s copies of the same project, they are used in case you want to commit some changes without modifying the original repository(master branch).

You will need to work with branches if you’re a group of contributors.

To create a new branch:

$ git branch branch_name

The above command won’t move us to the new branch, in order to switch branches, go ahead and type:

$ git checkout branch_name

Let’s go edit one of the master’s files, then perform an add command (don’t forget to save the changes Ctrl+S)

$ git add .

Commit the modified file

$ git commit -m ‘commit to new branch’

Now we are going to switch back to the master branch and make sure to keep your eyes on the lines you added to the file

$ git checkout master

The file you edited while on newbranch will be displayed in the text editor without the changes you made !

9- Merging branches

To merge both branches:

$ git merge branch_name

A vim will open on the window, press ‘i’ followed by a message (“merged two branches, yay!”, escape ‘esc’ and type ‘ :wq ’ hit the ENTER button.

10- Push your project

Open your Github Account online, create a new repository, you don’t need to initialize it ( we have already done that part), click the green “Create repository” button. You should see:

git remote add origin https://github.com/yourUsername/repositoryName.git

copy the above line on your Git Bash, then

$ git push -u orgin master
You will have to login through the small window that pops in to validate your credentials

11- Bonus

  • If you ever want to add some files after pushing, all you will have to do after the adding and committing them is:
    $ git push
  • To clone a repository: Move inside a folder, open Git Bash:

$ git commit link_to_the_repository_to_clone

  • if you are multiple developers working on the same repository you can check changes made using:
    $ git pull

That’s it everyone, I hope you learned something, I make tutorials also on youtube here’s my channel (feel free to subscribe and watch my videos): My Youtube Channel: https://www.youtube.com/channel/UCOilWFnZq8VitNeABQpzOGQ

My Github account: https://github.com/deGhost/

My Twitter account: https://twitter.com/codewithleksar

My Instagram account: https://www.instagram.com/codewithleksar/

--

--

code with lek
0 Followers

Computer science student, passionate about science, programming, development and learning new things