Skip to content

aKabra/git_howtos

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 

Repository files navigation

git_howtos

This document is licensed under Apache License, version 2.0 (see http://www.apache.org/licenses/LICENSE-2.0) Use this documentation at your own risk. I am not liable for any loss from the use of this document.

These commands have worked for me for my little git use. I perodically visit this list to refresh my memory.

Git help

  1. Add only modified files. Do not add new files.

git add -u

  1. To revert local add:

git reset HEAD

This will revert all the changes from the commit

  1. Add a specific file

git add RELATIVE_PATH_TO_FILE

  1. Commit changes added with "git add"

git commit -m "COMMENTS ABOUT THIS COMMIT"

  1. To Push commited changes to the remote

git push

  1. Forget a modification to a file

git checkout -f RELATIVE_PATH_TO_FILE

  1. Forget all modification to a branch

git checkout -f HEAD

  1. Create a new branch from another branch:

Switch to the source branch

git checkout SOURCEBRANCHNAME

Create a new branch from the current branch

git branch NEWBRANCHNAME

Switch to the new branch

git checkout NEWBRANCHNAME

Create the NEWBRANCHNAME on the remote

git push -u origin HEAD

  1. Delete remote branch

git push origin :BRANCHNAMETODELETE

  1. Remove stale branches from remotes

git remote prune origin

  1. Merge one branch into another

git merge BRANCHTOMERGE

  1. Setup upstream for a forked repo.

List the current remotes

git remote -v

Output:
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)

Set a new remote

git remote add upstream https://github.com/otheruser/repo.git

Verify new remote

git remote -v

Output:
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
upstream https://github.com/otheruser/repo.git (fetch)
upstream https://github.com/otheruser/repo.git (push)

  1. To checkout a branch that exists on remotes and set tracking do

git fetch
git checkout BRANCHENAME

  1. Delete a local branch

git branch -D BRANCHNAME

  1. Store credentials so that yu do not need to type them in again (helps when two step authentication is used with github):

git config credential.helper store
git pull (or another command that will prompt the password)
Username: type your username here
Password: type your password here

  1. Forget all local commits into the BRANCHNAME branch and replace the branch contents with the contents of remote (for example, if you accidentally merged some branch into the master, but then want undo that and get the original master back):

git checkout BRANCHNAME
git fetch
git reset --hard origin/BRANCHNAME

  1. Housekeeping for a local repo. It often makes the .git folder much smaller. However, it also makes it impossible to undo accidental hard resets.

git gc

  1. Roll back last commit, delete it from history, and forget about all the changes in it.

git reset --hard HEAD~1

  1. You created a local repository and also created an empty remote repository (with url https://github.com/user/repo.git). Now you want to push your local repo to the empty remote, do this:

git remote add origin https://github.com/user/repo.git

Now you can push and pull your local repo to the remote.

  1. Make git to forget about a file or folder (remove it from the git repo, without removing it from the local file system):

git rm --cached file_or_folder_name

The file will appear deleted, but they are deleted only from the index.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published