Git – Commands

By | 08/03/2023

In this post, we will see some Git commands that can help us to manage our projects.

git init:
It is used to start a new repository.

git init 'repository name'


git config:
It is used to set the name and the email to use with our commits.

git config --global user.name "name"
git config --global user.email "email"


git add:
It is used to add files to the staging area.

git add .


git commit:
It is used to save our changes.

git commit -m "message"


git diff:
It is used to show the differences between files that we haven’t saved yet.

git diff


git status:
It is used to show the status of the current branch.

git status


git log:
It is used to show the history of the current branch.

git log


git push:
It is used to push our files from the local repository to the remote repository.

git push "url remote repository" "branch name"


git pull:
It is used to get the latest version from the remote repository.

git pull "url remote repository"


git branch:
It is used to manage branch.
[List of all branches]

git branch

[Create a new branch]

git branch "name new branch"

[Delete a branch]

git branch -d "branch name"


git checkout:
It is used to switch from one branch to another.

git checkout "branch name"


git tag:
It is used to give a tag to a specific commit; if we don’t specify the commit ID, it will add the tag at the latest commit.

git tag [tag_name] [commit_SHA] -m "Tag message"


git merge:
It is used to merge a branch with another.

git merge "branch that we want to merge in the selected branch"

For example:
We have the branch called “branch_new_Change” where the file main.js is this:

Instead, in the branch “master” the file main.js is this:

Now, if we want to push the version of the main.js file from the “branch_new_change” in the “master” branch, we have to select the “master” branch:

Then, we have to run the command:

git merge branch_new_change

And now, if we check again the main.js file in the “master” branch, this will be the result:


git clone:
It is used to create a local working copy from a remote repository.

git clone "url remote repository"


git stash:
It is used to create temporarily save changes that are not yet committed. It’s useful when we need to switch branches or work on something else without committing our incomplete changes.
Following is an example of how we can switch from a branch called stage1, without commit some changes made, in another branch called master:

1. Changes in our current branch (stage1):

echo "Change in file1" >> file1.txt
git status

Output:

Changes not staged for commit:
    modified: file1.txt


2. Using the command ‘Stash’, we will save the changes without commit them:

git stash

Output:

Saved working directory and index state WIP on stage1: <commit_hash>


3. We switch to the master branch:

git checkout master


4. After we finished to work in the ‘master’ branch, we come back to the ‘stage1’ branch and, using the ‘stash’ command, we will recover the changes not yet commited:

git checkout stage1
...
git stash apply




OBVIOUSLY, THESE ARE ONLY SOME EXAMPLES OF THE GIT COMMANDS BUT, IF WE WANT TO KNOW THE COMPLETE LIST, WE CAN SEE IN THE GIT WEB SITE.



Leave a Reply

Your email address will not be published. Required fields are marked *