https://github.com/srivatsa17/git-commands
This repo contains a list of useful git commands along with their examples
https://github.com/srivatsa17/git-commands
git
Last synced: over 1 year ago
JSON representation
This repo contains a list of useful git commands along with their examples
- Host: GitHub
- URL: https://github.com/srivatsa17/git-commands
- Owner: srivatsa17
- Created: 2022-08-02T10:44:50.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-06T07:07:57.000Z (over 3 years ago)
- Last Synced: 2023-07-29T10:49:28.847Z (almost 3 years ago)
- Topics: git
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Git commands
## 1. Configure username and email
**Command**
```
$ git config --global user.name ""
$ git config --global user.email
```
**Example**
```
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
```
## 2. Create a new local repository
**Command**
```
$ git init
```
> Note: Create a folder in your system and run this command to create a local repository.
## 3. Clone a repository
**Command**
```
$ git clone
```
**Example**
```
$ git clone https://github.com/srivatsa17/Git-commands.git
```
## 4. Create a new branch, switch to it and push to remote
**Command**
```
$ git checkout -b
$ git push
```
**Example**
```
$ git checkout -b development master
$ git push origin development
```
## 5. Switch from one branch to another
**Command**
```
$ git checkout
```
**Example**
```
$ git checkout development
```
## 6. List current branch
**Command**
```
$ git branch
```
## 7. List all branches
**Command**
```
$ git branch -a
```
## 8. Push all local branches to remote
**Command**
```
$ git push -all
```
**Example**
```
$ git push -all origin
```
## 9. Delete a particular branch from remote and local
**Command**
```
$ git push -d
$ git branch -d
```
**Example**
```
$ git push -d origin development
$ git branch -d development
```
## 10. Adding a changed file to staging
**Command**
```
$ git add
```
**Example**
```
$ git add README.md
```
## 11. Adding multiple changed files to staging
**Command**
```
$ git add *
```
## 12. Commit changes to head but not to remote
**Command**
```
$ git commit -m "Commit message"
```
**Example**
```
$ git commit -m "First commit"
```
## 13. Push changes to remote
**Command**
```
$ git push
```
**Example**
```
$ git commit origin master
```
> Note: If upstream branch is already configured, just run `$ git push`
## 14. Get status of changed files
**Command**
```
$ git status
```
## 15. Fetch and merge changes on the remote server to your working directory
**Command**
```
$ git pull
```
## 16. Merge a branch into current branch
**Command**
```
$ git merge
```
**Example**
```
$ git merge development
```
> If current branch is master, this will merge branch development into master.
## 17. See the difference between previous and current versions of file modified
**Command**
```
$ git diff
```
## 18. Reset/Undo the files modified to its previous version
**Command**
```
$ git reset
```
## 19. Stash the changes for later use
**Command**
```
$ git stash
```
> This command takes the uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.
## 20. Re-applying stashed changes
**Command**
```
$ git stash pop
```
## 21. Create Tags
**Command**
```
$ git tag
```
**Example**
```
$ git tag 2022.05
```
## 22. List all Tags
**Command**
```
$ git tag -l
```
## 23. List the remote name
**Command**
```
$ git remote
```
## 24. List all the remotes
**Command**
```
$ git remote -v
```
## 25. List the commit logs
**Command**
```
$ git log
```
## 26. Print the commit logs
**Command**
```
$ git log -p
```
## 27. Reverting the commit
**Command**
```
$ git revert
```
**Example**
```
$ git revert d8ed902ffe7c1529d02d9a4857c1f541e2e0ce27
```
## 28. Replace the previous commit message
**Command**
```
$ git commit --amend -m "New commit message"
```
**Example**
```
$ git commit --amend -m "Updated message"
```
## 29. Searching in git
**Command**
```
$ git grep
```
**Example**
```
$ git grep "foo()"
```
## 30. Revert a git pull or move to a specific version of your code
**Command**
```
$ git reflog
$ git reset --hard
```
> `git reflog` will give the list of commit hashID's
> `git reset --hard ` will only make changes in local repo
> `git push --force` is required if changes has to be pushed to remote server
**Example**
```
$ git reset --hard 95377807
```
> This will move our code into previous commit with ID 95377807
## 31. Reset the untracked files
**Command**
```
$ git restore /path/to/folder
```
> `git restore .` will reset all the files to its original state
## 32. Copy a commit from one branch to another without merging of branches.
**Command**
```
$ git checkout
$ git cherry-pick
$ git push
```
**Example**
```
$ git checkout master
$ git cherry-pick a885370
$ git push
```
> Suppose i have branch development with commit hash a885370. I switch to branch master and use cherry pick command.