Skip to content

Instantly share code, notes, and snippets.

@abbas-v1
Last active December 2, 2023 09:33
Show Gist options
  • Select an option

  • Save abbas-v1/bfc9dcc7ab352b496cd2a4816b501811 to your computer and use it in GitHub Desktop.

Select an option

Save abbas-v1/bfc9dcc7ab352b496cd2a4816b501811 to your computer and use it in GitHub Desktop.
Common GIT commands
Clone a remote repository on your local machine
```
git clone <repository_url>
```
List all local branches
```
git branch
```
List all local and remote branches
```
git branch -a
```
List all tags
```
git tag -l
```
Checkout a specific branch, tag, revision, file or a directory
```
git checkout <branch_name>
git checkout tags/<tag_name>
git checkout <revision_sha>
git checkout origin/master -- <filename>
git checkout origin/master -- </directory/path/>
```
Stashing changes
```
git stash list
git stash
git stash pop
git stash drop
```
Ignore changes in a file that is currently being tracked
```
git update-index --assume-unchanged <file_name>
```
Start tracking changes in a file that has been previously ignored
```
git update-index --no-assume-unchanged <filen_name>
```
Revert changes made in a previous commit
```
git revert <revision_sha>
```
Show change history of a file
```
gitk <file_path>
```
Resolve merge conflicts
```
git mergetool
```
If already in conflict state, and want to accept all of theirs
```
git checkout --theirs .
git add .
```
If already in conflict state, and want to accept all of ours
```
git checkout --ours .
git add .
```
Undo the act of committing, leaving everything else intact
```
git reset --soft HEAD^
```
Undo the act of committing and everything you'd staged, but leave the work tree (your files intact)
```
git reset HEAD^
```
Completely undo it, throwing away all uncommitted changes, resetting everything to the previous commit
```
git reset --hard HEAD^
```
Reset local branch to previous commit
```
git reset --hard e3f1e37
```
Hard reset remote branch to previous commit
```
git reset --hard e3f1e37
git push --force origin develop
```
Hard reset to clean untracked files and directories
```
git clean -f -d
```
Files changed in this branch
```
git diff master.. --name-only
```
Cherry-pick commits from one branch into another
```
for commit in $(git log --reverse --pretty=%H --since="2021-05-27T17:19:00" --until="2021-05-30T12:03:00" --all);
do
git cherry-pick $commit
done
```
Get changes from GitHub template
On the other repositories you have to add this template repository as a remote.
git remote add template https://github.com/bit-oasis/template-service.git
Then run git fetch to update the changes
git fetch --all
Then is possible to merge another branch from the new remote to your current one.
git merge template/main --allow-unrelated-histories
Rebase your branch to master branch
git rebase master
git push --force origin your_branch_name
If you have a branch A from master and then another branch B from A and want to connect B to master
git checkout B
git rebase A --onto master
git push --force origin B
@abbas-v1

abbas-v1 commented Apr 1, 2022

Copy link
Copy Markdown
Author

get changes from template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment