Git Notes

5
0

Global Configuration

# Enable color console
git config --global color.ui auto

# Set meld as the diff tool
git config --global diff.tool meld

Configuration

# Convert a non-bare repository into a bare one
git config --bool core.bare true

Commit

# To edit last commit log (and possibly change
# the author data if it was changed)
git commit --amend --reset-author

Commit in the past

MYDATE='1999-12-31T23:59:59'
GIT_AUTHOR_DATE=$MYDATE GIT_COMMITTER_DATE=$MYDATE git commit -m 'bla bla bla''

Delete a commit

Revert to the previous commit

git reset --hard HEAD~1

Push the deletion if commit was previously pushed

git push --force

Remove files

# Remove untracked files and directories (not ignored)
git clean -f -d

# Remove untracked files and directories including ignored ones) 
git clean -f -x

# Remove ignored files
git clean -f -X	

Staging

git add				# Stage a file
git add --all		# Also stage deleted files

# Changed or new not ignored. Deleted files not staged
git add . 

# Stage currently tracked files (Not adding files )
git add -u

# Equivalent to all above
git add -A 

# Unstage a file
git reset HEAD <file>

Remove Files

git rm --cached <file> # Remove from git database but not from the filesystem

To revert a single file

git checkout -- src/com/mmonem/erp/server/service/LoginServiceImpl.java

ZZZZZZZZZZZZZZ

git checkout . -f

Branching and Merging

# List all branches:
git branch -a

# List remote branches:
git branch -r

# Switch to a branche
git checkout TheBranche

# Switched to a new branch
git checkout -b NEW_BRANCHE_NAME

# Or:
git branch NEW_BRANCHE_NAME
git checkout NEW_BRANCHE_NAME

# Delete a branch
git branch -d BRANCHE_NAME

Remotes

Show Remotes

git remote -v
git remote show origin

Adding a Remote

git remote add <remote name, e.g. origin)> <url>

Change Remote URL

git remote set-url origin <other URL>

Setting upstream

Verify that the remote is fetched first

git branch --set-upstream-to=origin/master

Pushing

git push --all -u
git push --tags
git push  <REMOTENAME>
git push  <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME> 

Delete a remote branch

git push origin --delete <branch>

or

git push origin :<branch>

Copy remote branch into another remote branch

git push origin origin/<old_branch_name>:refs/heads/<new_branch_name>

Rename a remote branch

git push origin origin/<old_branch_name>:refs/heads/<new_branch_name> :<old_branch_name>

Reflect deletion of remote branches into the local database

git fetch [remote] -p

Discovery

git ls-files
git ls-files -m # List modified files only
git branch -a

Cloning

# All branches, all history
git clone <repo_url> [local_dir]

# Specific branche
git clone -b <branch> <repo_url>

# Not all history
git clone --depth 1 <repo_url> 

# Into another bare repo
git clone --bare . /other/dir/name.git

Sparse Configuration for Existing Repos

# Enable sparse configuration
git config core.sparsecheckout true

# Set what do you want to track
echo some/dir/ >> .git/info/sparse-checkout
echo another/dir >> .git/info/sparse-checkout

# And finally
git read-tree -mu HEAD

 

Comments

عبد المنعم 1 year ago
ايه!!!!!
أم العيال 11 month ago
من فضلك ترجم
زغباوية 11 month ago
انزل بالترجمة!!!
M.Bastaka 9 month ago
We missed these notes since your departure BASTAKA
محمد عبد المنعم 9 month ago
Here we can bring back all those moments 🙂
zedony.com - A mmonem.com production