Why GIT commands?
When you use GIT long enough, you begin to realize it is just easier to use GIT commands. Using a console is faster in every way because UI for some reason or another is laggy and sluggish. I don’t understand why, but it is. There are a lot of ways to do this, but in my opinion using GIT BASH is the easiest. You can do it via PowerShell and a variety of other ways – honestly they are all the same, the only thing that changes is what is available to you via the shell you are using. Things like how you can copy and paste your commands and colors. I like the way GIT BASH colors everything, not crazy about how copy and paste works – but it’s not a big deal.
Very basic GIT commands
There are a number of commands you will need that are fairly common to use, but easy to forget if you aren’t doing this 24/7. Here are some commands, I will probably be updating it as I go.
How to clone a repository
Honestly this depends on your target, if it is GitHub the following command is just fine. If you are working off of TFS you are better off using Visual Studio for the sake of simplicity.
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY //Folder will be named after repo name git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY NameForFolder //You can use a custom name if you want git clone https://tfs.domain.com/collection/_git/RepositoryName //This is an example using TFS
How to delete a local branch
git branch -d features/41_myFeature
How to force delete a local branch when there are pending changes
git branch -D features/41_myFeature
Fetch from all remotes
git fetch --all
Pull from all remotes
This will only pull for your current branch, don’t get confused with the “–all” portion.
git pull --all
List local branches
This shows you what local branches you have and puts an asterisk next to the branch you are currently pointing to.
git branch
List remote branches
Displays all of the remote branches with their commit Ids. Useful for when you don’t know what branches are available.
git ls-remote
Switch branches
Switch to a local branch named master
git checkout master
Prune all local references of remote branches
This can clean up all mucky old references and removes branches that don’t exist anymore
git remote prune origin
Merge the remote master branch into your current local branch
git merge remotes/origin/master
How to create a new branch
Assuming you are on a branch you didn’t want to make changes on such as master
You can branch off of your current branch into a new branch along with your changes
git checkout -b features/41_myFeature
How to stage all files?
git add -A
How to unstage all files?
If you messed up and now you need to unstage everything to fix something before staging again.
git reset HEAD --
How to commit staged files?
git commit
After you execute this command you should be prompted to enter a comment. If you don’t enter a comment your commit will be aborted.
How to push your commits?
git push
If you created the branch locally and never established your branch on the remote then this will fail. You will get a message stating that you didn’t set the “upstream” branch or path.
If your upstream is not set then use the following format:
git push --set-upstream origin topicBranchAkaYourBranchNameHere