Skip to content

Instantly share code, notes, and snippets.

@pmgupte
Last active August 20, 2019 11:23
Show Gist options
  • Save pmgupte/6386692 to your computer and use it in GitHub Desktop.
Save pmgupte/6386692 to your computer and use it in GitHub Desktop.
List of commonly used git commands. I would update this list as I encounter other situations.
  1. get your repositiory
  2. checkout and create your local branch
  3. initialize submodules
  4. update submodules, download the code
  5. add files to git
  6. commit file(s)
  7. push your changes to remote branch
  8. merge develop branch into your private branch
  9. resolve conflict
  10. named stash
  11. see all commits on particular file
  12. to see all the branches
  13. To unstage the files staged for commit
  14. list files to be pushed
  15. rename local branch
  16. undo a local commit
  17. merge one branch into another
  18. create a new branch
  19. delete a branch
  20. submodules init/update
  21. compare versions of same file from two different branches
  22. change default editor for commit message to vim
  23. if you encounter "fatal: unable to access 'https:URL': Peer's Certificate issuer is not recognized"
  24. get all files that have been modified in branch
  25. determine URL from where local repo was cloned from
  26. change message of pushed commit
  27. merge an upstream repository into your fork

1. get your repositiory

git clone <your repository url>

2. checkout and create your local branch

cd <your project directory>
git checkout -b local-branch origin/develop

3. initialize submodules

git submodule init

4. update submodules, download the code

git submodule update

5. add files to git

git add <path to file>

6 commit file(s)

git commit <path to file(s)>

e.g. git commit example.php gui/anotherfile.php

7. push your changes to remote branch

git push origin local-branch:remote-branch

8. merge develop branch into your private branch

git pull --rebase
git merge origin/develop
git push origin local-branch:remote-branch

9. resolve conflict

git mergetool path/to/file

10. named stash

10.1 to save a stash with name

git stash save "WIP on X"

10.2 to list all the stashes

git stash list

This would output something like this: stash@{n} ... "WIP on X" stash@{m} ... "some other message" stash@{l} ... "some other name"

10.3 to apply a particular stash

git stash apply stash@{n}

10.4 to see what all has changed for a particular stash

git diff stash@{n}

11. see all commits on particular file

git log path/to/file

12. to see all the branches

12.1 list local branches

git branch

Git will put a * before name of the branch you are currently on.

12.2 list remote branches

git branch -r

12.3 list local branches and their tracking remote branches

git remote show origin

or

git -vv

13. To unstage the files staged for commit

`git reset --hard ` OR `git reset HEAD `

If you execute only this: git reset --hard It would unstage all files staged for commit.

14. list files to be pushed

`git diff --stat ` e.g. git diff --stat origin/myRemoveBranch

15. rename local branch

`git branch -m `

16. undo a local commit

`git reset --soft HEAD^`

A this point, file(s) would remain in staged for commit. To unstage them, refer point 13.

17. merge one branch into another

git checkout <destination branch>
git merge <source branch>

All new commits from source branch should be available on destination branch, so just push to your remote destination branch.

18. create a new branch

Create branch on your local machine: `git branch `

Push this branch to remote: git push origin <new branch name>

Switch to your new branch: git checkout <new branch name>

Add new remote to your branch: git remote add <name of remote> <url>

19. delete a branch

Delete branch on your local machine: `git branch -d `

Delete her remote tracking branch: git push origin :<branch name>

20. submodules init/update

Init submodules: `git submodule init `

Update submodules: git submodule update <submodule name>

In some cases you may face conflicts while updating submodules, and because of them the update gets aborted. In such a case, use --force option: git submodule update <submodule name> --force

21. compare versions of same file from two different branches

git diff <branch 1> <branch 2> -- <relative path to file>

22. change default editor for commit message to vim

git config --global core.editor vim

23. fatal: unable to access 'https:URL': Peer's Certificate issuer is not recognized

If you encounter "fatal: unable to access 'https:URL': Peer's Certificate issuer is not recognized" while cloning your remote repository, you are missing GIT_SSL_NO_VERIFY environmental variable.

Make sure you have valid id_rsa and id_rsa.pub files in your .ssh folder.

export GIT_SSL_NO_VERIFY=true

24. get all files that have been modified in branch

If you want to find out which all files have been modified in a particular branch, use this command.

git diff --name-only <branch in question> $(git merge-base <branch in question> <branch where you would merge back this branch>)

If your shell does not understand $(), you can use back-ticks instead.

25. determine URL from where local repo was cloned from

If you want to find out the URL from where your local repository was cloned from, use this command.

git config --get remote.origin.url

26. change message of pushed commit

If you need to change/update message of any pushed commit, do following.

git rebase -i <commit you wish to change>^

This will open some editor for you (e.g. vim), which will have list of commit(s) along with actions for them. Default action is pick. Replace pick by reword. Then save it (e.g. :wq in vim).

For each commit you are going to change, it will open another editor window with current commit message. Change it the way you want and save+quit.

Finally, forcibly push all those changes. git push origin --force.

27. merge an upstream repository into your fork

git checkout <branch you wish to merge to>

git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git <branch you wish to merge>

Resolve merge conflicts, if any.

git push origin <branch you wish to merge to>

@pmgupte
Copy link
Author

pmgupte commented Aug 20, 2019

Added point #27. merge an upstream repository into your fork.

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