GIT commands

Collection of GIT commands.

Reset last changes (locally)

1
git reset --hard HEAD~1
  • Affects local GIT repository only
  • Discard local commits without keeping changes in files

Reset file last changes (locally)

1
git reset --soft HEAD~1
  • Discard local commits while keeping changes in files

Retrieve latest changes form remote repository

  • Pulls the latest changes from the current branch
  • This command will cause error when there are uncommitted changes and your local repository is outdated (i.e.: # commits behind)
1
git pull

List GIT configurations

1
git config --list # list all configs 

Creating a new repository

1
2
3
4
git init  
git add --all
git commit -m "Initial Commit"
git push -u origin master

Cloning an existing repository

1
$ git clone "example@gitdomain.com/etc/etc/repository.git"

How to resolve merging conflicts

This example uses Visual Code and Gitlens but the principles should be applicable in other tools.  When multiple users are using the same repository code branch, it is common to encounter situations where there are conflicts in code between your local repository and the remote repository on GIT server.  Below are 2 ways to resolve this:

Revert, pull and apply changes

Revert all your local changes and re-apply the changes after retrieving the latest changes from the remote repository branch.

  • Stash your local changes
  • Pull latest from the remote server
  • Re-apply Stashed changes
1
2
3
4
5
6
# stash staged files (option: -a, all files -u, un-tracked files) 
$ git stash
$ git pull

$ git stash pop # applies changes and remove stash
$ git stash apply # OR applies changes but keep stash

VSCode example of stashes.

Commit, pull and merge

Commit all your local changes and pull the latest code from the remote repository. This will raise conflicts that you have to resolve before your code can be merged and sync back to the remote GIT server.

1
2
$ Stage and commit changes (locally)
$ git pull # pull changes, resolve conflicts and commit changes

Merge conflict as a result of conflicting changes between local/remote code files.

source - stash commands