cft

6 Git Commands That I Use Almost Everyday — Part 2

6 Git habbits to help me more productive


user

Pandhu Wibowo

10 months ago | 12 min read

Original Article: https://pandhuwibowo.medium.com/6-git-commands-that-i-use-almost-everyday-part-2-112f56cc2c1b

Git is a version control system that allows developers to keep track of changes made to their code and collaborate with other team members. There are several Git commands that are commonly used in the development process, including git pull, git add, git commit, git push, git remote, and git merge.

https://cdn-media-1.freecodecamp.org/images/VQhi-KgyeBh6jegrDc2zaLOGxsBWq0Bw5dNq

# git pull

Git pull is a command used to fetch and download content from a remote repository and immediately update the local repository to match that content. It is a combination of two other Git commands, git fetch and git merge. Git fetch is used to download the content from a remote repository, and git merge is used to merge the downloaded content with the local repository.

An example of using git pull would be if you are working on a project with a team and you want to retrieve the latest changes made by your team members. In this case, you would use the git pull command to download the latest changes from the remote repository and merge them with your local repository.

To use git pull, you would first open your terminal or command prompt and navigate to the directory where your local repository is located. Then you would enter the command git pull and specify the remote repository you want to pull from. For example, if the remote repository is located on GitHub, the command would be git pull origin master. The “origin” in this command refers to the remote repository, and the “master” branch refers to the branch you want to pull from.

It is important to note that before using git pull, it is recommended to first use git fetch to retrieve the changes from the remote repository, and then use git merge to merge the changes with the local repository. This allows for a more controlled merge process and allows for conflicts to be resolved before merging.

However, the git pull command combines these two steps into one and automatically performs the fetch and merge for you. This can be useful for quick and simple updates, but it is important to be aware of potential conflicts and to handle them appropriately.

For example, you and your team member both modified the same file, so when you try to git pull, git will not be able to merge the changes automatically. In this case, it will show the conflict and ask you to resolve the conflict manually and then commit the changes.

In summary, git pull is a command used to retrieve and merge changes from a remote repository with the local repository. It is a combination of git fetch and git merge and can be useful for quick and simple updates, but it is important to be aware of potential conflicts and handle them appropriately.

git pull <remote> <branch>

For example, if you want to pull changes from the “origin” remote and the “master” branch, the command would be as follows:

git pull origin master

# git add

Git add is a command used to stage changes in a local repository before committing them. It is used to add files, directories, or changes to the repository’s index, which is used to track changes made to the repository. The git add command is used to stage changes before committing them, which means that the changes will be tracked by Git but will not be part of the repository until they are committed.

There are several variants of the git add command that can be used depending on the specific needs of the user.

  1. git add <file name> : This command is used to stage a specific file. For example, if you made changes to a file called “index.html”, the command would be git add index.html.
  2. git add . : This command is used to stage all changes in the current directory and its subdirectories. This is useful for adding all changes in a directory at once.
  3. git add -A : This command is used to stage all changes, including new files and deleted files. This is useful for adding all changes in the repository at once.
  4. git add -p : This command is used to stage changes in a file interactively. This allows the user to view and select specific changes to stage before committing.
  5. git add — ignore-removal : This command is used to stage changes and ignore file deletions. This is useful for staging changes to files while keeping deleted files in the repository.

It is important to note that git add command only stages the changes, and it does not commit them. To commit the changes, you need to run git commit command.

For example, imagine you have made changes to two files ‘index.html’ and ‘style.css’ and you want to stage them before committing, you would use the command git add index.html style.css to stage the changes for both files.

In summary, git add is a command used to stage changes in a local repository before committing them. It is used to add files, directories, or changes to the repository’s index, which is used to track changes made to the repository. There are several variants of the git add command that can be used depending on the specific needs of the user. The basic syntax of the git add command is as follows:

git add <file>

For example, if you want to add a new file called “example.txt” to the staging area, the command would be as follows:

git add example.txt

# git commit

Git commit is a command used to save changes to a local repository. It is used to take a snapshot of the current state of the repository and save it as a new revision. The git commit command is used to save changes to a local repository, which means that the changes will be part of the repository and can be tracked by Git.

There are several variants of the git commit command that can be used depending on the specific needs of the user.

  1. git commit -m “<message>” : This command is used to commit changes with a message describing the changes made. For example, if you made changes to a file called “index.html”, the command would be git commit -m “updated index.html”.
  2. git commit -a : This command is used to commit all changes, including new files and deleted files. This is useful for committing all changes in the repository at once.
  3. git commit — amend : This command is used to edit the previous commit. This allows the user to make changes to the last commit and add new changes to it.
  4. git commit — no-verify : This command is used to commit changes without running the pre-commit hooks. This is useful for committing changes when the pre-commit hooks are not working.
  5. git commit -S : This command is used to sign the commit with a GPG key. This is useful for verifying the authenticity of a commit.

It is important to note that git commit command saves the changes to the local repository, but it does not push them to the remote repository. To push the changes to the remote repository, you need to run git push command.

For example, imagine you have made changes to two files ‘index.html’ and ‘style.css’ and you want to commit them with a message “updated website layout”, you would use the command git commit -m “updated website layout” to commit the changes for both files.

In summary, git commit is a command used to save changes to a local repository. It is used to take a snapshot of the current state of the repository and save it as a new revision. There are several variants of the git commit command that can be used depending on the specific needs of the user. The git commit command saves the changes to the local repository, but it does not push them to the remote repository. To push the changes to the remote repository, you need to run git push command.

The basic syntax of the git commit command is as follows:

git commit -m “message”

For example, if you want to commit changes made to the “example.txt” file with the message “added new text”, the command would be as follows:

git commit -m “added new text” example.txt

# git push

Git push is a command used to upload changes to a remote repository. It is used to take the changes in a local repository and send them to a remote repository, making them available to other team members. The git push command is used to upload changes to a remote repository, which means that the changes will be available to other team members and can be tracked by Git.

There are several variants of the git push command that can be used depending on the specific needs of the user.

  1. git push <remote> <branch> : This command is used to push changes to a specific remote and branch. For example, if the remote repository is located on GitHub and the branch is called “master”, the command would be git push origin master.
  2. git push -f : This command is used to force push changes to a remote repository. This is useful for overwriting remote changes that were made without the user’s knowledge.
  3. git push — all : This command is used to push all branches to the remote repository. This is useful for pushing multiple branches at once.
  4. git push — tags : This command is used to push all tags to the remote repository. This is useful for pushing tags that were created locally to the remote repository.
  5. git push — set-upstream <remote> <branch> : This command is used to set the upstream branch for a local branch. This allows the user to push changes to a remote branch that does not exist yet.

It is important to note that before pushing changes to the remote repository, it is recommended to first pull any changes from the remote repository to ensure that the local repository is up to date.

For example, imagine you have made changes to your local repository and you want to push them to the remote repository on GitHub, you would use the command git push origin master to push the changes to the master branch of the origin remote.

In summary, git push is a command used to upload changes to a remote repository. It is used to take the changes in a local repository and send them to a remote repository, making them available to other team members. There are several variants of the git push command that can be used depending on the specific needs of the user. Before pushing changes to the remote repository, it is recommended to first pull any changes from the remote repository to ensure that the local repository is up to date. The basic syntax of the git push command is as follows:

git push <remote> <branch>

For example, if you want to push changes made to the “master” branch to the “origin” remote, the command would be as follows:

git push origin master

# git remote

Git remote is a command used to manage remote repositories in a local repository. It is used to view and manipulate the remote repositories associated with a local repository. The git remote command is used to manage remote repositories, which means that it allows the user to view and manipulate the remote repositories associated with a local repository.

There are several variants of the git remote command that can be used depending on the specific needs of the user.

  1. git remote -v : This command is used to view the remote repositories associated with a local repository. It shows the name of the remote repository and its URL.
  2. git remote add <name> <url> : This command is used to add a new remote repository to a local repository. For example, if you want to add a remote repository called “origin” with the URL “https://github.com/username/repo.git", the command would be git remote add origin https://github.com/username/repo.git.
  3. git remote rename <oldname> <newname> : This command is used to rename a remote repository. For example, if you want to rename a remote repository from “origin” to “upstream”, the command would be git remote rename origin upstream.
  4. git remote remove <name> : This command is used to remove a remote repository from a local repository. For example, if you want to remove a remote repository called “origin”, the command would be git remote remove origin.
  5. git remote set-url <name> <newurl> : This command is used to change the URL of a remote repository. For example, if you want to change the URL of a remote repository called “origin” from “https://github.com/username/repo.git" to “https://github.com/username/newrepo.git", the command would be git remote set-url origin https://github.com/username/newrepo.git.

It is important to note that git remote command allows to view and manage remote repositories, but it does not push or pull changes from remote repositories. To push or pull changes from remote repositories, you need to use git push or git pull command.

For example, imagine you want to view the remote repositories associated with your local repository, you would use the command git remote -v to view the name of the remote repository and its URL.

In summary, git remote is a command used to manage remote repositories in a local repository. It is used to view and manipulate the remote repositories associated with a local repository. There are several variants of the git remote command that can be used depending on the specific needs of the user. git remote command allows to view and manage remote repositories, but it does not push or pull changes from remote repositories. To push or pull changes from remote repositories, you need to use git push or git pull command. The basic syntax of the git remote command is as follows:

git remote <subcommand> <remote>

For example, if you want to add a new remote repository called “upstream”, the command would be as follows:

git remote add upstream <remote repository URL>

# git merge

Git merge is a command used to combine changes from multiple branches in a local repository. It is used to take the changes in one branch and merge them into another branch, allowing for changes from multiple branches to be combined into one branch. The git merge command is used to combine changes from multiple branches in a local repository, allowing for changes from multiple branches to be combined into one branch.

There are several variants of the git merge command that can be used depending on the specific needs of the user.

  1. git merge <branch> : This command is used to merge changes from one branch into the current branch. For example, if you want to merge changes from a branch called "feature" into the current branch, the command would be git merge feature.
  2. git merge --no-ff : This command is used to merge changes and create a new commit even if the merge could be performed with a fast-forward. This is useful for creating a new commit that represents the merge.
  3. git merge --squash : This command is used to merge changes and squash them into a single commit. This is useful for combining multiple commits into one commit.
  4. git merge --abort : This command is used to abort a merge that is in progress. This is useful for canceling a merge when conflicts occur.
  5. git merge --no-commit : This command is used to merge changes without committing them. This is useful for merging changes and testing them before committing them.

It is important to note that git merge command allows to merge changes from multiple branches in a local repository, but it can create conflicts if the same lines of code have been modified in different branches. It is recommended to resolve conflicts before committing the merge.

For example, imagine you have made changes to a branch called "feature" and you want to merge those changes into your "master" branch. You would use the command git merge feature in the master branch to merge the changes from the feature branch into the master branch.

In summary, git merge is a command used to combine changes from multiple branches in a local repository. It is used to take the changes in one branch and merge them into another branch, allowing for changes from multiple branches to be combined into one branch. There are several variants of the git merge command that can be used depending on the specific needs of the user. git merge command allows to merge changes from multiple branches in a local repository, but it can create conflicts if the same lines of code have been modified in different branches. It is recommended to resolve conflicts before committing the merge.

The basic syntax of the git merge command is as follows:

git merge <branch>

For example, if you want to merge changes made to the “feature” branch into the “master” branch, the command would be as follows:

git merge feature

In summary, Git is a powerful version control system that allows developers to easily track changes made to their code and collaborate with other team members. The git pull command is used to retrieve changes from a remote repository and merge them with the local repository. The git add command is used to stage changes for a commit. The git commit command is used to commit changes to the local repository. The git push command is used to push changes to a remote repository. The git remote command is used to manage remote repositories. The git merge command is used to combine changes from multiple branches in a local repository, allowing for changes from different branches to be merged into one branch. Together, these commands provide a powerful set of tools for managing code changes and collaborating with other team members. By using Git, developers can easily track changes, collaborate with others, and maintain a clear history of the codebase. With its various commands, Git allows developers to work efficiently and effectively, making it an essential tool for any software development project.

cheers!!

Upvote


user
Created by

Pandhu Wibowo

Assalamu’alaikum. I’m Software Engineer | Tech Enthusiast — Support me on beneteen.com | Follow me : https://www.instagram.com/pandhu.wibowo/


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles