Basic Git Commands

diuni
2 min readSep 8, 2020

This is how I use git. If you know how to use it easier, please tell me :)

source: https://git-scm.com/downloads/logos
  1. Go to github.com
  2. Make new remote repository (in github.com) and then git clone in your local pc
$ git clone <repository_url>
how to get url from github.com

After this, you can work in your local repository !

3. Branch

Branch is required when working with multiple people. Usually, it’s better not to work in master, so you need to make your own branch and merge to master.

$ git branch                  # show branch list 
$ git branch <branchname> # make new branch
$ git checkout <branch> # switch branch
$ git branch -d <branchname> # delete branch

< How to merge branch >

$ git checkout master      
$ git merge <branch>

And then go to the your repository in github.com, you can check ‘git pull -request’ and if you don’t have any problem combining your branch to master, you can accept it manually.

4. Update Github Remote Repository

To communicate your colleagues you need to get the latest codes and update your codes to remote repository.

Remember !! PULL → ADD → COMMIT → PUSH

$ git pull     # Import from remote repository and merge to local$ git add *    # select all modified files and prepare the content
$ git add [file_path] # you can also manually select files
$ git add [file_pattern] # you can also use pattern like “*.txt”
$ git commit -m “commit message” # describe a modification$ git push origin <branch> # push local data to remote repository
$ git push origin master

5. Other Frequently Used Command

# show the current branch, added/modified files and directories
$ git status
$ git reset HEAD [file] # cancel the added [file]
$ git reset HEAD # cancel all added files
# Add except specific file
$ git add .
$ git reset [specific_file]
$ git commit -m "commit message"
# Return to specific commit
$ git revert --no-commit [hash of a commit wants to be returned]

How to delete the file

$ git rm [file]          # delete file both in repository and local
$ git rm --cached [file] # delete file only in repository
$ git commit -m "remove file"
$ git push origin <branch>

--

--