Most used Git commands for collaboration

Most used Git commands for collaboration

165Reads
4 September, 2022

Here is the list of most helpful and used Git commands. Use it as a cheatsheet.

<p dir="auto">Show remote:git remote
<p dir="auto">Show remote details:git remote -v
<p dir="auto">Add remote upstream from GitHub project:git remote add upstream https://github.com/user/project.git
<p dir="auto">Add remote upstream from existing empty project on server:git remote add upstream ssh://[email protected]/path/to/repository/.git
<p dir="auto">Fetch:git fetch upstream
<p dir="auto">Fetch a custom branch:git fetch upstream branchname:local_branchname
<p dir="auto">Merge fetched commits:git merge upstream/master
<p dir="auto">Merge automatically accepting the changes made to the current branchgit pull --strategy recursive -X ours origin master
<p dir="auto">Merge automatically accepting the changes made to the current branchgit pull --strategy recursive -X theirs origin master
<p dir="auto">Remove origin:git remote rm origin
<p dir="auto">Show remote branches:git branch -r
<p dir="auto">Show all branches:git branch -a
<p dir="auto">Create and checkout branch from a remote branch:git checkout -b local_branchname upstream/remote_branchname
<p dir="auto">Compare:git diff origin/master..master
<p dir="auto">Push (set default with-u):git push -u origin master
<p dir="auto">Push:git push origin master
<p dir="auto">Force-Push:git push origin master --force
<p dir="auto">Pull:git pull
<p dir="auto">Pull specific branch:git pull origin branchname
<p dir="auto">Fetch a pull request on GitHub by its ID and create a new branch:git fetch upstream pull/ID/head:new-pr-branch
<p dir="auto">Clone to localhost:git clone https://github.com/user/project.gitor:git clone ssh://[email protected]/~/dir/.git
<p dir="auto">Clone to localhost folder:git clone https://github.com/user/project.git ~/dir/folder
<p dir="auto">Clone specific branch to localhost:git clone -b branchname https://github.com/user/project.git
<p dir="auto">Delete remote branch (push nothing):git push origin :branchnameor:git push origin --delete branchname