Skip to content

List of some useful Git commands to get started with Git and Github.

Notifications You must be signed in to change notification settings

KashyapB02/Useful-Git-Commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Git Commands

1. Setup user name and user email

  • User name : git config --global user.name "user name"
  • User email : git config --global user.email "userEmailID"

2. View user name and user email

  • User name : git config --global user.name
  • User email : git config --global user.email

3. Cache login credentials (It helps to avoid re-typing the username and password everytime you perform a commit)

  • git config --global credential.helper cache

4. Obtain a repository from an existing URL

  • git clone URLofRepository

5. Initialize an empty git repository

  • Initialize git in existing folder : git init
  • Initialize git with new folder : git init folderName

6. Adding files to staging area

  • Adding a single file : git add fileName
  • Adding all files : git add .

7. Check repository status

  • git status

8. Commit changes with commit message

  • Single line message : git commit -m "Short summery about commit"
  • Complete commit message : git commit

9. View commit history with changes

  • List of newest commits : git log
  • Detailed changes of each file : git log -p

10. View a particular commit

  • git show commitID
  • git show commitHash

11. View changes before commiting

  • Unstaged changes : git diff
  • Staged changes : git diff --staged
  • Changes of a specific file : git diff fileName
  • Differences between two commit ids : git diff commitId1 commitID2

12. Remove tracked files from the current working tree

  • Remove a single file : git rm fileName or git rm directoryName/fileName
  • Remove files with a particular extension : git rm *.extensionName or git rm directoryName/*.extensionName
  • Remove file only from VCS : git rm --cached fileName or git rm directoryName/fileName

Note : First two commands will also remove the files from the local system.

13. Rename files

  • git mv oldFileName newFileName

14. Move files

  • git mv sourceDirectoryName/fileName destinationDirectoryName

Note : Executing the above command will move the source into the destination directory. The source must exists and can be a file, or a directory and the destination must be an existing directory.

15. Revert unstaged changes staged changes

  • Restore a unstaged working tree files : git checkout fileName
  • Restore a staged working tree file : git reset HEAD fileName
  • Restore all staged files : git reset HEAD

16. Amend the most recent commit

  • Update the most recent commit : git commit --amend -m "Updated message for the most recent commit."
  • Commit more files to most recent commit : git add fileName then git commit --amend --no-edit

17. Rollback last commit

  • git revert HEAD

18. Rollback a particular commit

  • git revert commitID
  • git revert commitHash

19. Undo all the commits

  • Undo but preserve the changes locally : git reset commitID or git reset commitHash
  • Discard all the history and changes back to the specific commit : git reset --hard commitID or git reset --hard commitHash

20. Creat and switch to a new branch

  • Only create a new branch : git branch newbranchName
  • Create and switch to a new branch : git checkout -b newBrachName

21. View list of all branches

  • List all branch : git branch
  • List all remote branches : git branch -a

22. Delete branch

  • Normally delete a branch : git branch -d branchName
  • Forcibly delete a branch : git branch -D branchName
  • Delete a remote branch : git push origin --delete branchName

23. Merge two branches

  • Normally merge a branch : git merge -d branchName
  • Merge a branch with a merge commit : git merge --no-ff branchName

24. Show commit log as graph for current or all branches

  • Commit log of current branch : git log --graph --oneline --decorate
  • Commit log of all branches : git log --all --graph --oneline --decorate

Note : The --graph option will draw an ASCII graph, which represents the branch structure of the commit history. When it used in association with the --oneline and --decorate flags, it makes it easier to identify which commit belongs to which branch.

25. Abort a conflicting merge

  • git merge --abort

26. Add a remote repository

  • Origin repositories : git remote add origin URLofRepository
  • Upstream repositories : git remote add upstream URLofRepository

27. View remote URLs

  • git remote -v

28. Get additional information about a remote repository

  • Origin repositories : git remote show origin
  • Upstream repositories : git remote show upstream

29. Push changes to a remote repository

  • git push origin branchNametobePushed

30. Pull changes from a remote repository

  • git pull
  • git pull --verbose

31. Merge remote repository with local repository

  • git merge origin

32. Push a new branch to remote repository

  • git push -u origin newBranchName

33. Remove a remote branch

  • git push --delete origin branchName

34. Use rebase

  • git rebase branchName

Note : The above command will change the base of your branch from one commit to another, which will make it appear as if you have created your branch from a different commit.

35. List all the ignored files within the project

  • git ls-files --others --ignored --exclude-standard

36. Temporarily store all the modified tracked files

  • git stash

37. Restore the most recently stashed file

  • git stash pop

38. List all the stashed changesets

  • git stash list

39. Discard the most recently stashed changeset

  • git stash drop

40. Remove untracked files

  • Remove untracked files : git clean -f
  • Remove untracked files/directories : git clean -fd
  • List all files/directories that would be removed : git clean -nfd

41. Listing the dates and the author of the changed file

  • git blame fileName

About

List of some useful Git commands to get started with Git and Github.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published