DEV Community

Andrew Garcia, PhD
Andrew Garcia, PhD

Posted on • Updated on

 

TLDR useful Git commands

I recommend printing this post and/or having it handy on another device while working on Git on your PC.

Project Dev. Sequence

This is a useful and common command sequence used to pass changes to a Github/Git project:

git status
git diff 
git add .   
git commit -m "changes to all files" 
git push
Enter fullscreen mode Exit fullscreen mode

Short explanations to each of them:

git status inspect / check the files that have been changed in the working directory
git diff [filename] check the specific changes made on the selected, changed file. Useful for writing the commit message.
git add [filename] stage changes to the selected file. This command adds the change to the staging area
git add . stage changes to all changed files
git commit -m "[commit message]" pass changes to the local repository (i.e. to the folder in your local computer)
git push push these passed changes to the server (the Github page / internet)

Create a Repository

Locally

In computer:

  • Make a folder, name it with your desired repository name, add stuff to it.
  • Open terminal in that folder and type git init.
  • Keep the terminal open.

In Github:

  • Click on "+" sign on top-right corner of / New Repository...
  • Add info on that page and then click on "Create Repository" green button.
  • Copy-paste manual guidelines on next page (starts with "echo README" I think) to open terminal from above.
  • git push. You're done!

Easier way

In Github:

  • Click on "+" sign on top-right corner of / New Repository...
  • Add info on that page and then click on "Create Repository" green button.
  • ... Ignore next page and go to the next option.
  • While in new repository, click on green "Code" button and copy https/ssh address (I typically copy the ssh one).

In computer:

  • Open terminal and type git clone [copied-address-from-GitHub]. You're done!

Branches

Basic commands

# create a new branch
git checkout -b [your-new-branch-name]

# check the names of all your branches
git branch

# go to a particular branch
git checkout [your-branch-name]

# transfer all information from a "source" branch to a "sink" branch
git checkout [sink-branch-name]
git merge [source-branch-name]

# delete branch locally (computer)
git branch -d [your-branch-name]

# delete branch remotely ("on GitHub")
git push origin --delete [your-branch-name]
Enter fullscreen mode Exit fullscreen mode

Rename branch

# checkout to master branch
git checkout main
# local 
git branch -m [old-branch-name] [new-branch-name]
# remote (Github)
git push origin [new-branch-name]
git push origin --delete [old-branch-name]
Enter fullscreen mode Exit fullscreen mode

Undoing Stuff

Revert modified files not yet staged for commit

git restore [filename]  
Enter fullscreen mode Exit fullscreen mode

Let's say you have a file with code that you had previously pushed and you made changes to the code; this command does an UNDO on those changes made.

Undo a commit

On your Github repository, Click on the commits counter below the Code button
Image description

Let's say I want to undo the first commit, then I would copy its hash (95f0e33) and run the following command:

git revert 95f0e33 
Enter fullscreen mode Exit fullscreen mode

Temporarily revert to previous commit

git checkout [commit_ID]  #go to the former code commit / version
git checkout master #go back to current version
Enter fullscreen mode Exit fullscreen mode

Remove files listed on .gitignore which have been previously committed (i.e. "not gitignored")

git rm -r --cached .;
git add .;
git commit -m "gitignored files deleted from remote!"
Enter fullscreen mode Exit fullscreen mode

Make Git Remember Your Username and Password

Watch this video around the 3:29 timestamp
Generating an SSH Key 3:29

Adapt SSH key to repositories

'''new repository'''
git clone git@github.com:[user]/[repository].git

'''or if already created repository'''
cat .git/config  #displays local information of repository
git remote set-url origin git@github.com:[user]/[repository].git  #this is the command which sets  the key to your local repository.
Enter fullscreen mode Exit fullscreen mode

Find all folders which are linked to Github repositories in a specific folder (Linux users)

While in folder to search, you may open a Terminal and type the following:

find -name ".git"  #simple
'''or'''
find -name ".git" -print0 | sort -z | xargs -0 cat #in alphabetical order (may take long to compile IF several git repositories synced)

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
hima_khaitan profile image
Himanshu Khaitan

Informative

Collapse
 
andrewrgarcia profile image
Andrew Garcia, PhD

thank you Himanshu