Useful Git Commands

Useful Git Commands

I figured the best way to start is to get (git) us on the same page (pun intended)
Before I start showing you code for automation, I figure I should show you the very basics that you need to know about using Git. I hope you don't mind, as I won't be wasting valuable screen space on how to install Git. If you run into trouble installing Git, don't worry, you are also going to run into trouble with automation😃 Rest assured, as we all struggle at some point with automation, but finding a solution will bring you pride and joy in your accomplishments. To be successful with automation, you will have to continue to learn new technologies and tools. All joking aside, you will actually find many good resources online and offline that will help you get unstuck.

I want to make it very clear that running any of these commands without the knowledge of what they do can be very dangerous. As such, following license applies:
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.

Create/Initialize

You actually use init to create a git repo. It is best to do this in an empty directory, as you normally want the first change to be a readme.md file, but this can be done in a directory with files already present. Also you can rerun this command in a directory safely, meaning your current work will not be affected.
git init
Initialize the repo without working copy, useful if you are setting up a central server, see additional info. To be honest, I only used this once, but thought it might be useful for someone.
git init --bare

Status

Used to see the current status of the branch. Status shows you current branch you are on and any changes that you might have made since last commit. If you are setup to track a remote branch, status will tell you how far ahead or behind you are against the remote branch. It will also show you what changes you have staged or not staged for commit.
git status

Checkout

Checkout the current master branch.
git checkout master
Create a new branch with name branchName, based on what you currently have checked out and changed. You normally do this because you forgot to create a branch before you started to change files.
git checkout -b branchName

Commit

Basic commit syntax, if setup correctly, your editor will open and ask for commit message
git commit
Add commit message inline without opening editor
git commit -m "my message"
Stage all files for commit and add commit message
git commit -am "my message"

Add

Stage all files for commit.
git add .
Stage specific files for commit.
git add filename1 filename2 filename3

Remote

If you need to add a remote repository, use this command. This is actually useful when you have to fetch from multiple remote repositories.
git remote add origin "https://url"
You can change origin to any name. Then you can do the following.
git remote add ns "https://url"
git fetch ns master
If you want to see what remotes you have setup just do the following
git remote -v

Rebase

Pull gets latest from remote repository and rebase reapplies your local changes on the HEAD of the branch. It is very possible you will have to deal with merge conflicts when you run this.
git pull --rebase

Reset

Reset current branch to origin/master, but keep all changes you made. This is useful if you want to clean up the commit message or remove a few changes.
git reset origin/master --soft
You can also reset to a particular change # or branch.
git reset ###### --soft
Reset current branch to origin/master, but do not keep changes you made since then. Can be very dangerous as this wipes out what you currently have been working on.
git reset origin/master --hard

Cherry-Pick

Apply one commit from another branch to current branch. Where ###### is a commit id.
git cherry-pick ######

Merge

Merge origin/branch into the current branch.
git merge origin/branch

Push

Upload changes to the remote repo
git push
Upload changes to the remote with alias origin
git push origin
Upload changes to the remote with alias origin and create a remote branch with tracking
git push origin -u branchName

Pull

Pull gets latest from remote repo and rebase reapplies your local changes on the HEAD of the branch.
git pull --rebase
Pull from remote named origin and branch named master and reapply current changes on our currently checked out branch
git pull origin master --rebase

Log

Show commits of the current branch, type q to quit. The log command is pretty extensive, so see documentation for all the possible options. Also see examples.
git log
Show last 2 commits. You can change 2, to another number to get that amount of commit messages you want displayed.
git log -2

Diff

Compare current non-committed changes to committed changes on current branch.
git diff
Compare only for this file
git diff filename
Compare current branch to origin/master.
git diff origin/master

Conclusion

These git commands are good to start with. There are many more commands at your disposal. The commands in the article should get you by in most of your day-to-day activities. If I find people are interested, I will add more in depth git examples.

Comments

Popular posts from this blog

Azure DevOps - Auto Approve PR with PAT

Ubuntu 18.04 - Install Android Emulator

Ansible Module - VMWare Update Guest PCI Device