Reference for some common git commands
- Initialise a new repo (only done once at the beginning of the project)
git init
- Stage changes
git add .
- Commit changes
git commit -m "Initial commit"
- Add remote for remote repository
git remote add origin <github_repo_url>
- Push to remote repo
git push origin master
- Create new Branch and make changes
git checkout -b <new-branch-name>
- Stage changes
git add .
- Commit changes
git commit -m "Initial commit"
- Pull code from github and merge with local code
git pull origin master
- Merge new-branch to master
git merge <new-branch-name>
git merge --no-ff branch-name
# Merge with no fast-forward - Add remote for remote repository
git remote add origin <github_repo_url>
- Push new-brach to github
git push origin <new-branch-name>
git push origin HEAD
#Shortcut to push current branch wiithout specifying name - Go to github and open a new pull request to merge with the master branch.
- Even while working on an individual project, it is useful to open a PR, as github can perform automated checks on the code before approving the PR.
- Switch to existing branch
git checkout branch_name
- Create new branch (relative to current branch) and switch to it
git checkout -b branch_name
- Rename current branch to
new_name
git branch -m new_name
- Delete current branch
git branch -d branch_name
- Create a tag for the current commit a tag number and message
git tag -a v1.4 -m "my version 1.4"
- Create a tag for an old commit/Add additional tag for old tag
git tag -a v1.2 <old_tag/hash> -m "Message here"
- Push tags to remote repo
git push origin --tags
- Delete local tag
git tag -d <tag_name>
- Delete remote tag
git push --delete origin <tag_name>
- Make git automatically convert line endings correctly for your platform. This can be used to prevent uncessary whitespace additons to commits when working on different OSs.
git config --global core.autocrlf true
When using git lfs, if there is a previously failed commit it should be reset, if there is an error even after tracking the file. Sometimes a history of a failed push can cause issues"
Git LFS is used to track and commit large files. The steps are:
- Track a File
-
Track a single file
git lfs track "filename"
-
Track a whole folder
git lfs track "Folder/**"
-
Add the
.gitattributes
file
git add .gitattributes
-
Add the files, commit and push
- View tracked files
git lfs ls-files