Useful Git commands

This is a list of commands, that I found useful for me.

Local commands. Does not impact remote repository until a push.

git clone [repo url] # Fetches a repository locally. This is the git setup.

git checkout [branch name] # Do this to change to your branch. If you play with many branches and windows, do this after every window swap/file change or you will accidentally commit to the wrong branch (see below how to fix...).

git branch # show available branches and current branch.

git branch [branch name] # Creates a new branch from the current checked out branch. (see git checkout above). Important, check before from which branch you are branching!

git push --set-upstream origin <branch name> # Pushes new branch including commits to remote.

git add * # Add new files to your local repository. Commit afterwards (below).

git commit * # Commit your file changes locally before a push. Replace * with a file name if required.

git push # this pushes all your changes from local to remote. Commit all your changes before that (see above). Also see git push -f below.

git pull # get remote changes into your local repository

git tag <tag name> # creates a tag locally. See below to push. Tags do not need to be committed.

git push origin --tags # push local tags to remote

git tag -d <tag name> # deletes a tag locally. You might also need to delete it remotely (see below).

git log # shows all commits of the current branch.

 

git remote add upstream <upstream repo> # Add a different upstream repo  to your local repo. Not sure if upstream is a given name or git function.

git fetch --all # retrieves available remote branches.

git checkout -b <new branch> upstream/master #  create a new branch from a different one.

git push -u origin <new branch> # pushes a branch remote

 

Remote commands. Impacts your remote repository.

git push --delete origin # deletes remote tags. See local tagging above.

Fixing/patching things

git cherry-pick <commit> # [local] import commits from other branches. Make sure you checkout to the destination branch first (see above).

git reset HEAD #  [local] revert current branch to last commit status.

git reset HEAD~1 --hard # [local] revert last commit. Follow this by a "git push -f" to push remote, if required. Combine with "git log". Note, this also deletes local changes.  

git reset HEAD~1 --soft # [local] revert last commit. Follow this by a "git push -f" to push remote, if required. Combine with "git log". Note, this keeps local changes.

git reset --hard <commit> # [local] reverts to this commit. All commits after this one will be dropped. Follow this by a "git push -f" to push remote, if required. Combine with "git log". Note, this keeps local changes.

git rebase -i <older commit> # after a git log. Allows you to play with all future commits (drop, squash, fix, reorder, edit etc.). git push -f afterwards.

 

Sources:

https://www.devroom.io/2010/06/10/cherry-picking-specific-commits-from-another-branch/

https://www.git-tower.com/learn/git/faq/undo-last-commit

http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html

https://medium.com/@igor_marques/combining-two-commits-84281f470ee8