DEV Community

Cover image for How to use GIT in shell (basics)
LuCaS
LuCaS

Posted on • Edited on

How to use GIT in shell (basics)

On my full-stack developer journey I've found, that making notes during your learning is a great way to remember stuff. You can also go back to your notes, if you need some help.

If you are new to GIT, you can should read a git handbook here: [https://docs.github.com/en/get-started/using-git/about-git]

(https://learngitbranching.js.org/)

I summarized basics here.

When we are in a local bash and in particular project directory, here is what we can do.

git phases are:

  • WORKING DIRECTORY
  • STAGING AREA
  • LOCAL REPOSITORY
  • REMOTE REPOSITORY

1. To INITIALIZE a git

git init

2. To INCLUDE ALL FILES (those listed in .gitignore are omited

git add .

Premade templates for .gitignore files

[https://github.com/github/gitignore]

3. To CREATE A LOCAL STAGE

git commit -m "Initial Commit"

4. To PUSH commits to a remote repository

Push the git to the origin main (remote)
Don't forget to commit changes first, then :

$ git commit -m "Your commit stage description"
git remote add origin https://github.com/DeLucasso/NAME_OF_GIT.git
git branch -m main
git push -u origin main

5. To RESET to some specifid commit

Use git log command to find a hash of commit that you'd like to reset back to.
git reset --hard [hash]

6. If you want to PRESERVE your work, you can use the stash command:

_git stash
git reset --hard [hash]
git stash pop
_

7. To IGNORE (omit) files from the git repository to be uploaded to GitHub: First create a hidden file gitignore

touch .gitignore

8. To ADD files to the .gitignore file

open .gitignore

9. To REMOVE ALL files from git stage

git -rm --cashed -r .

10. To REMOVE SINGLE file from git stage

git -rm --cashed filename.txt

11. To GET A STATUS a status of git

git status

12. To GET A LOG of the git stage

git log

13. To put a COMMENT into .gitignore use

#This is a comment line in .gitignore

14. To use WILDCARDS in .gitignore

*.txt

An article to understand git branching:

15. To MAKE A NEW a new branch

git branch -m new_branch_name

16. To CHECK branch list

git branch

17. To SWITCH a branch ( Check Out ), then you can see '*' at

git checkout branch_name

19. To RENAME a local branch

git -m old_name new_name

20. To DELETE a LOCAL branch

First you have to checkout a branch that you want to delete (if its' marked as *)
Then you can delete a branch
git -d branch_name_to_delete

21. To DELETE a REMOTE branch

git push origin --delete branch_name

22.!! Everytime when you want to COMMIT changes to the git, you have to do this every time:

git add . // (to include all files to git)
git commit -m "Your commit comment of the changes you've made"

21. To MERGE a branch to main, don't forget to add all files and commit changes first!

First if you are happy with the changes in branch, you can merge the branch with main

git checkout main
git merge name_of_branch_that_you_want_to_merge

The vim editor will open, so you can add comments to merge. Then to save it, just write:
:q!

That's it folks. I wrote everything in an order, that you will probably use it. I hope it help you with a git commands in shell.

Here is my github repo:

Image description

Top comments (0)