DEV Community

Cover image for πŸ“š Let's learn the main GIT commands
Veni
Veni

Posted on

πŸ“š Let's learn the main GIT commands

Let's simplify and review the main commands used in GIT and what each of them is for!

✨ Initializing a repository
After installing GIT and configuration your credentials, open Git Bash by right-clicking on your project folder and type the command below:

git init
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ Connecting with remote repository
To connect your local repository with the remote one:

git remote add origin https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

This link is just a example, replace with yours.

⚑ Cloning a remote repository
If you already have a remote repository, you can just clone this into your folder:

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

βž• Adding files in your commit
Before do a commit, you need add your new and change files to the staging area.

Adding just one file:

git add file_name
Enter fullscreen mode Exit fullscreen mode

Adding a file that is in another folder:

git add folder/file_name
Enter fullscreen mode Exit fullscreen mode

Adding all the files and changes:

git add .
Enter fullscreen mode Exit fullscreen mode

βž– Removing a file in the stating area
To remove a file in the staging area (this not undo your changes in your real files project):

git restore -- staged file_name
Enter fullscreen mode Exit fullscreen mode

If you want remove all them:

git restore -- staged .
Enter fullscreen mode Exit fullscreen mode

πŸ” Checking status in the staging area
To check which files is already in the staging and which is not in:

git status
Enter fullscreen mode Exit fullscreen mode

What's in red is the files which is not in the staging area yet:

Git Status with files to add

What's in green is the files which is in the staging area:

Git Status with files already added

And if you don't have any files to add in the staging area:

Git status empty

πŸ“¦ Doing a commit
To do a commit, which will prepare all the files in the staging area to be push late:

git commit -m "your message here"
Enter fullscreen mode Exit fullscreen mode

Be careful when writing a message, be sure that you are explain what the commit is for.

πŸš€ Doing a push
To do a push to your remote repository:

git push -u origin nome_branch
Enter fullscreen mode Exit fullscreen mode

πŸ” Checking your commit history
To check all commits that you did:

git log
Enter fullscreen mode Exit fullscreen mode

πŸ“ Updating your last commit message
To update the last commit:

git commit --amend -m "your new message here"
Enter fullscreen mode Exit fullscreen mode

↩️ Undoing a commit in the HEAD
To undo the last commit that was did without undo the changes in the staging area, just in the HEAD (which always point in the last commit):

git reset --soft
Enter fullscreen mode Exit fullscreen mode

↩️ Undoing a commit in the HEAD and in the staging area
To undo the last commit in the HEAD (which always point in the last commit) and the staging area:

git reset --mixed
Enter fullscreen mode Exit fullscreen mode

↩️❗ Undoing a commit in the HEAD, in the staging area and in your project
To undo the last commit in the HEAD (which always point in the last commit) and the staging area AND IN YOUR PROJECT:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

⬇️ Updating your project by pull
In case you and another person is working in the same project and you want get the changes that was did and update in your local repository:

git pull
Enter fullscreen mode Exit fullscreen mode

βž• Creating a new branch
To create a new branch:

git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode

πŸ”¨ Swapping branches
To swap to another branch already created:

git checkout branch_name
Enter fullscreen mode Exit fullscreen mode

πŸ” Checking the last commit in each branch
To check the last commit in each branch already created:

git branch -v
Enter fullscreen mode Exit fullscreen mode

❌ Deleting a branch
To delete a branch:

git branch -d branch_name
Enter fullscreen mode Exit fullscreen mode

πŸ”€ Merging between two branches
Before do a merge, check if you are in the branch that will receive the update. After that, type the command below:

git merge origin_branch
Enter fullscreen mode Exit fullscreen mode

Top comments (0)