DEV Community

Pooja Sanap
Pooja Sanap

Posted on

Git Cheat sheet

Git is a version control (VCS) system for tracking changes to projects. These projects can be large-scale programs like the Linux kernel, but they can also be smaller scale projects like your own R development, homework assignments, papers, or thesis.

git logo

Using Git

  • to add name
git config --global user.name yourname
Enter fullscreen mode Exit fullscreen mode
  • to add email
git config --global user.email youremail@yourdomain.com
Enter fullscreen mode Exit fullscreen mode
  • to check name
git config --global user.name
Enter fullscreen mode Exit fullscreen mode
  • to check email
git config --global user.email
Enter fullscreen mode Exit fullscreen mode
  • to initialize empty git repository
git init
Enter fullscreen mode Exit fullscreen mode
  • to show hidden files
ls - lart
Enter fullscreen mode Exit fullscreen mode
  • to create blank files
touch file_name
Enter fullscreen mode Exit fullscreen mode
  • to check status of file
git status
Enter fullscreen mode Exit fullscreen mode
  • to commit file
git commit
Enter fullscreen mode Exit fullscreen mode
  • to commit all files
git commit -a
Enter fullscreen mode Exit fullscreen mode
  • to add file in current repository
git add file_name
Enter fullscreen mode Exit fullscreen mode
  • to add all files
git add -A
Enter fullscreen mode Exit fullscreen mode

-to add commit message

git commit -m "MESSAGE_HERE"
Enter fullscreen mode Exit fullscreen mode
  • to match with last commit, i.e., to recover losses
git checkout file_name
Enter fullscreen mode Exit fullscreen mode
  • to match all files with the last git commit
git checkout -f
Enter fullscreen mode Exit fullscreen mode
  • to check all commits
git log 
Enter fullscreen mode Exit fullscreen mode
  • to check last 'n' commits (n should be a number)
git log -p -n
Enter fullscreen mode Exit fullscreen mode
  • to get difference between working file and staged file
git diff
Enter fullscreen mode Exit fullscreen mode
  • to get a list of the version history for the current branch.
ls
Enter fullscreen mode Exit fullscreen mode
  • to directly commit files (skip staging area)
git commit -a -m "MESSAGE_HERE
Enter fullscreen mode Exit fullscreen mode
  • to delete files
git rm file_name
Enter fullscreen mode Exit fullscreen mode
  • to remove file from staging area
git rm --cached file_name
Enter fullscreen mode Exit fullscreen mode
  • to get summarized status
git status -s
Enter fullscreen mode Exit fullscreen mode
  • to create ignore files
touch .gitignore
Enter fullscreen mode Exit fullscreen mode
  • to add new branches to git
git branch branch_name
Enter fullscreen mode Exit fullscreen mode
  • to switch between branches
git checkout branch_name
Enter fullscreen mode Exit fullscreen mode
  • to restore branch into master branch
git checkout master
Enter fullscreen mode Exit fullscreen mode
  • to merge branches into master branch (user must be in master)
git merge branch_name
Enter fullscreen mode Exit fullscreen mode
  • to create a branch and directly switch into it
git checkout -b branch_name
Enter fullscreen mode Exit fullscreen mode
  • to add remote git repository
git remote add URL_HERE
Enter fullscreen mode Exit fullscreen mode
  • to push branch into remote repository
git push repository_name branch_name
Enter fullscreen mode Exit fullscreen mode
  • to clone the repository into local setup from a URL
git clone URL_HERE
Enter fullscreen mode Exit fullscreen mode
  • to get remote URL from where the repository was cloned
git config --get remote.origin.url
Enter fullscreen mode Exit fullscreen mode
  • to unstage the file, but preserve the file contents
git reset file_name
Enter fullscreen mode Exit fullscreen mode
  • to undo all the commits after the specified commit and preserve the changes locally
git reset [commit]
Enter fullscreen mode Exit fullscreen mode
  • to discard all history and goes back to the specified commit
git reset –hard [commit]
Enter fullscreen mode Exit fullscreen mode
  • to show the metadata and content changes of the specified commit
git show [commit]
Enter fullscreen mode Exit fullscreen mode

Hope you find it helpful and informative!

Do follow for more such blogs
Feel free to comment down your thoughts or suggestions if any.

Top comments (0)