This is a list of git commands that I frequently use and need to stackoverflow them from time to time. So, recording them here so that I dont need to look them up at all times like now.
I work on windows, but use git bash for everything to do with git.
Clone a remote repository
To clone a remote repository
To clone the libgit2sharp repo from github
This will -
- Create a new folder with the name of the repo
- Clone the repository creating remote tracking branches for each branch in the remote repository
- Checks out the currently active branch in the remote repository.
To clone into an existing folder
To clone libgit2sharp into gitsharp
To checkout a specific branch after clone All the clone commands above will checkout the active branch in the remote repository. In order to checkout a specific remote branch
The following command clones the repo into an existing folder gitsharp and checks out the vNext branch
Search for a branch
I often need to search for a branch by name, there may be nice native git command that I do not know of, but I use plain ol’ grep
Checkout a remote branch
There are a couple of terms here that would be good to clarify.
Branch - A label that we assign to the commit which is at the top of a reachable series of commits.
Local branch - A branch that exists in a local repository.
Remote branch - A branch that exists from a remote repository. Note that remote branches are addressed as [remote repo]/[branch name]
Tracking branch - If we want to work on a remote branch, we need a local version that ‘tracks’ the remote branch. This is a tracking branch.
So, lets see what remote branches are available
To create a tracking branch, checkout and switch to the branch
This command will fail if the branch already exists.
fatal: A branch named 'master' already exists.
to reset or create a new branch use the -B option during checkout
Reset local changes
When you want to discard all the local changes
this will reset the index i.e. remove all the files from the index, but will leave the working directory untouched. Specify ‘–hard’ to reset the working directory as well
Clean working directory
When you want to reset your working area, you might want to remove all untracked files, these are all the files that git does not know about yet, these include local changes not yet staged and all .git ignored files
In this post I have ignored the more often used commands like
git add and
git commit and also some of the more more esoteric ones like
git cat-file. The intention is mostly to record the ones that I need regularly, but not often enough for them to be embedded in my muscle memory.