top of page

Coffee and Tips Newsletter

Inscreva-se na nossa newsletter semanal

Nos vemos em breve!

How to use Git: A Tutorial for Beginners

  • Writer: JP
    JP
  • 7 days ago
  • 4 min read

How to use Git and understanding the Structure of Git Commands

Comandos Git
How to use Git

Let's understand how to use Git and also understanding the general structure of a Git command :


git <command> [arguments] [parameters]

  • Command: The action Git will perform (e.g., commit, pull, add)

  • Arguments (or flags): Modify the behavior of the command (e.g., -m, --force)

  • Parameters: Are the values or targets of the command (e.g., file name, branch name)


Exemplo clássico:

git pull origin master
  • git: invokes Git

  • pull: command to “pull” or fetch updates from the remote repository

  • origin: parameter, name of the remote repository (default is “origin”)

  • master: parameter, name of the branch to update locally


1. git init


Command: init


What it does: Initializes a new Git repository in the current directory.


git init

  • No arguments or parameters in the basic form.

  • Can use:

    • --bare: creates a repository without a working directory (server mode).


Common error: Running git init in a folder that already has a Git repository.



2. git clone


Command: clone


What it does: Creates a local copy of a remote repository.



  • https://...: parameter with the remote repository URL.

  • Useful arguments:

    • --branch name: clones a specific branch.

    • --depth 1: clones only the latest commit.t.



3. git status


Command: status


What it does: Shows what was changed, added, or removed.

git status

  • No required arguments or parameters.



4. git add


Command: add


What it does: Adds files to the staging area (preparing for commit).


git add file.txt

  • file.txt: parameter — the file being added.

  • Argumentos úteis:

    • .: adds all files in the current directory.

    • -A: adds all files (including deleted ones).

    • -u: adds modified and deleted files.



5. git commit


Command: commit


What it does: Saves the staged changes into Git history.


git commit -m "message"

  • -m: required argument to define the commit message.

  • "message": parameter — the message describing the change.


Other arguments:

  • --amend: edits the last commit.



6. git push


Command: push


What it does: Sends local commits to the remote repository.


git push origin main

  • origin: parameter — remote repository name.

  • main: parameter — the branch to be updated remotely.


Useful arguments:


  • --force: forces the push (use with caution).

  • --set-upstream: sets the default branch for future pushes.



7. git pull


Command: pull


What it does: Fetches changes from the remote repo to your local branch.


git pull origin main

  • origin: parameter — remote repo name.

  • main: parameter — branch to be updated locally.


Useful arguments:


  • --rebase: reapplies your commits on top of remote commits instead of merging.


Common error: Having uncommitted local changes causing conflicts.



8. git branch


Command: branch


What it does: Lists, creates, renames, or deletes branches.


git branch new-feature

  • new-feature: parameter — name of the new branch.


Useful arguments:


  • -d name: deletes a branch.

  • -m new-name: renames the current branch.



9. git checkout


Command: checkout


What it does: Switches branches or restores files.


git checkout new-feature

  • new-feature: parameter — the target branch.


Useful argument:


  • -b: creates and switches to a new branch.

git checkout -b nova-feature


10. git merge


Command: merge


What it does: Merges changes from one branch into another.


git merge branch-name

  • branch-name: parameter — the branch with changes to apply.




11. git log


Command: log


What it does: Shows the commit history.


git log

Useful arguments:


  • --oneline: displays commits in one line.

  • --graph: draws the branch graph.

  • --all: includes all branches in the log.



12. git reset


Command: reset


What it does: Reverts to a previous commit.


git reset --soft HEAD~1

  • --soft: keeps changes in the code.

  • HEAD~1: parameter — points to the previous commit.


Other option:


  • --hard: erases everything permanently.



13. git revert


Command: revert


What it does: Creates a new commit undoing a specific commit.


git revert abc1234

  • abc1234: parameter — the hash of the commit to be reverted.



14. git stash


Command: stash


What it does: Temporarily stores uncommitted changes.


git stash
git stash pop

Useful arguments:


  • pop: applies and removes the stored stash.

  • list: shows all stashes.



15. git remote


Command: remote


What it does: Manages connections to remote repositories


git remote add origin https://github.com/user/repo.git

  • add: argument — adds a new remote.

  • origin: parameter — name of the remote.

  • https://...: parameter — repository URL.


Others:


  • -v: shows existing remotes.

  • remove: removes a remote.



Final Thoughts


Understanding what each part of a command does (command, argument, parameter) is a game-changer for anyone learning Git. With this guide, you're not just running commands — you're understanding them, and that makes all the difference.


Know that throughout your journey working on multi-team projects, understanding Git is almost mandatory. Use this guide in your daily routine, and I’m sure it’ll help you a lot.



 
 
 

Comments


bottom of page