$git config

You can use it to configure the author’s name, email address, file formats and many more to be used with your commits.

git config --global user.name "Brice"
git config --global user.email "brice@example.com"

$git init

Using this command you make it sure that your git repository is initialized and creates the initial .git directory in a new or in an existing project. The output will be the following:

$  git init
You can undo a $git init with rm -rf .git.

Link a local project with a remote repository
git remote add origin https://github.com/T-z/androidMe.git
or 
git remote add origin https://github.com/T-z/androidMe
then now, "origin" have an url, 
and when you make git push -u origin master,
you are pushing your local change in the remote repository 
(in the master branch).

Change Git Remote URL

For example, let’s say that you want to change the URL of your Git origin remote.

$ git remote set-url origin https://git-repo/new-repository.git

In order to verify that the changes were made, you can use the “git remote” command with the “-v” option (for verbose)

$ git remote -v

Copy a repository
git clone /path/repository

You can clone one specific branch at a time: git clone -b <branch_name><repository_url>:

git clone -b branch_name git@github:user/repository.git

Push Branch to Another Repository

In order to push a branch to another repository, you need to execute the “git push” command, and specify the correct remote name as well as the branch to be pushed.

$ git push <remote> <branch>

Rename a Branch

To rename a branch in Git, you can follow these steps:

  • Check out a different branch. This step is necessary because you can’t rename the branch you are currently on. You can switch to any existing branch using the command git checkout <branch-name>.
  • Rename the branch using the -m option with the git branch command. Type the following command, replacing <old-branch-name> with the name of the branch you want to rename and <new-branch-name> with the desired new name:
git branch -m <old-branch-name> <new-branch-name>

// For example, if you want to rename a branch called "feature/foo" to "feature/bar", you //would run:

git branch -m feature/foo feature/bar

If the branch you renamed is the currently checked out branch, you need to update the upstream branch reference as well. Use the following command, replacing <new-branch-name> with the new name of the branch:

git push origin -u <new-branch-name>


//This command updates the upstream reference to the new branch name on the remote //repository.

Verify that the branch has been renamed by running:

git branch -a

The remote repository (URL)

In order to see the remotes defined in your repository, you have to execute the “git remote” command with the “-v” option for “verbose”.

$ git remote -v

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

Push Branch to Another Branch

In some cases, you may want to push your changes to another branch on the remote repository.

As an example, let’s say that you have created a local branch named “my-feature”.

$ git branch

  master
* my-feature
  feature

However, you want to push your changes to the remote branch named “feature” on your repository.

In order to push your branch to the “feature” branch, you would execute the following command

$ git push origin my-feature:feature

In order to push your branch to another branch, you may need to merge the remote branch to your current local branch.

In order to be merged, the tip of the remote branch cannot be behind the branch you are trying to push.

Before pushing, make sure to pull the changes from the remote branch and integrate them with your current local branch.

$ git pull

$ git checkout my-feature

$ git merge origin/feature

$ git push origin my-feature:feature
Note : when merging the remote branch, you are merging your local
 branch with the upstream branch of your local repository.
Rembember: After you pushed the change in the branch feature,
 The remote branch my-feature will be deleted.



List All Branches

To see local branches, run this command:

git branch

To see remote branches, run this command

git branch -r

To see all local and remote branches, run this command

git branch -a

Switch to a Branch That Came From a Remote Repo

Run this command to switch to the branch:

// 1. To get a list of all branches from the remote, run this command:
git pull

// then Run this command to switch to the branch
git checkout --track origin/my-branch-name

Comparing Actual Changes Between Two Branches

Let’s say you’d like to take a look at a feature branch named “feature/login”. You want to see all changes that are different from “master” – to get an idea of what would be integrated if you performed e.g. a git merge now.

$ git diff master..feature/login

you can also have Git show you the commits that are different. The solution is very similar, although we have to use the git log command in this case:

$ git log master..feature/login
git log is a command with dozens of interesting options.
 Feel free to tinker around a bit, for example by using the 
--oneline option to make the output a bit more concise:
$ git log --oneline master..feature/login

Comparing A Specific File Between Branches

Sometimes, you might want to compare how exactly a certain file is different in two branches. Simply add the file’s path to our git diff command from above:

$ git diff main..feature/login index.html

Discarding Local Changes in a File

Changes that haven’t been committed to the local repository are called “local” changes in Git. They exist in your Working Copy, but you haven’t wrapped them in a commit, yet.

If you want to discard this type of changes, you can use the git restore command:

git restore index.html
// This will undo all uncommitted local changes in the specified file. Please be careful // // because you cannot get these changes back once you've discarded them!
Discarding All Local Changes

If you want to undo all of your current changes, you can use the git restore command with the “.” parameter (instead of specifying a file path):

$ git restore .

If, additionally, you have untracked (= new) files in your Working Copy and want to get rid of those, too, then the git clean command is your friend:

$ git clean -f

View the change history of a specific file

To view the change history of an individual file : complete details with what has changed?

git log -p filename

Git : Who changes/modify the the file/line

If you only need the last change:

git blame <filename>
//or
git annotate <filename> 

To see commits affecting line 40 of file foo:

git blame -L 40,+1 foo

The +1 means exactly one line. To see changes for lines 40-60, it’s:

git blame -L 40,+21 foo

OR

git blame -L 40,60 foo

The second number can be an offset designated with a ‘+’, or a line number.


Merging 2 branches

Let´s assume we have created a new branch, and we made some changes / commits on it. The branch can now be merged back into the main code branch (usually master). That is how we shall do:

First we run git checkout master to change the active branch back to master. Then we run the command git merge new-branch to merge the new feature into the master

$ git checkout master
Switched to branch 'master'
$ git merge new-branch

Delete a remote branch

You can also delete a branch from your remote repository:

git push origin :<remote-branch-name>

//or 

$ git push origin --delete <remote-branch-name>

Delete a local branch in Git
$ git branch -d <local-branch>

Undo the last commit

You made a commit but now you regret it. Maybe you committed secrets by accident – not a good idea – or maybe you want to add more tests to your code changes. These are all legit reasons to undo your last commit.

And we are 2 ways to it, depending on the situation.

If you want to keep the changes from the commit you want to undo:

$ git reset --soft HEAD^

To destroy the changes from the commit you want to undo:

$ git reset --hard HEAD^

Changing a commit message

If the commit only exists in your local repository and has not been pushed to GitHub, you can amend the commit message with the git commit --amend command.

$ git commit --amend -m "new commit message"

Differences between branches

When you are working with multiple git branches, it’s important to be able to compare and contrast the differences between two different branches on the same repository. You can do this using the $ git diff command.

Find the diff between the tips of the two branches

$ git diff branch_1..branch_2

Produce the diff between two branches from common ancestor commit

$ git diff branch_1...branch_2

Comparing files between branches:

$ git diff branch1:file branch2:file

Change branch name
  1. Checkout to the branch you need to rename: $ git checkout <old-name>
  2. Rename branch name locally: $ git branch -m <new-name>
  3. Delete old branch from remote: $ git push origin :<old-name> <new-name>
  4. Reset the upstream branch for the new branch name: $ git push origin -u <new-name>

pull changes from the Remote
git pull origin master

This will pull changes from the origin remote, master branch and merge them to the local checked-out branch.


pull changes from the local branch
git pull origin/master

will pull changes from the locally stored branch origin/master and merge that to the local checked-out branch. The origin/master branch is essentially a “cached copy” of what was last pulled from origin, which is why it’s called a remote branch in git parlance. This might be somewhat confusing.

You can see what branches are available with git branch and git branch -r to see the “remote branches”.


Force “git pull” to overwrite local files ( !!!A revoir !!!)

Sometimes, we just want to force an overwrite of local files on a git pull.

// First, run a fetch to update
// all origin/<branch> refs to latest:
git fetch --all

// optional we can make git branch -r, to see
// the locally "remote branches" (cached copy") 

// then 
git reset --hard origin/<branch_name>
the git reset resets the current branch you are working on.
The --hard option changes all the files in your working tree
 to match the files in origin/<branch_name>

*Backup before resetting : It’s worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits.


Fixing issues-commit message

You can link a pull request to an issue by using a supported keyword in the pull request’s description or in a commit message (please note that the pull request must be on the default branch).

  • close
  • closes
  • closed
  • fix
  • fixes
  • fixed
  • resolve
  • resolves
  • resolved

for example: to link an issue with id=10

$ git commit -m "fixes #28"

The syntax for closing keywords depends on whether the issue is in the same repository as the pull request.

inked issueSyntaxExample
Issue in the same repositoryKEYWORD #ISSUE-NUMBERCloses #10
Issue in a different repositoryKEYWORD OWNER/REPOSITORY#ISSUE-NUMBERFixes octo-org/octo-repo#100
Multiple issuesUse full syntax for each issueResolves #10, resolves #123, resolves octo-
 

Find Latest Git Tag Available

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option.

git describe --tags
Tagging a project

You can use tagging to mark a significant change you made, such as a release.

git tag 1.0.0 <commit_id>

or we can make it simple way, with annotated Tag:

$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4

The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can type it in.

You can see the tag data along with the commit that was tagged by using the git show command:

$ git show v0.0.2
tag v0.0.2
Tagger: Brice Nguenkam <brice.nguenkam@vipco.de>
Date:   Sun Feb 21 23:02:06 2021 +0100

upgrade patch version

commit 3c659c6c1cd25b6150311c7bad0dfbf032a5fb31 (HEAD -> master, tag: v0.0.2, origin/master)
Author: Brice Nguenkam <brice.nguenkam@vipco.de>
Date:   Sun Feb 21 23:02:06 2021 +0100

    upgrade patch version

That shows the tagger information, the date the commit was tagged, and the annotation message before showing the commit information.


Checkout latest Git tag

In order to checkout the latest Git tag, first update your repository by fetching the remote tags available.

$ git fetch --tags


Fetching origin
From git-repository
   98a14be..7a9ad7f  master     -> origin/master
 * [new tag]         v2.0       -> v2.0
 * [new tag]         v1.0       -> v1.0

Sharing Tags

By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches : ?you can run git push origin <tagname>.

$ git $ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
 * [new tag]         v1.5 -> v1.5

If you have a lot of tags that you want to push up at once, you can also use the --tags option to the git push command. This will transfer all of your tags to the remote server that are not already there.

$ git push origin --tags

Deleting Tags

To delete a tag on your local repository, you can use git tag -d <tagname>.

$ git tag -d v1.4-lw
Deleted tag 'v1.4-lw' (was e7d5add)

Note that this does not remove the tag from any remote servers. There are two common variations for deleting a tag from a remote server.

The best (and more intuitive) way to delete a remote tag is with:

$ git push origin --delete <tagname>

Checking out Tags

If you want to view the versions of files a tag is pointing to, you can do a git checkout of that tag, although this puts your repository in “detached HEAD” state, which has some ill side effects:

$ git checkout v2.0.0
Note: switching to 'v2.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final

$ git checkout v2.0-beta-0.1
Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final
HEAD is now at df3f601... Add atlas.json and cover image

In “detached HEAD” state, if you make changes and then create a commit, the tag will stay the same, but your new commit won’t belong to any branch and will be unreachable, except by the exact commit hash. Thus, if you need to make changes?—?say you’re fixing a bug on an older version, for instance?—?you will generally want to create a branch:

$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'

If you do this and make a commit, your version2 branch will be slightly different than your v2.0.0 tag since it will move forward with your new changes, so do be careful.


Log- Listing of commits

It shows a listing of commits on a branch with corresponding details.

$git log

//or for a contracted form 

$git shortlog

The git shortlog command is a special version of git log intended for creating release announcements. It groups each commit by author and displays the first line of each commit message. This is an easy way to see who’s been working on what.

$ git shortlog
Brice Nguenkam (7):
      first init
      initial commit 11.11.2020
      first init
      modify expertise role ( typescripter)
      0.0.1
      upgrade to angular version 11
      upgrade patch version

 List the files tracked on the current branch
$ git ls-tree -r master

Remove a file from git tracking (git status )

The easiest way to delete a file in your Git repository is to execute the “git rm” command and to specify the file to be deleted.

$ git rm <file>

$ git commit -m "Deleted the file from the git repository"

$ git push

 you will also have to commit your changes, “git rm” does not remove the file from the Git index unless you commit it.

Note that by using the “git rm” command, the file will also be
deleted from the filesystem. If you dont want to delete it from
the filesystem, then use the option --cached.
( git rm --cached <file> )

Remove a folder from git tracking (git status )

How to remove a folder from git repository without deleting it from the local machine (i.e., development environment)?

Step 1. Add the folder path to your repo’s root .gitignore file.

//example: /node_modules
path_to_your_folder/

Step 2. Remove the folder from your local git tracking, but keep it on your disk.

// example: git rm -r --cached node_modules
git rm -r --cached path_to_your_folder/
the option -r is put for "recursively", because a folder contains many files.

Step 3. Push your changes to your git repo.

The folder will be considered "deleted" from Git's point of view
(i.e. they are in past history, but not in the latest commit,
and people pulling from this repo will get the files removed
from their trees), but stay on your working directory because
you've used --cached.

Revert/ Undo "git add ” 

You can undo git add before commit with:

git reset <file>

which will remove it from the current index (the “about to be committed” list) without changing anything else.

You can use it without any file name to unstage all due changes

git reset
Return to a previous commit

To jump back to a previous commit, first find the commit’s hash using git log

To temporarily jump back to that commit, detach your head with:

git checkout 789abcd

This places you at commit 789abcd. You can now make new commits on top of this old commit without affecting the branch your head is on.

To roll back to a previous commit while keeping the changes:

git reset --soft 789abcd

To roll back the last commit:

git reset --soft HEAD~

To permanently discard any changes made after a specific commit, use:

git reset --hard 789abcd

To permanently discard any changes made after the last commit:

git reset --hard HEAD~

Beware: While you can recover the discarded commit using reflog and reset , uncommitted changes cannot be recovered. Use git stash; git reset instead of git reset --hard to be safe.

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *