DEV Community

castnutt
castnutt

Posted on

Git basics - cheatsheet

Git general knowledge

  • Git is a distributed version control system that tracks changes in source code. It helps enabling collaboration and history management for projects.

  • The name "origin" is a shorthand name for the remote repository that a project was originally cloned from. It is a standard convention used instead of that original repository's URL, making referencing much easier.

  • A .gitignore file is used to exclude files in the repository. These files wont be part of a commit. Used with personal, configuration or library files.

  • It is best practises to use branches for your own development and merge later instead of committing directly to main.

  • Default branch for a repository is called main. Before it was called master, but that has now been deprecated (can be found in old repositories)

Useful sequences

Standard dev sequence

  1. Checkout the main branch and pull any updates so that your local copy of the repository is up to date.

      git checkout main
      git pull
    
  2. Create a new branch and start development

      git checkout -b new_dev_branch
    
  3. Add any modified or new files into a stage so that they can be committed.

      git add new_files
    
  4. Commit files and push to the remote repository

      git commit -m 'commit message' 
      git push
    

Merge with/without conflicts

  1. Rebase your branch to main - download any updates from main so that your branch is up to date
git fetch origin/main
git merge origin/main
Enter fullscreen mode Exit fullscreen mode
  • If no conflicts, then push the changes
git push
Enter fullscreen mode Exit fullscreen mode
  • If conflicts are found, then resolve the conflicts and push the changes
git add any_files
git commit -m 'updating my branch'
git push
Enter fullscreen mode Exit fullscreen mode

Top used git commands

git config

  • Set username and email for global config
git config --global user.name "username"
git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode
  • Set username and email for local config
git config --local user.name "username"
git config --local user.email "email"
Enter fullscreen mode Exit fullscreen mode

git pull

  • Get the latest version of the repository for the current active branch
 git pull
Enter fullscreen mode Exit fullscreen mode
  • Get the latest version of the repository for a specific branch (main in this case)
git pull origin main 
Enter fullscreen mode Exit fullscreen mode

git checkout

  • Create a branch for work
git checkout -b work_branch
Enter fullscreen mode Exit fullscreen mode
  • Change to a different branch (main for the default repository branch)
git checkout main
Enter fullscreen mode Exit fullscreen mode
  • Get just a single file from another branch (if origin/main used, then it will reset the file status to the repository's main branch)
git checkout origin/master -- filename
Enter fullscreen mode Exit fullscreen mode

git branch

  • Delete branch locally
git branch -d work_branch
Enter fullscreen mode Exit fullscreen mode
  • Delete branch remotely
git push origin --delete work_branch
Enter fullscreen mode Exit fullscreen mode
  • Rename branch from main
git branch -m old_name new_name
Enter fullscreen mode Exit fullscreen mode
  • Rename branch from the same branch
git branch -m new_name
Enter fullscreen mode Exit fullscreen mode
  • Rename the local branch to the new name
git branch -m $old_name $new_name
Enter fullscreen mode Exit fullscreen mode
  • Delete the old branch on remote
git push $remote --delete $old_name
Enter fullscreen mode Exit fullscreen mode
  • Prevent git from using the old name when pushing in the next step
git branch --unset-upstream $new_name
Enter fullscreen mode Exit fullscreen mode
  • Push the new branch to remote
git push $remote $new_name
Enter fullscreen mode Exit fullscreen mode
  • Reset the upstream branch for the new_name local branch
git push $remote -u $new_name
Enter fullscreen mode Exit fullscreen mode

git status

  • See the status of the repository. Which branch you are in, what files have been modified and what files have been created ad new.
git status
Enter fullscreen mode Exit fullscreen mode

git add

  • Stage all changes
git add . --all
Enter fullscreen mode Exit fullscreen mode

git diff

  • Differences between commits
git diff --name-only commit_hash commit_hash
Enter fullscreen mode Exit fullscreen mode
  • See changes between branches (diff)
git diff master...branch
Enter fullscreen mode Exit fullscreen mode
  • Export the differences between two branches into an external file.
git diff branch1..branch2 -- file.txt
Enter fullscreen mode Exit fullscreen mode

git reset

  • Discard local changes as only indexed files
git reset –hard
Enter fullscreen mode Exit fullscreen mode
  • Discard all the file
git clean -fxd
Enter fullscreen mode Exit fullscreen mode
  • Reset branch to the same status as origin
git fetch origin
git reset --hard origin/main 
Enter fullscreen mode Exit fullscreen mode

git commit

  • Commit changes
git commit -m 'message'
Enter fullscreen mode Exit fullscreen mode

git log

  • Commit history/log
git log
Enter fullscreen mode Exit fullscreen mode
  • Summarized log to one line
git log --oneline
Enter fullscreen mode Exit fullscreen mode
  • List the modified file names in each commit
git log --name-only
Enter fullscreen mode Exit fullscreen mode
  • Compact summary with commit message plus filenames with a note whether they are gone
git log --compact-summary
Enter fullscreen mode Exit fullscreen mode

git revert

  • Find a previous commit and revert to it
git log --oneline
git revert to_commit_hash
Enter fullscreen mode Exit fullscreen mode

git push

  • Push changes to repo
git push origin 'work_branch'
Enter fullscreen mode Exit fullscreen mode
  • Push a new branch to remote
git push --set-upstream origin new-branch
Enter fullscreen mode Exit fullscreen mode

git stash

  • Save current changes into a dirty directory (push) and then retrieve them later (pop)
git stash push/pop
Enter fullscreen mode Exit fullscreen mode
  • Stash work with a message
git stash save 'message'
Enter fullscreen mode Exit fullscreen mode
  • Retrieve a specific stash
git stash pop --index n
Enter fullscreen mode Exit fullscreen mode
  • List of all stashes
git stash list
Enter fullscreen mode Exit fullscreen mode
  • Delete specific stash
git stash drop index
Enter fullscreen mode Exit fullscreen mode
  • Delete all stashes
git stash clear
Enter fullscreen mode Exit fullscreen mode

git merge

  • Git merge while resolving conflicts (our changes or their changes)
git merge branch --strategy-option ours/theirs
Enter fullscreen mode Exit fullscreen mode
  • Abort a merge command
git merge --abort
Enter fullscreen mode Exit fullscreen mode

As always, if you think anything is missing, wrong or can be improved, add your comments.

Top comments (0)