avatar

Andres Jaimes

Common git commands

By Andres Jaimes

- 4 minutes read - 681 words

Hello. This page’s intention is to help me keep some git commands handy, since some times I forget how to do some tasks in the github workflow.

Our first step is to clone a repository using the command git clone git@github.com:some-account/some-project.git. Why use the git link instead of the http one? To avoid user authorization issues.

Once we’ve cloned the repository, we 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, we can use the git branch command, and use git branch --show-current to see the current branch.

If we need to switch back to the master branch (now called main), we can use the command git checkout master.

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

To update our branch from master, we 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 our branch, and git merge master to merge the changes from master into our branch. If a conflict occurs, we can abort the merge by using the git merge --abort command, or we 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 we’re swithing between branches and need to temporarily save our changes without committing them to avoid conflicts, we can use the git stash command. To recover the changes later, we can use the git stash pop command, and to remove them completely, we can use the git stash drop command.

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

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

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

When we’re ready to update the remote repository with our changes, we 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. I recommended you 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 us to review the changes before merging them manually.