Useful Git Commands

Git

I have started learning Git commands off by heart to use in Powershell but still seem to forget some of the less often used ones. 

 

There are various Cheat-Sheets for it, such as this nice looking one from NDP software, but I want just a small list of the ones I genuinely use often.

 

I'm going to create a list of useful git commands here and if it is used I will create a dedicated page for it.

 

 

Basic Commands

 

Initializing a Repository in an Existing Directory – 

git init

Checkout -

git fetch && git checkout TR-810

Add new files

git add .

git add README

Clone repo (not checkout in true DVCS style)

Automatically adds it as a remote called origin

git clone git://github.com/top/top.git 

Check what you have to check in

git status

Commit your code (locally) -

git commit

git commit –m ‘Commit Message’

Add all changes in the working directory to the next commit, including new files and deletions:

git add –A

git commit -am "TR-810: blah blah"

Remove a file

git remove file.ext

Move a file

git mv file_from file_to

Adding a remote repository

git remote add [shortname] [url]

Pull

git pull origin TR-810

Pushing changes back to remote

git push [remote-name] [branch-name]

git push origin TR-810

git push origin master

Merge master

git pull origin master

Branching

Create a new branch starting at the some point in history as the current branch -

git branch BranchName

Note that this creates the new branch, but does not “check it out” (make it the current working branch) 

Switch from the current branch to the indicated branch

git checkout BranchName

Overwriting Local branch with Remote branch (warning, this will remove changes from your working tree!): 

1. Create a new branch in the current position (in case you need your old history):

git branch fubar-pin

2. Update your list of remote branches and sync new commits: 

git fetch --all

3. Reset your branch to the point where origin/branch points to: 

git reset --hard origin/branch

Or - HEAD is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory

 git reset HEAD --hard

Cleaning removes all untracked and ignored files and is a good step before a merge into your branch. This powerful command forces the removal of untracked files and directories 

git clean -xdf

Browse archive

Belfast Skyline

Recent Posts

Browse archive