Keeping your repositories close to the root of your storage drive helps prevent issues with maximum path lengths, especially on Windows systems, where paths longer than 260 characters can cause problems.
Tip
We highly recommend storing your repos in a consistent place.
I typically use C:/git or ~/git
When cloning an empty repository, you may want to set up the default branches.
Here’s a script to create a new repository with trunk and develop branches.
Save the script as
NewRepo.shand place it in the root of the directory your clone your repos to. That way, you can call it from inside your cloned repo with../NewRepo.sh
When cloning a repository, you can use different URL types. Here’s a brief overview.
HTTP URLs are useful when you need to work through a firewall or proxy. However, they require you to enter your credentials each time you push changes unless you use a credential manager.
e.g.
git clone https://github.com/username/repo.git
SSH URLs are preferred for their security and convenience. They use SSH keys for authentication, eliminating the need to enter your credentials for each operation.
Click here for help on setting up SSH
e.g.
git clone git@github.com:username/repo.git
Git URLs are similar to SSH URLs but are less common. They can only be used if both the client and server are on the same network or the server is accessible via a direct connection.
You probably won't use these, but they're listed here for completeness...
e.g.
git clone git://github.com/username/repo.git
Here are some common Git commands you will use on a daily basis.
Clone a repository.
git clone <SSH_URL>Add all changes to the staging area.
git add .Commit changes with a message.
git commit -m "Commit message"Pull the latest changes from the remote repository.
git pullPush your changes to the remote repository.
git pushStage specific files.
git add <file1> <file2>Stage all changes.
git add .Un-stage specific files.
git reset <file1> <file2>Un-stage all changes.
git resetStage file for deletion and delete from working area.
git rm <file1>Rename a file, retaining history tracking, staging that change for the next commit.
git mv <file_name> <new_name>Branches allow you to work on multiple features or fixes simultaneously.
List all local branches
git branchList all branches including remote
git branch -aCreate and switch to a new local branch
git checkout -b <branch-name>Note
The above is shorthand for
git branch <branch-name> && git checkout <branch-name>
Sync your local branch to the remote repository
git push -u origin <branch-name>Note
The above is shorthand for
git push --set-upstream origin <branch-name>
Merge a branch into the current branch
git merge <branch-name>Delete a branch
git branch -d <branch-name>Pull Requests are used to review and merge changes from one branch to another, typically from a feature branch to develop, or develop to trunk.
- Push your branch to the remote repository.
- Create a Pull Request on the hosting platform (e.g., GitHub, GitLab).
- Review the changes and merge the PR after approval.
Submodules allow you to include other Git repositories within a repository.
Add a submodule
git submodule add <repo-url> <path>Initialise and update submodules
git submodule update --initTags are used to mark specific points in your repository’s history, typically for releases.
Create a lightweight tag
git tag <tag-name>Create an annotated tag
git tag -a <tag-name> -m "Tag message"Push tags to the remote repository
git push --tagsTags can be used for versioning, and git describe can show the most recent tag reachable from a commit.
git describe --tags --long --dirty