Common git problems cheat sheet
Table of Contents
- GitHub Cheat Sheet
- Common git problems cheat sheet
- Table of Contents
- Create a new repository
- Create a new repository FROM THE COMMAND LINE
- Subsequent adds, commits, pushes
- Open repo on github browser
- List all repositories
- Tips
- Commit and push to GitHub
- Delete all local git files
- Clone a repository
- Pull remote changes from GitHub to local
- Create a new branch
- Switch to a branch
- See all branches
- Delete a branch
- Merge a branch
- See all commits
- See all commits in a branch
- HARD Reset to a commit
- HARD Reset to a commit, delete all local changes
- Show
- Revert
- Resolve merge conflicts
- Display description and README.md of a repository
- Get how many commits a day in a repo
- Get how many commits a month in a repo
- Get how many commits a year in a repo
- Contribute to an Open Source project
- Create a new feature in a repository that you own
- Create a new feature in a repository that you do not own
mkdirorcdinto the root directory of your projectgit initgit add .git commit -m "first commit"- Go to GitHub and create a new repository with name and description, then:
git remote add origin https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.git git push -u origin main
git initgit add .git commit -m "first commit"gh repo create REPO-NAME --public -d 'description'git remote add origin https://github.com/emanuelefavero/REPO-NAME.gitgit push -u origin main
git add .git commit -m "commit message"git push
gh browse
gh repo list -L 200
- Create a README.md file in the root of the repository
- Create a .gitignore file in the root of the repository
- Create a LICENSE file in the root of the repository
git add .git commit -m "commit message"git push -u origin main
rm -rf .git
git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.gitORgh repo clone YOUR_USERNAME/YOUR_REPOSITORY_NAME
git pull
git branch BRANCH_NAMEOr, to create a new branch and switch to it immediately:git checkout -b BRANCH_NAME
git checkout BRANCH_NAME
git branch
git branch -d BRANCH_NAMEORgit branch --delete BRANCH_NAME
git merge BRANCH_NAME
git log
git log BRANCH_NAME
WARNING: this will delete all commits after the commit you are resetting to
git reset --hard COMMIT_HASH
WARNING: this will delete all commits after the commit you are resetting to and force push to remote. This will delete all commits after the commit you are resetting to on the remote
Don’t use this method if the github repo is being used by multiple users
git reset --hard COMMIT_HASHgit clean -fgit push -f origin main
File changes in directory:
-
git statusChanges in tracked files:
-
git diffHistory;
-
git logWho changed what and when:
-
git blame- Example 1:
git blame README.md - Example 2:
git blame --line-porcelain README.md
Show commit:
- Example 1:
-
git show COMMIT_HASHLocal branches
-
git branch
Undo to last commit
git reset --hard
Undo a commit:
-
git reset --hard HEAD~1Undo two commits:
-
git reset --hard HEAD~2
Revert to specific commit:
git revert COMMIT_HASH
View conflicts:
git diff
View merge conflicts against base file:
git diff --base FILENAME
View merge conflicts against your changes:
git diff --ours FILENAME
View merge conflicts against their changes:
git diff --theirs FILENAME
Discard a conflicting patch:
git reset --hardgit rebase --skip
After resolving conflicts, merge with:
git add FILENAMEgit rebase --continue
gh repo viewgh repo view https://github.com/emanuelefavero/REPO-NAME.git
git log --date=short --pretty=format:%ad | sort | uniq -c
git log --date=short --pretty=format:%am | sort | uniq -c
git log --date=short --pretty=format:%ay | sort | uniq -c
upstreamis the name of the remote repository you forked fromoriginis the name of the remote repository you cloned fromlocalis the name of the local repository
- Fork the repository
- Clone the repository
- Add the original repository as a remote:
git remote add upstream UPSTREAM_URL - Create a new branch for the new feature or fix:
git checkout -b BRANCH_NAME - Make your changes, commit and push to your forked repository and branch
- Pull the latest changes from the original repository:
git pull upstream/main - Be suer to jump back to your branch:
git checkout BRANCH_NAME - Merge main into your branch:
git merge main - Resolve any conflicts
- Make the pull request to the original repository
git push origin BRANCH_NAME - Check the pull request on GitHub
- If the pull request is accepted, delete the branch
For more info: Using git in the real world
Switch back to the main branch and pull the latest changes:
git checkout main
git pullMerge your feature branch into the main branch:
git merge feat/my-new-featurePush the changes to the remote repository:
git push origin mainDelete the feature branch if you no longer need it:
git branch -d feat/my-new-feature
# If you pushed the branch to the remote repository, delete it there too
git push origin --delete feat/my-new-featureCreate a new branch for your feature:
git checkout -b feat/my-new-feature- Work on your feature:
git add .
git commit -m "feat: add my new feature"Push the branch to your forked repository:
git push -u origin feat/my-new-featureCreate a new branch: git checkout -b feat/my-new-feature
Work on your feature:
git add .
git commit -m "feat: add my new feature"
# Optional: push the branch to the remote repository
git push -u origin feat/my-new-featureFork the repository on GitHub
Clone the forked repository to your local machine:
git clone https://github.com/YOUR_USERNAME/REPO_NAME.git
cd REPO_NAMEGo to the original repository on GitHub and create a pull request from your forked repository's branch
Wait for the pull request to be reviewed and merged by the repository owner
If the pull request is accepted, you can delete the branch from your forked repository:
git branch -d feat/my-new-feature
git push origin --delete feat/my-new-featurefrom https://github.com/emanuelefavero/github-cheat-sheet
No comments:
Post a Comment