Basic/Advanced Git commands

Git – version control, the best thing since sliced bread. Right? I can’t imagine my life without a version control system in place. I know millions work with Git these days so decided to write up a simple post on some useful commands.

I won’t go into how it all works, well maybe just a little bit. So basically git tracks references to changes from the main branch such as ‘master’.

List of basic commands:

git diff # show diff of all changed and uncommitted files.

git add <file> # Stage changes from a file.

git diff --cached # Show diff of all staged files.

git checkout <file> # Revert back to original content from the branch you are on.

git branch # show list of branches available locally.

git branch <branch name> # Create a branch

git branch -d <branch name # Delete a local branch (safe)

git branch -D <branch name # Delete a local branch (unsafe)

git checkout <branch> # Switch to another branch.

git commit -m <commit msg> # Commit your changes to the branch.

git reset <file> # Reset/unstage change.

git push # push changes to the remote repo. (create branch if not already exists)

git pull # pull changes from the remote repo.

git merge <branch> # Merge a branch into the branch you’re on.

git tag <tag version> # Create a hard reference to your commit hash represented by a tag.

git push --tags # Push all tags to remote repository.

More advanced commands:

git checkout <branch> <file> # Checkout changes for file from another branch.

git reset --hard HEAD~1 # Revert a local merge. (if its the last thing you’ve done)

git push origin <branch> --force # Force re-write of commit history as per your local version.

git rebase <commit hash> # Rewrite commit history up until the commit hash provided.

git rebase --abort|--skip # Abort or skip the rebase change.

git branch -u origin/<branch> # Set tracking to a remote branch. (if exits)

git commit -am "<commit msg"> # Add files and commit changes in one go.

git stash # Stash uncommitted changes temporarily.

git stash apply # Re-apply the stashed changes.

git stash list # Show list of stashed changes.

git diff <branch> <file> # Show diff of file against another branch.

There are lots more! But I have found that these cover the bulk of my work!

Leave a Reply

Your email address will not be published. Required fields are marked *