avatar

Andres Jaimes

Common git commands

By Andres Jaimes

- 4 minutes read - 641 words

Imagine you’re a developer working on a project that’s stored in a remote Git repository. Your first step is to clone the repository using the command git clone git@github.com:some-account/some-project.git.

Once you’ve cloned the repository, you can start working on a new feature by creating a new branch using the git checkout command with the -b flag and a branch name, like this: git checkout -b my-feature. This creates a new branch called my-feature and switches to it. To see the list of branches, you can use the git branch command.

If you need to switch back to the master branch to work on something else, you can use the command git checkout master.

If you want to view a specific commit or a log of all the commits, you can use the git log command. You can even checkout a specific commit by passing its SHA to the git checkout <SHA> command.

To update your branch from master, you can use the following commands: git checkout master to switch to the master branch, git pull to fetch the latest changes, git checkout <branch> to switch back to your branch, and git merge master to merge the changes from master into your branch. If a conflict occurs, you can abort the merge by using the git merge --abort command, or you can resolve the conflict by editing the file and then using the git add <file> command to add the file to the list of files to commit, and then git commit to commit the changes.

If you’re swithing between branches and need to temporarily save your changes without committing them to avoid conflicts, you can use the git stash command. To recover the changes later, you can use the git stash pop command, and to remove them completely, you can use the git stash drop command.

To see the changes you’ve made locally, you can use the git status command. If you need to undo changes to a specific file, you can use the command git checkout -- filename.txt. In general, when you want to perform a git operation on a single file, use -- filename. To see the differences between your local changes and the latest commit, you can use the git diff command, or to compare with master you can use git diff master...

To get the URL of the remote repository, you can use the git remote -v command. This will show you the origin URL from where you originally cloned the repository.

When you’re done updating your files, you can use git status to see the list of files that you have modified, then git add <file> to add the files to the list of files to commit, and git commit to commit them.

When you’re ready to update the remote repository with your changes, you can use the git fetch or git pull commands to download any new changes, and then use the git push command to push your changes to the remote repository.

Important. It’s always recommended to try the commands on a test repository before using them on a real project.

Common questions

pull vs fetch, what’s the difference?

The git pull command is used to update the local repository with changes from the remote repository. It combines two commands: git fetch and git merge.

git fetch only downloads the changes from the remote repository to the local repository, without merging them into the current branch.

In summary, git pull is a convenient way to update the local branch with the latest changes from the remote branch, but it can also be risky if there are conflicts between the local and remote changes. In contrast, git fetch is a safer way to update the local repository as it does not automatically merge the changes, allowing you to review the changes before merging them manually.